]> git.lizzy.rs Git - rust.git/blob - src/test/ui/regions/regions-outlives-projection-container.rs
Rollup merge of #97317 - GuillaumeGomez:gui-settings-text-click, r=jsha
[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 // revisions: base nll
6 // ignore-compare-mode-nll
7 //[nll] compile-flags: -Z borrowck=mir
8
9 #![allow(dead_code)]
10 #![feature(rustc_attrs)]
11
12 pub trait TheTrait {
13     type TheAssocType;
14 }
15
16 pub struct TheType<'b> {
17     m: [fn(&'b()); 0]
18 }
19
20 impl<'b> TheTrait for TheType<'b> {
21     type TheAssocType = &'b ();
22 }
23
24 pub struct WithAssoc<T:TheTrait> {
25     m: [T; 0]
26 }
27
28 pub struct WithoutAssoc<T> {
29     m: [T; 0]
30 }
31
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.
37
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
43 }
44
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.
51
52     let _x: &'a WithAssoc<TheType<'b>> = loop { };
53 }
54
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>`.
58
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
62 }
63
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
67     // no data.
68
69     call::<&'a WithAssoc<TheType<'b>>>();
70     //[base]~^ ERROR reference has a longer lifetime
71     //[nll]~^^ ERROR lifetime may not live long enough
72 }
73
74 fn call_without_assoc<'a,'b>() {
75     // As `without_assoc`, but in a distinct scenario.
76
77     call::<&'a WithoutAssoc<TheType<'b>>>();
78     //[base]~^ ERROR reference has a longer lifetime
79     //[nll]~^^ ERROR lifetime may not live long enough
80 }
81
82 fn call<T>() { }
83
84 fn main() {
85 }