-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathtest.rs
640 lines (524 loc) · 15.4 KB
/
test.rs
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#![cfg(test)]
use test_util::*;
#[test]
fn well_formed_trait_decl() {
lowering_success! {
program {
trait Clone { }
trait Copy where Self: Clone { }
struct i32 { }
impl Clone for i32 { }
impl Copy for i32 { }
}
}
}
#[test]
fn ill_formed_trait_decl() {
lowering_error! {
program {
trait Clone { }
trait Copy where Self: Clone { }
struct i32 { }
impl Copy for i32 { }
} error_msg {
"trait impl for \"Copy\" does not meet well-formedness requirements"
}
}
}
#[test]
fn cyclic_traits() {
lowering_success! {
program {
trait A where Self: B { }
trait B where Self: A { }
impl<T> B for T { }
impl<T> A for T { }
}
}
lowering_error! {
program {
trait Copy { }
trait A where Self: B, Self: Copy {}
trait B where Self: A { }
// This impl won't be able to prove that `T: Copy` holds.
impl<T> B for T {}
impl<T> A for T where T: B {}
} error_msg {
"trait impl for \"B\" does not meet well-formedness requirements"
}
}
lowering_success! {
program {
trait Copy { }
trait A where Self: B, Self: Copy {}
trait B where Self: A { }
impl<T> B for T where T: Copy {}
impl<T> A for T where T: B {}
}
}
}
#[test]
fn cyclic_wf_requirements() {
lowering_success! {
program {
trait Foo where <Self as Foo>::Value: Foo {
type Value;
}
struct Unit { }
impl Foo for Unit {
type Value = Unit;
}
}
}
}
#[test]
fn ill_formed_assoc_ty() {
lowering_error! {
program {
trait Foo { }
struct OnlyFoo<T> where T: Foo { }
struct i32 { }
trait Bar {
type Value;
}
impl Bar for i32 {
// `OnlyFoo<i32>` is ill-formed because `i32: Foo` does not hold.
type Value = OnlyFoo<i32>;
}
} error_msg {
"trait impl for \"Bar\" does not meet well-formedness requirements"
}
}
}
#[test]
fn implied_bounds() {
lowering_success! {
program {
trait Eq { }
trait Hash where Self: Eq { }
struct Set<K> where K: Hash { }
struct OnlyEq<T> where T: Eq { }
trait Foo {
type Value;
}
impl<K> Foo for Set<K> {
// Here, `WF(Set<K>)` implies `K: Hash` and hence `OnlyEq<K>` is WF.
type Value = OnlyEq<K>;
}
}
}
}
#[test]
fn ill_formed_ty_decl() {
lowering_error! {
program {
trait Hash { }
struct Set<K> where K: Hash { }
struct MyType<K> {
value: Set<K>
}
} error_msg {
"type declaration \"MyType\" does not meet well-formedness requirements"
}
}
}
#[test]
fn implied_bounds_on_ty_decl() {
lowering_success! {
program {
trait Eq { }
trait Hash where Self: Eq { }
struct OnlyEq<T> where T: Eq { }
struct MyType<K> where K: Hash {
value: OnlyEq<K>
}
}
}
}
#[test]
fn wf_requiremements_for_projection() {
lowering_error! {
program {
trait Foo {
type Value;
}
trait Iterator {
type Item;
}
impl<T> Foo for T {
// The projection is well-formed if `T: Iterator` holds, which cannot
// be proved here.
type Value = <T as Iterator>::Item;
}
} error_msg {
"trait impl for \"Foo\" does not meet well-formedness requirements"
}
}
lowering_success! {
program {
trait Foo {
type Value;
}
trait Iterator {
type Item;
}
impl<T> Foo for T where T: Iterator {
type Value = <T as Iterator>::Item;
}
}
}
}
#[test]
fn ill_formed_type_in_header() {
lowering_error! {
program {
trait Foo {
type Value;
}
trait Bar { }
// Types in where clauses are not assumed to be well-formed,
// an explicit where clause would be needed (see below).
impl<T> Bar for T where <T as Foo>::Value: Bar { }
} error_msg {
"trait impl for \"Bar\" does not meet well-formedness requirements"
}
}
lowering_success! {
program {
trait Foo {
type Value;
}
trait Bar { }
impl<T> Bar for T where T: Foo, <T as Foo>::Value: Bar { }
}
}
}
#[test]
fn bound_in_header_from_env() {
lowering_success! {
program {
trait Foo { }
trait Bar {
type Item: Foo;
}
struct Stuff<T> { }
impl<T> Bar for Stuff<T> where T: Foo {
// Should have FromEnv(T: Foo) here.
type Item = T;
}
}
}
lowering_error! {
program {
trait Foo { }
trait Baz { }
trait Bar {
type Item: Baz;
}
struct Stuff<T> { }
impl<T> Bar for Stuff<T> where T: Foo {
// No T: Baz here.
type Item = T;
}
} error_msg {
"trait impl for \"Bar\" does not meet well-formedness requirements"
}
}
}
#[test]
fn mixed_indices_check_projection_bounds() {
lowering_success! {
program {
trait Foo<T> { }
trait Bar<T> {
type Item: Foo<T>;
}
struct Stuff<T, U> { }
impl<T, U> Bar<T> for Stuff<T, U> where U: Foo<T> {
type Item = U;
}
}
}
lowering_error! {
program {
trait Foo<T> { }
trait Baz<T> { }
trait Bar<T> {
type Item: Baz<T>;
}
struct Stuff<T, U> { }
impl<T, U> Bar<T> for Stuff<T, U> where U: Foo<T> {
type Item = U;
}
} error_msg {
"trait impl for \"Bar\" does not meet well-formedness requirements"
}
}
}
#[test]
fn mixed_indices_check_generic_projection_bounds() {
lowering_success! {
program {
struct Stuff<T, U> { }
trait Foo<T> { }
// A type that impls Foo<T> as long as U: Foo<T>.
struct Fooey<U, V> { }
impl<T, U, V> Foo<T> for Fooey<U, V> where U: Foo<T> { }
trait Bar<T> {
type Item<V>: Foo<T> where V: Foo<T>;
}
impl<T, U> Bar<T> for Stuff<T, U> where U: Foo<T> {
type Item<V> = Fooey<U, V>;
}
}
}
lowering_error! {
program {
struct Stuff<T, U> { }
trait Foo<T> { }
trait Baz<T> { }
// A type that impls Foo<T> as long as U: Foo<T>.
struct Fooey<U, V> { }
impl<T, U, V> Foo<T> for Fooey<U, V> where U: Foo<T> { }
trait Bar<T> {
type Item<V>: Baz<T> where V: Foo<T>;
}
impl<T, U> Bar<T> for Stuff<T, U> where U: Foo<T> {
type Item<V> = Fooey<U, V>;
}
} error_msg {
"trait impl for \"Bar\" does not meet well-formedness requirements"
}
}
}
#[test]
fn generic_projection_where_clause() {
lowering_success! {
program {
trait PointerFamily { type Pointer<T>; }
struct Cow<T> { }
struct CowFamily { }
impl PointerFamily for CowFamily { type Pointer<T> = Cow<T>; }
struct String { }
struct Foo<P> where P: PointerFamily {
bar: <P as PointerFamily>::Pointer<String>
}
}
}
lowering_error! {
program {
trait Copy { }
trait PointerFamily { type Pointer<T> where T: Copy; }
struct Cow<T> { }
struct CowFamily { }
impl PointerFamily for CowFamily { type Pointer<T> = Cow<T>; }
struct String { }
struct Foo<P> where P: PointerFamily {
// No impl Copy for String, so this will fail.
bar: <P as PointerFamily>::Pointer<String>
}
} error_msg {
"type declaration \"Foo\" does not meet well-formedness requirements"
}
}
}
#[test]
fn generic_projection_bound() {
lowering_success! {
program {
trait Clone { }
trait PointerFamily { type Pointer<T>: Clone where T: Clone; }
struct Cow<T> { }
impl<T> Clone for Cow<T> where T: Clone { }
struct CowFamily { }
// impl is WF due because of:
// - `where T: Clone` clause on PointerFamily::Pointer<T>
// - impl<T> Clone for Cow<T> where T: Clone
impl PointerFamily for CowFamily { type Pointer<T> = Cow<T>; }
struct String { }
impl Clone for String { }
struct Foo<P> where P: PointerFamily {
bar: <P as PointerFamily>::Pointer<String>
}
}
}
lowering_error! {
program {
trait Clone { }
trait PointerFamily { type Pointer<T>: Clone where T: Clone; }
struct Cow<T> { }
struct CowFamily { }
// No impl Clone for Cow<T>, so this will fail.
impl PointerFamily for CowFamily { type Pointer<T> = Cow<T>; }
} error_msg {
"trait impl for \"PointerFamily\" does not meet well-formedness requirements"
}
}
}
#[test]
fn higher_ranked_trait_bounds() {
lowering_error! {
program {
trait Foo<'a> { }
trait Bar where forall<'a> Self: Foo<'a> { }
struct i32 { }
impl Bar for i32 { }
} error_msg {
"trait impl for \"Bar\" does not meet well-formedness requirements"
}
}
lowering_success! {
program {
trait Foo<'a> { }
trait Bar where forall<'a> Self: Foo<'a> { }
struct i32 { }
impl<'a> Foo<'a> for i32 { }
impl Bar for i32 { }
}
}
}
#[test]
fn higher_ranked_trait_bound_on_gat() {
lowering_success! {
program {
trait Foo<'a> { }
struct i32 { }
trait Bar<'a> {
type Item<V>: Foo<'a> where forall<'b> V: Foo<'b>;
}
impl<'a> Bar<'a> for i32 {
type Item<V> = V;
}
}
}
}
// See `cyclic_traits`, this is essentially the same but with higher-ranked co-inductive WF goals.
#[test]
fn higher_ranked_cyclic_requirements() {
lowering_success! {
program {
trait Foo<T> where forall<U> Self: Bar<U> { }
trait Bar<T> where forall<U> Self: Foo<T> { }
impl<T, U> Foo<T> for U { }
impl<T, U> Bar<T> for U { }
}
}
lowering_error! {
program {
trait Copy { }
trait Foo<T> where forall<U> Self: Bar<U>, Self: Copy { }
trait Bar<T> where forall<U> Self: Foo<T> { }
impl<T, U> Foo<T> for U { }
impl<T, U> Bar<T> for U where U: Foo<T> { }
} error_msg {
"trait impl for \"Foo\" does not meet well-formedness requirements"
}
}
lowering_success! {
program {
trait Copy { }
trait Foo<T> where forall<U> Self: Bar<U>, Self: Copy { }
trait Bar<T> where forall<U> Self: Foo<T> { }
impl<T, U> Foo<T> for U where U: Copy { }
impl<T, U> Bar<T> for U where U: Foo<T> { }
}
}
}
#[test]
fn higher_ranked_inline_bound_on_gat() {
lowering_success! {
program {
trait Fn<T> { }
struct Ref<'a, T> { }
struct i32 {}
struct fn<T> { }
impl<'a, T> Fn<Ref<'a, T>> for for<'b> fn<Ref<'b, T>> { }
trait Bar {
type Item<T>: forall<'a> Fn<Ref<'a, T>>;
}
impl Bar for i32 {
type Item<T> = for<'a> fn<Ref<'a, T>>;
}
}
}
lowering_error! {
program {
trait Fn<T, U> { }
struct i32 {}
struct fn<T, U> { }
impl<T, U> Fn<T, U> for fn<T, U> { }
trait Bar {
type Item<T>: forall<U> Fn<T, U>;
}
impl Bar for i32 {
type Item<T> = fn<T, i32>;
}
} error_msg {
"trait impl for \"Bar\" does not meet well-formedness requirements"
}
}
}
#[test]
fn assoc_type_recursive_bound() {
lowering_error! {
program {
trait Sized { }
trait Print {
// fn print();
}
trait Foo {
type Item: Sized where <Self as Foo>::Item: Sized;
}
struct i32 { }
struct str { } // not sized
impl Foo for i32 {
// Well-formedness checks require that the following
// goal is true:
// ```
// if (str: Sized) { # if the where clauses hold
// str: Sized # then the bound on the associated type hold
// }
// ```
// which it is :)
type Item = str;
}
struct OnlySized<T> where T: Sized { }
impl<T> Print for OnlySized<T> {
// fn print() {
// println!("{}", std::mem::size_of::<T>());
// }
}
trait Bar {
type Assoc: Print;
}
impl<T> Bar for T where T: Foo {
type Assoc = OnlySized<<T as Foo>::Item>;
}
// Above, we used to incorrectly assume that `OnlySized<<T as Foo>::Item>`
// is well-formed because of the `FromEnv(T: Foo)`, hence making the `T: Bar`
// impl pass the well-formedness check. But the following query will
// (and should) always succeed, as there is no where clauses on `Assoc`:
// ```
// forall<T> { if (T: Bar) { WellFormed(<T as Bar>::Assoc) } }
// ```
//
// This may lead to the following code to compile:
// ```
// fn foo<T: Print>() {
// T::print() // oops, in fact `T = OnlySized<str>` which is ill-formed
// }
// fn bar<T: Bar> {
// // ok, we have `FromEnv(T: Bar)` hence
// // `<T as Bar>::Assoc` is well-formed and
// // `Implemented(<T as Bar>::Assoc: Print)` hold
// foo<<T as Bar>::Assoc>(
// }
// fn main() {
// bar::<i32>() // ok, `Implemented(i32: Bar)` hold
// }
// ```
} error_msg {
"trait impl for \"Bar\" does not meet well-formedness requirements"
}
}
}