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