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.
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.
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.
17 use std::mem::transmute;
20 ///////////////////////////////////////////////////////////////////////////
28 pub struct TheType<'b> {
32 impl<'b> TheTrait for TheType<'b> {
33 type TheAssocType = &'b ();
36 ///////////////////////////////////////////////////////////////////////////
38 pub struct WithAssoc<T:TheTrait> {
42 pub struct WithoutAssoc<T> {
46 fn with_assoc<'a,'b>() {
47 // For this type to be valid, the rules require that all
48 // associated types of traits that appear in `WithAssoc` must
49 // outlive 'a. In this case, that means TheType<'b>::TheAssocType,
50 // which is &'b (), must outlive 'a.
52 let _: &'a WithAssoc<TheType<'b>> = loop { }; //~ ERROR cannot infer
55 fn with_assoc1<'a,'b>() where 'b : 'a {
56 // For this type to be valid, the rules require that all
57 // associated types of traits that appear in `WithAssoc` must
58 // outlive 'a. In this case, that means TheType<'b>::TheAssocType,
59 // which is &'b (), must outlive 'a, so 'b : 'a must hold, and
60 // that is in the where clauses, so we're fine.
62 let _: &'a WithAssoc<TheType<'b>> = loop { };
65 fn without_assoc<'a,'b>() {
66 // Here there are no associated types and the `'b` appearing in
67 // `TheType<'b>` is purely covariant, so there is no requirement
68 // that `'b:'a` holds.
70 let _: &'a WithoutAssoc<TheType<'b>> = loop { };
73 fn call_with_assoc<'a,'b>() {
74 // As `with_assoc`, but just checking that we impose the same rule
75 // on the value supplied for the type argument, even when there is
78 call::<&'a WithAssoc<TheType<'b>>>();
79 //~^ ERROR cannot infer
82 fn call_without_assoc<'a,'b>() {
83 // As `without_assoc`, but in a distinct scenario.
85 call::<&'a WithoutAssoc<TheType<'b>>>();