1 // Test that we are imposing the requirement that every associated
2 // type of a bound that appears in the where clause on a struct must
3 // outlive the location in which the type appears. Issue #22246.
6 #![feature(rustc_attrs)]
12 pub struct TheType<'b> {
16 impl<'b> TheTrait for TheType<'b> {
17 type TheAssocType = &'b ();
20 pub struct WithAssoc<T:TheTrait> {
24 pub struct WithoutAssoc<T> {
28 fn with_assoc<'a,'b>() {
29 // For this type to be valid, the rules require that all
30 // associated types of traits that appear in `WithAssoc` must
31 // outlive 'a. In this case, that means TheType<'b>::TheAssocType,
32 // which is &'b (), must outlive 'a.
34 // FIXME (#54943) NLL doesn't enforce WF condition in unreachable code if
35 // `_x` is changed to `_`
36 let _x: &'a WithAssoc<TheType<'b>> = loop { };
37 //~^ ERROR lifetime may not live long enough
40 fn with_assoc1<'a,'b>() where 'b : 'a {
41 // For this type to be valid, the rules require that all
42 // associated types of traits that appear in `WithAssoc` must
43 // outlive 'a. In this case, that means TheType<'b>::TheAssocType,
44 // which is &'b (), must outlive 'a, so 'b : 'a must hold, and
45 // that is in the where clauses, so we're fine.
47 let _x: &'a WithAssoc<TheType<'b>> = loop { };
50 fn without_assoc<'a,'b>() {
51 // Here there are no associated types but there is a requirement
52 // that `'b:'a` holds because the `'b` appears in `TheType<'b>`.
54 let _x: &'a WithoutAssoc<TheType<'b>> = loop { };
55 //~^ ERROR lifetime may not live long enough
58 fn call_with_assoc<'a,'b>() {
59 // As `with_assoc`, but just checking that we impose the same rule
60 // on the value supplied for the type argument, even when there is
63 call::<&'a WithAssoc<TheType<'b>>>();
64 //~^ ERROR lifetime may not live long enough
67 fn call_without_assoc<'a,'b>() {
68 // As `without_assoc`, but in a distinct scenario.
70 call::<&'a WithoutAssoc<TheType<'b>>>();
71 //~^ ERROR lifetime may not live long enough