]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/regions-assoc-type-outlives-container.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.git] / src / test / compile-fail / regions-assoc-type-outlives-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 use std::marker::PhantomFn;
19
20 ///////////////////////////////////////////////////////////////////////////
21
22 pub trait TheTrait: PhantomFn<Self, Self> {
23     type TheAssocType;
24 }
25
26 pub struct TheType<'b> {
27     m: [fn(&'b()); 0]
28 }
29
30 impl<'b> TheTrait for TheType<'b> {
31     type TheAssocType = &'b ();
32 }
33
34 ///////////////////////////////////////////////////////////////////////////
35
36 pub struct WithAssoc<T:TheTrait> {
37     m: [T; 0]
38 }
39
40 pub struct WithoutAssoc<T> {
41     m: [T; 0]
42 }
43
44 fn with_assoc<'a,'b>() {
45     // For this type to be valid, the rules require that all
46     // associated types of traits that appear in `WithAssoc` must
47     // outlive 'a. In this case, that means TheType<'b>::TheAssocType,
48     // which is &'b (), must outlive 'a.
49
50     let _: &'a WithAssoc<TheType<'b>> = loop { }; //~ ERROR cannot infer
51 }
52
53 fn with_assoc1<'a,'b>() where 'b : 'a {
54     // For this type to be valid, the rules require that all
55     // associated types of traits that appear in `WithAssoc` must
56     // outlive 'a. In this case, that means TheType<'b>::TheAssocType,
57     // which is &'b (), must outlive 'a, so 'b : 'a must hold, and
58     // that is in the where clauses, so we're fine.
59
60     let _: &'a WithAssoc<TheType<'b>> = loop { };
61 }
62
63 fn without_assoc<'a,'b>() {
64     // Here there are no associated types and the `'b` appearing in
65     // `TheType<'b>` is purely covariant, so there is no requirement
66     // that `'b:'a` holds.
67
68     let _: &'a WithoutAssoc<TheType<'b>> = loop { };
69 }
70
71 fn call_with_assoc<'a,'b>() {
72     // As `with_assoc`, but just checking that we impose the same rule
73     // on the value supplied for the type argument, even when there is
74     // no data.
75
76     call::<&'a WithAssoc<TheType<'b>>>();
77     //~^ ERROR cannot infer
78 }
79
80 fn call_without_assoc<'a,'b>() {
81     // As `without_assoc`, but in a distinct scenario.
82
83     call::<&'a WithoutAssoc<TheType<'b>>>();
84 }
85
86 fn call<T>() { }
87
88 fn main() {
89 }