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 // ignore-compare-mode-nll
7 //[nll] compile-flags: -Z borrowck=mir
10 #![feature(rustc_attrs)]
16 pub struct TheType<'b> {
20 impl<'b> TheTrait for TheType<'b> {
21 type TheAssocType = &'b ();
24 pub struct WithAssoc<T:TheTrait> {
28 pub struct WithoutAssoc<T> {
32 fn with_assoc<'a,'b>() {
33 // For this type to be valid, the rules require that all
34 // associated types of traits that appear in `WithAssoc` must
35 // outlive 'a. In this case, that means TheType<'b>::TheAssocType,
36 // which is &'b (), must outlive 'a.
38 // FIXME (#54943) NLL doesn't enforce WF condition in unreachable code if
39 // `_x` is changed to `_`
40 let _x: &'a WithAssoc<TheType<'b>> = loop { };
41 //[base]~^ ERROR reference has a longer lifetime
42 //[nll]~^^ ERROR lifetime may not live long enough
45 fn with_assoc1<'a,'b>() where 'b : 'a {
46 // For this type to be valid, the rules require that all
47 // associated types of traits that appear in `WithAssoc` must
48 // outlive 'a. In this case, that means TheType<'b>::TheAssocType,
49 // which is &'b (), must outlive 'a, so 'b : 'a must hold, and
50 // that is in the where clauses, so we're fine.
52 let _x: &'a WithAssoc<TheType<'b>> = loop { };
55 fn without_assoc<'a,'b>() {
56 // Here there are no associated types but there is a requirement
57 // that `'b:'a` holds because the `'b` appears in `TheType<'b>`.
59 let _x: &'a WithoutAssoc<TheType<'b>> = loop { };
60 //[base]~^ ERROR reference has a longer lifetime
61 //[nll]~^^ ERROR lifetime may not live long enough
64 fn call_with_assoc<'a,'b>() {
65 // As `with_assoc`, but just checking that we impose the same rule
66 // on the value supplied for the type argument, even when there is
69 call::<&'a WithAssoc<TheType<'b>>>();
70 //[base]~^ ERROR reference has a longer lifetime
71 //[nll]~^^ ERROR lifetime may not live long enough
74 fn call_without_assoc<'a,'b>() {
75 // As `without_assoc`, but in a distinct scenario.
77 call::<&'a WithoutAssoc<TheType<'b>>>();
78 //[base]~^ ERROR reference has a longer lifetime
79 //[nll]~^^ ERROR lifetime may not live long enough