]> git.lizzy.rs Git - rust.git/blob - src/test/ui/regions/regions-outlives-projection-container.rs
Auto merge of #93718 - thomcc:used-macho, r=pnkfelix
[rust.git] / src / test / ui / regions / regions-outlives-projection-container.rs
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.
4
5 #![allow(dead_code)]
6 #![feature(rustc_attrs)]
7
8 pub trait TheTrait {
9     type TheAssocType;
10 }
11
12 pub struct TheType<'b> {
13     m: [fn(&'b()); 0]
14 }
15
16 impl<'b> TheTrait for TheType<'b> {
17     type TheAssocType = &'b ();
18 }
19
20 pub struct WithAssoc<T:TheTrait> {
21     m: [T; 0]
22 }
23
24 pub struct WithoutAssoc<T> {
25     m: [T; 0]
26 }
27
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.
33
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
38 }
39
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.
46
47     let _x: &'a WithAssoc<TheType<'b>> = loop { };
48 }
49
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>`.
53
54     let _x: &'a WithoutAssoc<TheType<'b>> = loop { };
55     //~^ ERROR lifetime may not live long enough
56 }
57
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
61     // no data.
62
63     call::<&'a WithAssoc<TheType<'b>>>();
64     //~^ ERROR lifetime may not live long enough
65 }
66
67 fn call_without_assoc<'a,'b>() {
68     // As `without_assoc`, but in a distinct scenario.
69
70     call::<&'a WithoutAssoc<TheType<'b>>>();
71     //~^ ERROR lifetime may not live long enough
72 }
73
74 fn call<T>() { }
75
76 fn main() {
77 }