]> git.lizzy.rs Git - rust.git/blob - src/test/ui/regions/regions-outlives-projection-container.rs
Check user types are well-formed in MIR borrow check
[rust.git] / src / test / ui / regions / regions-outlives-projection-container.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Test that we are imposing the requirement that every associated
12 // type of a bound that appears in the where clause on a struct must
13 // outlive the location in which the type appears. Issue #22246.
14
15 #![allow(dead_code)]
16 #![feature(rustc_attrs)]
17
18 ///////////////////////////////////////////////////////////////////////////
19
20 pub trait TheTrait {
21     type TheAssocType;
22 }
23
24 pub struct TheType<'b> {
25     m: [fn(&'b()); 0]
26 }
27
28 impl<'b> TheTrait for TheType<'b> {
29     type TheAssocType = &'b ();
30 }
31
32 ///////////////////////////////////////////////////////////////////////////
33
34 pub struct WithAssoc<T:TheTrait> {
35     m: [T; 0]
36 }
37
38 pub struct WithoutAssoc<T> {
39     m: [T; 0]
40 }
41
42 fn with_assoc<'a,'b>() {
43     // For this type to be valid, the rules require that all
44     // associated types of traits that appear in `WithAssoc` must
45     // outlive 'a. In this case, that means TheType<'b>::TheAssocType,
46     // which is &'b (), must outlive 'a.
47
48     // FIXME (#54943) NLL doesn't enforce WF condition in unreachable code if
49     // `_x` is changed to `_`
50     let _x: &'a WithAssoc<TheType<'b>> = loop { };
51     //~^ ERROR reference has a longer lifetime
52 }
53
54 fn with_assoc1<'a,'b>() where 'b : 'a {
55     // For this type to be valid, the rules require that all
56     // associated types of traits that appear in `WithAssoc` must
57     // outlive 'a. In this case, that means TheType<'b>::TheAssocType,
58     // which is &'b (), must outlive 'a, so 'b : 'a must hold, and
59     // that is in the where clauses, so we're fine.
60
61     let _x: &'a WithAssoc<TheType<'b>> = loop { };
62 }
63
64 fn without_assoc<'a,'b>() {
65     // Here there are no associated types but there is a requirement
66     // that `'b:'a` holds because the `'b` appears in `TheType<'b>`.
67
68     let _x: &'a WithoutAssoc<TheType<'b>> = loop { };
69     //~^ ERROR reference has a longer lifetime
70 }
71
72 fn call_with_assoc<'a,'b>() {
73     // As `with_assoc`, but just checking that we impose the same rule
74     // on the value supplied for the type argument, even when there is
75     // no data.
76
77     call::<&'a WithAssoc<TheType<'b>>>();
78     //~^ ERROR reference has a longer lifetime
79 }
80
81 fn call_without_assoc<'a,'b>() {
82     // As `without_assoc`, but in a distinct scenario.
83
84     call::<&'a WithoutAssoc<TheType<'b>>>(); //~ ERROR reference has a longer lifetime
85 }
86
87 fn call<T>() { }
88
89 fn main() {
90 }