]> git.proxmox.com Git - rustc.git/blob - src/test/ui/nll/ty-outlives/projection-two-region-trait-bound-closure.rs
New upstream version 1.27.1+dfsg1
[rustc.git] / src / test / ui / nll / ty-outlives / projection-two-region-trait-bound-closure.rs
1 // Copyright 2016 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 cases where we constrain `<T as Anything<'a, 'b>>::AssocType`
12 // to outlive `'a` and there are two bounds in the trait definition of
13 // `Anything` -- i.e., we know that `AssocType` outlives `'a` and
14 // `'b`. In this case, it's not clear what is the best way to satisfy
15 // the trait bound, and hence we propagate it to the caller as a type
16 // test.
17
18 // compile-flags:-Zborrowck=mir -Zverbose
19
20 #![allow(warnings)]
21 #![feature(rustc_attrs)]
22
23 use std::cell::Cell;
24
25 trait Anything<'a, 'b> {
26 type AssocType: 'a + 'b;
27 }
28
29 fn with_signature<'a, T, F>(cell: Cell<&'a ()>, t: T, op: F)
30 where
31 F: FnOnce(Cell<&'a ()>, T),
32 {
33 op(cell, t)
34 }
35
36 fn require<'a, 'b, 'c, T>(_cell: Cell<&'a ()>, _t: T)
37 where
38 T: Anything<'b, 'c>,
39 T::AssocType: 'a,
40 {
41 }
42
43 #[rustc_regions]
44 fn no_relationships_late<'a, 'b, 'c, T>(cell: Cell<&'a ()>, t: T)
45 where
46 T: Anything<'b, 'c>,
47 {
48 with_signature(cell, t, |cell, t| require(cell, t));
49 //~^ WARNING not reporting region error due to nll
50 //~| ERROR associated type `<T as Anything<'_#5r, '_#6r>>::AssocType` may not live long enough
51 }
52
53 #[rustc_regions]
54 fn no_relationships_early<'a, 'b, 'c, T>(cell: Cell<&'a ()>, t: T)
55 where
56 T: Anything<'b, 'c>,
57 'a: 'a,
58 {
59 with_signature(cell, t, |cell, t| require(cell, t));
60 //~^ WARNING not reporting region error due to nll
61 //~| ERROR associated type `<T as Anything<'_#6r, '_#7r>>::AssocType` may not live long enough
62 }
63
64 #[rustc_regions]
65 fn projection_outlives<'a, 'b, 'c, T>(cell: Cell<&'a ()>, t: T)
66 where
67 T: Anything<'b, 'c>,
68 T::AssocType: 'a,
69 {
70 // This error is unfortunate. This code ought to type-check: we
71 // are projecting `<T as Anything<'b>>::AssocType`, and we know
72 // that this outlives `'a` because of the where-clause. However,
73 // the way the region checker works, we don't register this
74 // outlives obligation, and hence we get an error: this is because
75 // what we see is a projection like `<T as
76 // Anything<'?0>>::AssocType`, and we don't yet know if `?0` will
77 // equal `'b` or not, so we ignore the where-clause. Obviously we
78 // can do better here with a more involved verification step.
79
80 with_signature(cell, t, |cell, t| require(cell, t));
81 //~^ WARNING not reporting region error due to nll
82 //~| ERROR associated type `<T as Anything<'_#6r, '_#7r>>::AssocType` may not live long enough
83 }
84
85 #[rustc_regions]
86 fn elements_outlive1<'a, 'b, 'c, T>(cell: Cell<&'a ()>, t: T)
87 where
88 T: Anything<'b, 'c>,
89 'b: 'a,
90 {
91 with_signature(cell, t, |cell, t| require(cell, t));
92 }
93
94 #[rustc_regions]
95 fn elements_outlive2<'a, 'b, 'c, T>(cell: Cell<&'a ()>, t: T)
96 where
97 T: Anything<'b, 'c>,
98 'c: 'a,
99 {
100 with_signature(cell, t, |cell, t| require(cell, t));
101 }
102
103 #[rustc_regions]
104 fn two_regions<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
105 where
106 T: Anything<'b, 'b>,
107 {
108 with_signature(cell, t, |cell, t| require(cell, t));
109 //~^ WARNING not reporting region error due to nll
110 //~| ERROR does not outlive free region
111 }
112
113 #[rustc_regions]
114 fn two_regions_outlive<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
115 where
116 T: Anything<'b, 'b>,
117 'b: 'a,
118 {
119 with_signature(cell, t, |cell, t| require(cell, t));
120 }
121
122 #[rustc_regions]
123 fn one_region<'a, T>(cell: Cell<&'a ()>, t: T)
124 where
125 T: Anything<'a, 'a>,
126 {
127 // Note that in this case the closure still propagates an external
128 // requirement between two variables in its signature, but the
129 // creator maps both those two region variables to `'a` on its
130 // side.
131 with_signature(cell, t, |cell, t| require(cell, t));
132 }
133
134 fn main() {}