Skip to content

Commit e92db1d

Browse files
committed
Address review comments
Signed-off-by: Nick Cameron <nrc@ncameron.org>
1 parent 60593cc commit e92db1d

File tree

4 files changed

+202
-199
lines changed

4 files changed

+202
-199
lines changed

chalk-solve/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pub trait RustIrDatabase<I: Interner>: Debug {
6767
/// Returns the representation for the ADT definition with the given id.
6868
fn adt_repr(&self, id: AdtId<I>) -> Arc<AdtRepr<I>>;
6969

70+
/// Returns the siza and alignment of the ADT definition with the given id.
7071
fn adt_size_align(&self, id: AdtId<I>) -> Arc<AdtSizeAlign>;
7172

7273
/// Returns the datum for the fn definition with the given id.

chalk-solve/src/wf.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,8 +1078,9 @@ impl WfWellKnownConstraints {
10781078
/// - Self and the type parameter must be structs
10791079
/// - Self and the type parameter must have the same definitions
10801080
/// - Self must not be `#[repr(packed)]` or `#[repr(C)]`
1081-
/// - Self must have exactly one field which is not a 1-ZST, and that field must have a
1082-
/// different type in the type parameter (i.e., it is a field being coerced)
1081+
/// - Self must have exactly one field which is not a 1-ZST (there may be any number of 1-ZST
1082+
/// fields), and that field must have a different type in the type parameter (i.e., it is
1083+
/// the field being coerced)
10831084
/// - `DispatchFromDyn` is implemented for the type of the field being coerced.
10841085
fn dispatch_from_dyn_constraint<I: Interner>(
10851086
solver: &mut dyn Solver<I>,

tests/test/dispatch_from_dyn.rs

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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

Comments
 (0)