-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathcycle.rs
More file actions
384 lines (335 loc) · 9.43 KB
/
Copy pathcycle.rs
File metadata and controls
384 lines (335 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//! Tests related to cycles amongst impls, which we try to handle with
//! grace.
use super::*;
#[test]
fn inner_cycle() {
// Interesting test that shows why recursive solver needs to run
// to an inner fixed point during iteration. Here, the first
// round, we get that `?T: A` has a unique sol'n `?T = i32`. On
// the second round, we ought to get ambiguous: but if we don't
// run the `?T: B` to a fixed point, it will terminate with `?T =
// i32`, leading to an (incorrect) unique solution.
test! {
program {
#[marker]
trait A { }
#[marker]
trait B { }
struct Foo { }
struct Vec<T> { }
impl<T> A for T where T: B { }
impl A for Foo { }
impl<T> B for T where T: A { }
impl<T> B for Vec<T> where T: B { }
}
goal {
exists<T> { T: A }
} yields {
expect![["Ambiguous; no inference guidance"]]
}
}
}
#[test]
fn cycle_no_solution() {
test! {
program {
trait Foo { }
struct S<T> { }
impl<T> Foo for S<T> where T: Foo { }
}
// only solution: infinite type S<S<S<...
goal {
exists<T> {
T: Foo
}
} yields {
expect![["No possible solution"]]
}
}
}
#[test]
fn cycle_many_solutions() {
test! {
program {
trait Foo { }
struct S<T> { }
struct Zero { }
impl<T> Foo for S<T> where T: Foo { }
impl Foo for Zero { }
}
// infinite family of solutions: {Zero, S<Zero>, S<S<Zero>>, ... }
goal {
exists<T> {
T: Foo
}
} yields {
expect![["Ambiguous; no inference guidance"]]
}
}
}
#[test]
fn cycle_unique_solution() {
test! {
program {
trait Foo { }
trait Bar { }
struct S<T> { }
struct Zero { }
impl<T> Foo for S<T> where T: Foo, T: Bar { }
impl Foo for Zero { }
}
goal {
exists<T> {
T: Foo
}
} yields {
expect![["Unique; substitution [?0 := Zero]"]]
}
}
}
#[test]
fn multiple_ambiguous_cycles() {
test! {
program {
trait WF { }
trait Sized { }
struct Vec<T> { }
struct Int { }
impl Sized for Int { }
impl WF for Int { }
impl<T> WF for Vec<T> where T: Sized { }
impl<T> Sized for Vec<T> where T: WF, T: Sized { }
}
// ?T: WF
// |
// |
// |
// Int: WF. <-----> (Vec<?T>: WF) :- (?T: Sized)
// |
// |
// |
// Int: Sized. <-------> (Vec<?T>: Sized) :- (?T: Sized), (?T: WF)
// | |
// | |
// | |
// cycle cycle
//
// Depending on the evaluation order of the above tree (which cycle we come upon first),
// we may fail to reach a fixed point if we loop continuously because `Ambig` does not perform
// any unification. We must stop looping as soon as we encounter `Ambig`. In fact without
// this strategy, the above program will not even be loaded because of the overlap check which
// will loop forever.
goal {
exists<T> {
T: WF
}
} yields {
expect![["Ambiguous; no inference guidance"]]
}
}
}
#[test]
fn overflow() {
test! {
program {
trait Q { }
struct Z { }
struct G<X> { }
struct S<X> { }
impl Q for Z { }
impl<X> Q for G<X> where X: Q { }
impl<X> Q for S<X> where X: Q, S<G<X>>: Q { }
}
// Will try to prove S<G<Z>>: Q then S<G<G<Z>>>: Q etc ad infinitum
goal {
S<Z>: Q
} yields[SolverChoice::slg(10, None)] {
expect![["Ambiguous; no inference guidance"]]
} yields[SolverChoice::recursive_default()] {
expect![["Ambiguous; no inference guidance"]]
}
}
}
#[test]
fn overflow_universe() {
test! {
program {
struct Foo { }
trait Bar { }
// When asked to solve X: Bar, we will produce a
// requirement to solve !1_0: Bar. And then when asked to
// solve that, we'll produce a requirement to solve !1_1:
// Bar. And so forth.
forall<X> { X: Bar if forall<Y> { Y: Bar } }
}
goal {
Foo: Bar
} yields {
// The internal universe canonicalization in the on-demand/recursive
// solver means that when we are asked to solve (e.g.)
// `!1_1: Bar`, we rewrite that to `!1_0: Bar`, identifying a
// cycle.
expect![["No possible solution"]]
}
}
}
#[test]
fn infinite_recursion() {
test! {
program {
trait A { }
trait B { }
trait C { }
trait D { }
struct Vec<T> { }
impl<T> A for Vec<T> where T: B { }
impl<T> B for Vec<T> where T: C { }
impl<T> C for Vec<T> where T: D { }
impl<T> D for Vec<T> where T: A { }
}
goal {
exists<T> { T: A }
} yields_all[SolverChoice::slg(10, None)] {
}
}
}
// Regression test for chalk#571
#[test]
fn cycle_with_ambiguity() {
test! {
program {
#[lang(sized)]
trait Sized { }
trait From<T> {}
trait ToOwned {
type Owned;
}
impl<T> ToOwned for [T] where T: Sized {
type Owned = Vec<T>;
}
struct Rc<T> { }
struct Vec<T> {}
struct Cow<T> {}
impl<T> From<Vec<T>> for Rc<[T]> {}
impl<B> From<Cow<B>> for Rc<B>
where
B: ToOwned,
Rc<B>: From<<B as ToOwned>::Owned>
{
}
}
goal {
exists<S, T> {
Rc<S>: From<T>
}
} yields[SolverChoice::slg_default()] {
expect![["Ambiguous; no inference guidance"]]
}
}
}
#[test]
fn inductive_canonical_cycle() {
test! {
program {
trait Trait<T, U> {}
trait IsNotU32 {}
impl IsNotU32 for i32 {}
impl IsNotU32 for i16 {}
impl<T, U> Trait<T, U> for ()
where
(): Trait<U, T>,
T: IsNotU32,
{}
impl<T> Trait<u32, T> for () {}
}
goal {
(): Trait<i32, u32>
} yields {
expect![["Unique"]]
}
goal {
(): Trait<u32, i32>
} yields {
expect![["Unique"]]
}
goal {
exists<T, U> {
(): Trait<T, U>
}
} yields[SolverChoice::slg(10, None)] {
expect![["Ambiguous; no inference guidance"]]
} yields[SolverChoice::recursive_default()] {
expect![["Ambiguous; no inference guidance"]]
}
}
}
#[test]
fn mixed_cycle_detection_not_on_stack1() {
test! {
program {
#[coinductive]
trait A<T> {}
#[coinductive]
trait B<T> {}
trait C<T> {}
impl<T> A<T> for ()
where
(): B<T>,
(): C<T>,
{}
impl<T> B<T> for ()
where
(): A<T>,
{}
impl<T> C<T> for ()
where
(): B<T>,
{}
}
goal {
exists<T> {
(): A<T>
}
} yields[SolverChoice::slg(10, None)] {
expect![["No possible solution"]]
} yields[SolverChoice::recursive_default()] {
expect![["No possible solution"]]
}
}
}
#[test]
fn mixed_cycle_detection_not_on_stack2() {
test! {
program {
#[coinductive]
trait A<T> {}
#[coinductive]
trait B<T> {}
trait C<T> {}
impl<T> A<T> for ()
where
(): C<T>,
(): B<T>,
{}
impl<T> B<T> for ()
where
(): A<T>,
{}
impl<T> C<T> for ()
where
(): B<T>,
{}
}
goal {
exists<T> {
(): A<T>
}
} yields[SolverChoice::slg(10, None)] {
// FIXME: this should be no solution as `C` is inductive
expect![["Unique; for<?U0> { substitution [?0 := ^0.0] }"]]
} yields[SolverChoice::recursive_default()] {
// FIXME: this should be no solution as `C` is inductive
expect![["Unique; for<?U0> { substitution [?0 := ^0.0] }"]]
}
}
}