]> git.proxmox.com Git - rustc.git/blob - src/test/ui/nll/ty-outlives/projection-one-region-closure.rs
New upstream version 1.27.1+dfsg1
[rustc.git] / src / test / ui / nll / ty-outlives / projection-one-region-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<'b>>::AssocType` to
12 // outlive `'a` and there are no bounds in the trait definition of
13 // `Anything`. This means that the constraint can only be satisfied in two
14 // ways:
15 //
16 // - by ensuring that `T: 'a` and `'b: 'a`, or
17 // - by something in the where clauses.
18 //
19 // As of this writing, the where clause option does not work because
20 // of limitations in our region inferencing system (this is true both
21 // with and without NLL). See `projection_outlives`.
22 //
23 // Ensuring that both `T: 'a` and `'b: 'a` holds does work (`elements_outlive`).
24
25 // compile-flags:-Zborrowck=mir -Zverbose
26
27 #![allow(warnings)]
28 #![feature(rustc_attrs)]
29
30 use std::cell::Cell;
31
32 trait Anything<'a> {
33 type AssocType;
34 }
35
36 fn with_signature<'a, T, F>(cell: Cell<&'a ()>, t: T, op: F)
37 where
38 F: FnOnce(Cell<&'a ()>, T),
39 {
40 op(cell, t)
41 }
42
43 fn require<'a, 'b, T>(_cell: Cell<&'a ()>, _t: T)
44 where
45 T: Anything<'b>,
46 T::AssocType: 'a,
47 {
48 }
49
50 #[rustc_regions]
51 fn no_relationships_late<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
52 where
53 T: Anything<'b>,
54 {
55 with_signature(cell, t, |cell, t| require(cell, t));
56 //~^ WARNING not reporting region error due to nll
57 //~| ERROR the parameter type `T` may not live long enough
58 //~| ERROR does not outlive free region
59 }
60
61 #[rustc_regions]
62 fn no_relationships_early<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
63 where
64 T: Anything<'b>,
65 'a: 'a,
66 {
67 with_signature(cell, t, |cell, t| require(cell, t));
68 //~^ WARNING not reporting region error due to nll
69 //~| ERROR the parameter type `T` may not live long enough
70 //~| ERROR does not outlive free region
71 }
72
73 #[rustc_regions]
74 fn projection_outlives<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
75 where
76 T: Anything<'b>,
77 T::AssocType: 'a,
78 {
79 // This error is unfortunate. This code ought to type-check: we
80 // are projecting `<T as Anything<'b>>::AssocType`, and we know
81 // that this outlives `'a` because of the where-clause. However,
82 // the way the region checker works, we don't register this
83 // outlives obligation, and hence we get an error: this is because
84 // what we see is a projection like `<T as
85 // Anything<'?0>>::AssocType`, and we don't yet know if `?0` will
86 // equal `'b` or not, so we ignore the where-clause. Obviously we
87 // can do better here with a more involved verification step.
88
89 with_signature(cell, t, |cell, t| require(cell, t));
90 //~^ WARNING not reporting region error due to nll
91 //~| ERROR the parameter type `T` may not live long enough
92 //~| ERROR free region `ReEarlyBound(1, 'b)` does not outlive free region `ReEarlyBound(0, 'a)`
93 }
94
95 #[rustc_regions]
96 fn elements_outlive<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
97 where
98 T: Anything<'b>,
99 T: 'a,
100 'b: 'a,
101 {
102 with_signature(cell, t, |cell, t| require(cell, t));
103 }
104
105 fn main() {}