-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathtest.rs
462 lines (408 loc) · 9.57 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
#![cfg(test)]
use chalk_ir::tls;
use chalk_solve::solve::SolverChoice;
use std::sync::Arc;
use test_util::*;
#[test]
fn lower_success() {
lowering_success! {
program {
struct Foo { field: Foo }
trait Bar { }
impl Bar for Foo { }
}
}
}
#[test]
fn not_trait() {
lowering_error! {
program {
struct Foo { }
trait Bar { }
impl Foo for Bar { }
}
error_msg {
"expected a trait, found `Foo`, which is not a trait"
}
}
}
#[test]
fn auto_trait() {
lowering_error! {
program {
#[auto] trait Foo<T> { }
}
error_msg {
"auto trait cannot have parameters"
}
}
lowering_error! {
program {
trait Bar { }
#[auto] trait Foo where Self: Bar { }
}
error_msg {
"auto trait cannot have where clauses"
}
}
lowering_error! {
program {
#[auto] trait Foo {
type Item;
}
}
error_msg {
"auto trait cannot define associated types"
}
}
lowering_success! {
program {
#[auto] trait Send { }
}
}
}
#[test]
fn negative_impl() {
lowering_error! {
program {
trait Foo {
type Item;
}
struct i32 { }
impl !Foo for i32 {
type Item = i32;
}
}
error_msg {
"negative impls cannot define associated values"
}
}
lowering_success! {
program {
trait Foo { }
trait Iterator {
type Item;
}
struct i32 { }
impl<T> !Foo for T where T: Iterator<Item = i32> { }
}
}
}
#[test]
fn invalid_name() {
lowering_error! {
program {
struct Foo { }
trait Bar { }
impl Bar for X { }
}
error_msg {
"invalid type name `X`"
}
}
}
#[test]
fn type_parameter() {
lowering_success! {
program {
struct Foo { }
trait Bar { }
impl<X> Bar for X { }
}
}
}
#[test]
fn type_parameter_bound() {
lowering_success! {
program {
struct Foo { }
trait Bar { }
trait Eq { }
impl<X> Bar for X where X: Eq { }
}
}
}
#[test]
fn assoc_tys() {
lowering_success! {
program {
struct String { }
struct Char { }
trait Iterator { type Item; }
impl Iterator for String { type Item = Char; }
trait Foo { }
impl<X> Foo for <X as Iterator>::Item where X: Iterator { }
}
}
}
#[test]
fn goal_quantifiers() {
let program = Arc::new(
parse_and_lower_program(
"trait Foo<A, B> { }",
SolverChoice::default()
).unwrap()
);
let goal = parse_and_lower_goal(
&program,
"forall<X> {exists<Y> {forall<Z> {Z: Foo<Y, X>}}}"
).unwrap();
tls::set_current_program(&program, || {
assert_eq!(
format!("{:?}", goal),
"ForAll<type> { Exists<type> { ForAll<type> { Implemented(^0: Foo<^1, ^2>) } } }"
);
});
}
#[test]
fn atc_accounting() {
let program = Arc::new(
parse_and_lower_program(
"
struct Vec<T> { }
trait Iterable {
type Iter<'a>;
}
impl<T> Iterable for Vec<T> {
type Iter<'a> = Iter<'a, T>;
}
struct Iter<'a, T> { }
",
SolverChoice::default()
).unwrap(),
);
tls::set_current_program(&program, || {
let impl_text = format!("{:#?}", &program.impl_data.values().next().unwrap());
println!("{}", impl_text);
assert_eq!(
&impl_text[..],
r#"ImplDatum {
binders: for<type> ImplDatumBound {
trait_ref: Positive(
Vec<^0> as Iterable
),
where_clauses: [],
associated_ty_values: [
AssociatedTyValue {
associated_ty_id: (Iterable::Iter),
value: for<lifetime> AssociatedTyValueBound {
ty: Iter<'^0, ^1>
}
}
],
specialization_priority: 0,
impl_type: Local
}
}"#
);
let goal = parse_and_lower_goal(
&program,
"forall<X> { forall<'a> { forall<Y> { \
X: Iterable<Iter<'a> = Y> } } }",
).unwrap();
let goal_text = format!("{:?}", goal);
println!("{}", goal_text);
assert_eq!(
goal_text,
"ForAll<type> { \
ForAll<lifetime> { \
ForAll<type> { \
(ProjectionEq(<^2 as Iterable>::Iter<'^1> = ^0), \
Implemented(^2: Iterable)) \
} \
} \
}"
);
});
}
#[test]
fn check_parameter_kinds() {
lowering_error! {
program {
struct Foo<'a> { }
struct i32 { }
trait Bar { }
impl Bar for Foo<i32> { }
}
error_msg {
"incorrect parameter kind: expected lifetime, found type"
}
};
lowering_error! {
program {
struct Foo<T> { }
trait Bar { }
impl<'a> Bar for Foo<'a> { }
}
error_msg {
"incorrect parameter kind: expected type, found lifetime"
}
};
lowering_error! {
program {
trait Iterator { type Item<'a>; }
trait Foo { }
impl<X, T> Foo for <X as Iterator>::Item<T> where X: Iterator { }
}
error_msg {
"incorrect kind for associated type parameter: expected lifetime, found type"
}
};
lowering_error! {
program {
trait Iterator { type Item<T>; }
trait Foo { }
impl<X, 'a> Foo for <X as Iterator>::Item<'a> where X: Iterator { }
}
error_msg {
"incorrect kind for associated type parameter: expected type, found lifetime"
}
};
lowering_error! {
program {
trait Into<T> {}
struct Foo {}
impl<'a> Into<'a> for Foo {}
}
error_msg {
"incorrect kind for trait parameter: expected type, found lifetime"
}
}
lowering_error! {
program {
trait IntoTime<'a> {}
struct Foo {}
impl<T> IntoTime<T> for Foo {}
}
error_msg {
"incorrect kind for trait parameter: expected lifetime, found type"
}
}
}
#[test]
fn gat_parse() {
lowering_success! {
program {
trait Sized {}
trait Clone {}
trait Foo {
type Item<'a, T>: Sized + Clone where Self: Sized;
}
trait Bar {
type Item<'a, T> where Self: Sized;
}
struct Container<T> {
value: T
}
trait Baz {
type Item<'a, 'b, T>: Foo<Item<'b, T> = Container<T>> + Clone;
}
trait Quux {
type Item<'a, T>;
}
}
}
lowering_error! {
program {
trait Sized { }
trait Foo {
type Item where K: Sized;
}
}
error_msg {
"invalid type name `K`"
}
}
}
#[test]
fn gat_higher_ranked_bound() {
lowering_success! {
program {
trait Fn<T> {}
trait Ref<'a, T> {}
trait Sized {}
trait Foo {
type Item<T>: forall<'a> Fn<Ref<'a, T>> + Sized;
}
}
}
}
#[test]
fn duplicate_parameters() {
lowering_error! {
program {
trait Foo<T, T> { }
}
error_msg {
"duplicate or shadowed parameters"
}
}
lowering_error! {
program {
trait Foo<T> {
type Item<T>;
}
}
error_msg {
"duplicate or shadowed parameters"
}
}
lowering_error! {
program {
struct fn<'a> { }
struct Foo<'a> {
a: for<'a> fn<'a>
}
} error_msg {
"duplicate or shadowed parameters"
}
}
lowering_error! {
program {
trait Fn<T> {}
trait Ref<'a, T> {}
trait Foo<'a> {
type Item<T>: forall<'a> Fn<Ref<'a, T>>;
}
} error_msg {
"duplicate or shadowed parameters"
}
}
}
#[test]
fn upstream_items() {
lowering_success! {
program {
#[upstream] trait Send { }
#[upstream] struct Vec<T> { }
}
}
}
#[test]
fn deref_trait() {
lowering_success! {
program {
#[lang_deref] trait Deref { type Target; }
}
}
lowering_error! {
program {
#[lang_deref] trait Deref { }
#[lang_deref] trait DerefDupe { }
} error_msg {
"Duplicate lang item `DerefTrait`"
}
}
}
#[test]
fn fundamental_multiple_type_parameters() {
lowering_error! {
program {
#[fundamental]
struct Boxes<T, U> { }
}
error_msg {
"Only fundamental types with a single parameter are supported"
}
}
}