|
| 1 | +use crate::test::*; |
| 2 | + |
| 3 | +#[test] |
| 4 | +fn dispatch_from_dyn() { |
| 5 | + test! { |
| 6 | + program { |
| 7 | + #[lang(dispatch_from_dyn)] |
| 8 | + trait DispatchFromDyn<T> {} |
| 9 | + |
| 10 | + impl<'a, T, U> DispatchFromDyn<&'a U> for &'a T {} |
| 11 | + } |
| 12 | + |
| 13 | + // Smoke test that DispatchFromDyn works just like any other impl. |
| 14 | + goal { |
| 15 | + forall<'a> { |
| 16 | + &'a u8: DispatchFromDyn<&'a u8> |
| 17 | + } |
| 18 | + } yields { |
| 19 | + "Unique" |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +#[test] |
| 25 | +fn dispatch_from_dyn_wf() { |
| 26 | + lowering_success! { |
| 27 | + program { |
| 28 | + #[lang(dispatch_from_dyn)] |
| 29 | + trait DispatchFromDyn<T> {} |
| 30 | + |
| 31 | + #[one_zst] |
| 32 | + struct Zst<T> {} |
| 33 | + |
| 34 | + struct Foo<T> { |
| 35 | + f: *mut T, |
| 36 | + f2: Zst<u8>, |
| 37 | + } |
| 38 | + |
| 39 | + // References and pointers |
| 40 | + impl<'a, T, U> DispatchFromDyn<&'a U> for &'a T {} |
| 41 | + impl<'a, T, U> DispatchFromDyn<&'a mut U> for &'a mut T {} |
| 42 | + impl<T, U> DispatchFromDyn<*const U> for *const T {} |
| 43 | + impl<T, U> DispatchFromDyn<*mut U> for *mut T {} |
| 44 | + |
| 45 | + // Struct |
| 46 | + impl<T, U> DispatchFromDyn<Foo<U>> for Foo<T> {} |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Reference: mutability mismatch |
| 51 | + lowering_error! { |
| 52 | + program { |
| 53 | + #[lang(dispatch_from_dyn)] |
| 54 | + trait DispatchFromDyn<T> {} |
| 55 | + |
| 56 | + impl<'a, T, U> DispatchFromDyn<&'a U> for &'a mut T {} |
| 57 | + } error_msg { |
| 58 | + "trait impl for `DispatchFromDyn` does not meet well-formedness requirements" |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Raw pointer: mutability mismatch |
| 63 | + lowering_error! { |
| 64 | + program { |
| 65 | + #[lang(dispatch_from_dyn)] |
| 66 | + trait DispatchFromDyn<T> {} |
| 67 | + |
| 68 | + impl<'a, T, U> DispatchFromDyn<*mut U> for *const T {} |
| 69 | + } error_msg { |
| 70 | + "trait impl for `DispatchFromDyn` does not meet well-formedness requirements" |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // No non-ZST fields |
| 75 | + lowering_error! { |
| 76 | + program { |
| 77 | + #[lang(dispatch_from_dyn)] |
| 78 | + trait DispatchFromDyn<T> {} |
| 79 | + |
| 80 | + #[one_zst] |
| 81 | + struct Zst<T> {} |
| 82 | + |
| 83 | + struct Foo<T> { |
| 84 | + f: Zst<T>, |
| 85 | + } |
| 86 | + |
| 87 | + impl<T, U> DispatchFromDyn<Foo<U>> for Foo<T> {} |
| 88 | + } error_msg { |
| 89 | + "trait impl for `DispatchFromDyn` does not meet well-formedness requirements" |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Too many fields |
| 94 | + lowering_error! { |
| 95 | + program { |
| 96 | + #[lang(dispatch_from_dyn)] |
| 97 | + trait DispatchFromDyn<T> {} |
| 98 | + |
| 99 | + struct Foo<T> { |
| 100 | + f: *mut T, |
| 101 | + f2: u8, |
| 102 | + } |
| 103 | + |
| 104 | + impl<T, U> DispatchFromDyn<Foo<U>> for Foo<T> {} |
| 105 | + } error_msg { |
| 106 | + "trait impl for `DispatchFromDyn` does not meet well-formedness requirements" |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Field does not impl DispatchFromDyn |
| 111 | + lowering_error! { |
| 112 | + program { |
| 113 | + #[lang(dispatch_from_dyn)] |
| 114 | + trait DispatchFromDyn<T> {} |
| 115 | + |
| 116 | + struct Foo<T> { |
| 117 | + f: T, |
| 118 | + } |
| 119 | + |
| 120 | + impl<T, U> DispatchFromDyn<Foo<U>> for Foo<T> {} |
| 121 | + } error_msg { |
| 122 | + "trait impl for `DispatchFromDyn` does not meet well-formedness requirements" |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Field type does not change |
| 127 | + lowering_error! { |
| 128 | + program { |
| 129 | + #[lang(dispatch_from_dyn)] |
| 130 | + trait DispatchFromDyn<T> {} |
| 131 | + |
| 132 | + #[one_zst] |
| 133 | + struct Zst<T> {} |
| 134 | + |
| 135 | + struct Foo<T> { |
| 136 | + f: *const u8, |
| 137 | + f2: Zst<T>, |
| 138 | + } |
| 139 | + |
| 140 | + impl<T, U> DispatchFromDyn<Foo<U>> for Foo<T> {} |
| 141 | + } error_msg { |
| 142 | + "trait impl for `DispatchFromDyn` does not meet well-formedness requirements" |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // Different definitions |
| 147 | + lowering_error! { |
| 148 | + program { |
| 149 | + #[lang(dispatch_from_dyn)] |
| 150 | + trait DispatchFromDyn<T> {} |
| 151 | + |
| 152 | + struct Foo<T> { |
| 153 | + f: *const T, |
| 154 | + } |
| 155 | + |
| 156 | + struct Bar<T> { |
| 157 | + f: *const T, |
| 158 | + } |
| 159 | + |
| 160 | + impl<T, U> DispatchFromDyn<Bar<U>> for Foo<T> {} |
| 161 | + } error_msg { |
| 162 | + "trait impl for `DispatchFromDyn` does not meet well-formedness requirements" |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // Not a struct |
| 167 | + lowering_error! { |
| 168 | + program { |
| 169 | + #[lang(dispatch_from_dyn)] |
| 170 | + trait DispatchFromDyn<T> {} |
| 171 | + |
| 172 | + enum Foo<T> { |
| 173 | + Bar(*const T), |
| 174 | + } |
| 175 | + |
| 176 | + impl<T, U> DispatchFromDyn<Foo<U>> for Foo<T> {} |
| 177 | + } error_msg { |
| 178 | + "trait impl for `DispatchFromDyn` does not meet well-formedness requirements" |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + // repr(C) |
| 183 | + lowering_error! { |
| 184 | + program { |
| 185 | + #[lang(dispatch_from_dyn)] |
| 186 | + trait DispatchFromDyn<T> {} |
| 187 | + |
| 188 | + #[repr(C)] |
| 189 | + struct Foo<T> { |
| 190 | + f: *mut T, |
| 191 | + } |
| 192 | + |
| 193 | + impl<T, U> DispatchFromDyn<Foo<U>> for Foo<T> {} |
| 194 | + } error_msg { |
| 195 | + "trait impl for `DispatchFromDyn` does not meet well-formedness requirements" |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments