Skip to content

Commit 011ffe8

Browse files
committed
Write tests for GATs
1 parent 383243a commit 011ffe8

File tree

4 files changed

+433
-26
lines changed

4 files changed

+433
-26
lines changed

src/ir/lowering/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ fn gat_parse() {
315315
lowering_success! {
316316
program {
317317
trait Sized {}
318+
trait Clone {}
318319

319320
trait Foo {
320321
type Item<'a, T>: Sized + Clone where Self: Sized;

src/rules/wf/test.rs

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ fn projection_type_in_header() {
217217

218218
// Projection types in an impl header are not assumed to be well-formed,
219219
// an explicit where clause is needed (see below).
220-
impl<T> Bar for <T as Foo>::Value { }
220+
impl<T> Bar for T where <T as Foo>::Value: Bar { }
221221
} error_msg {
222222
"trait impl for \"Bar\" does not meet well-formedness requirements"
223223
}
@@ -231,7 +231,125 @@ fn projection_type_in_header() {
231231

232232
trait Bar { }
233233

234-
impl<T> Bar for <T as Foo>::Value where T: Foo { }
234+
impl<T> Bar for T where T: Foo, <T as Foo>::Value: Bar { }
235+
}
236+
}
237+
}
238+
239+
#[test]
240+
fn bound_in_header_from_env() {
241+
lowering_success! {
242+
program {
243+
trait Foo { }
244+
245+
trait Bar {
246+
type Item: Foo;
247+
}
248+
249+
struct Stuff<T> { }
250+
251+
impl<T> Bar for Stuff<T> where T: Foo {
252+
// Should have FromEnv(T: Foo) here.
253+
type Item = T;
254+
}
255+
}
256+
}
257+
258+
lowering_error! {
259+
program {
260+
trait Foo { }
261+
trait Baz { }
262+
263+
trait Bar {
264+
type Item: Baz;
265+
}
266+
267+
struct Stuff<T> { }
268+
269+
impl<T> Bar for Stuff<T> where T: Foo {
270+
// No T: Baz here.
271+
type Item = T;
272+
}
273+
} error_msg {
274+
"trait impl for \"Bar\" does not meet well-formedness requirements"
275+
}
276+
}
277+
}
278+
279+
#[test]
280+
fn generic_projection_where_clause() {
281+
lowering_success! {
282+
program {
283+
trait PointerFamily { type Pointer<T>; }
284+
285+
struct Cow<T> { }
286+
struct CowFamily { }
287+
impl PointerFamily for CowFamily { type Pointer<T> = Cow<T>; }
288+
289+
struct String { }
290+
struct Foo<P> where P: PointerFamily {
291+
bar: <P as PointerFamily>::Pointer<String>
292+
}
293+
}
294+
}
295+
296+
lowering_error! {
297+
program {
298+
trait Copy { }
299+
trait PointerFamily { type Pointer<T> where T: Copy; }
300+
301+
struct Cow<T> { }
302+
struct CowFamily { }
303+
impl PointerFamily for CowFamily { type Pointer<T> = Cow<T>; }
304+
305+
struct String { }
306+
struct Foo<P> where P: PointerFamily {
307+
// No impl Copy for String, so this will fail.
308+
bar: <P as PointerFamily>::Pointer<String>
309+
}
310+
} error_msg {
311+
"type declaration \"Foo\" does not meet well-formedness requirements"
312+
}
313+
}
314+
}
315+
316+
#[test]
317+
fn generic_projection_bound() {
318+
lowering_success! {
319+
program {
320+
trait Clone { }
321+
trait PointerFamily { type Pointer<T>: Clone where T: Clone; }
322+
323+
struct Cow<T> { }
324+
impl<T> Clone for Cow<T> where T: Clone { }
325+
326+
struct CowFamily { }
327+
328+
// impl is WF due because of:
329+
// - `where T: Clone` clause on PointerFamily::Pointer<T>
330+
// - impl<T> Clone for Cow<T> where T: Clone
331+
impl PointerFamily for CowFamily { type Pointer<T> = Cow<T>; }
332+
333+
struct String { }
334+
impl Clone for String { }
335+
struct Foo<P> where P: PointerFamily {
336+
bar: <P as PointerFamily>::Pointer<String>
337+
}
338+
}
339+
}
340+
341+
lowering_error! {
342+
program {
343+
trait Clone { }
344+
trait PointerFamily { type Pointer<T>: Clone where T: Clone; }
345+
346+
struct Cow<T> { }
347+
struct CowFamily { }
348+
349+
// No impl Clone for Cow<T>, so this will fail.
350+
impl PointerFamily for CowFamily { type Pointer<T> = Cow<T>; }
351+
} error_msg {
352+
"trait impl for \"PointerFamily\" does not meet well-formedness requirements"
235353
}
236354
}
237355
}

0 commit comments

Comments
 (0)