]> git.lizzy.rs Git - rust.git/blob - tests/ui/nll/ty-outlives/projection-one-region-trait-bound-static-closure.rs
Auto merge of #101138 - Rejyr:diagnostic-migration-rustc-lint-pt2, r=davidtwco
[rust.git] / tests / ui / nll / ty-outlives / projection-one-region-trait-bound-static-closure.rs
1 // Test cases where we constrain `<T as Anything<'b>>::AssocType` to
2 // outlive `'static`. In this case, we don't get any errors, and in fact
3 // we don't even propagate constraints from the closures to the callers.
4
5 // compile-flags:-Zverbose
6 // check-pass
7
8 #![allow(warnings)]
9 #![feature(rustc_attrs)]
10
11 use std::cell::Cell;
12
13 trait Anything<'a> {
14     type AssocType: 'static;
15 }
16
17 fn with_signature<'a, T, F>(cell: Cell<&'a ()>, t: T, op: F)
18 where
19     F: FnOnce(Cell<&'a ()>, T),
20 {
21     op(cell, t)
22 }
23
24 fn require<'a, 'b, T>(_cell: Cell<&'a ()>, _t: T)
25 where
26     T: Anything<'b>,
27     T::AssocType: 'a,
28 {
29 }
30
31 #[rustc_regions]
32 fn no_relationships_late<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
33 where
34     T: Anything<'b>,
35 {
36     with_signature(cell, t, |cell, t| require(cell, t));
37 }
38
39 #[rustc_regions]
40 fn no_relationships_early<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
41 where
42     T: Anything<'b>,
43     'a: 'a,
44 {
45     with_signature(cell, t, |cell, t| require(cell, t));
46 }
47
48 #[rustc_regions]
49 fn projection_outlives<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
50 where
51     T: Anything<'b>,
52     T::AssocType: 'a,
53 {
54     // This error is unfortunate. This code ought to type-check: we
55     // are projecting `<T as Anything<'b>>::AssocType`, and we know
56     // that this outlives `'a` because of the where-clause. However,
57     // the way the region checker works, we don't register this
58     // outlives obligation, and hence we get an error: this is because
59     // what we see is a projection like `<T as
60     // Anything<'?0>>::AssocType`, and we don't yet know if `?0` will
61     // equal `'b` or not, so we ignore the where-clause. Obviously we
62     // can do better here with a more involved verification step.
63
64     with_signature(cell, t, |cell, t| require(cell, t));
65 }
66
67 #[rustc_regions]
68 fn elements_outlive<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
69 where
70     T: Anything<'b>,
71     'b: 'a,
72 {
73     with_signature(cell, t, |cell, t| require(cell, t));
74 }
75
76 #[rustc_regions]
77 fn one_region<'a, T>(cell: Cell<&'a ()>, t: T)
78 where
79     T: Anything<'a>,
80 {
81     // Note that in this case the closure still propagates an external
82     // requirement between two variables in its signature, but the
83     // creator maps both those two region variables to `'a` on its
84     // side.
85     with_signature(cell, t, |cell, t| require(cell, t));
86 }
87
88 fn main() {}