]> git.lizzy.rs Git - rust.git/blob - tests/ui/regions/regions-relate-bound-regions-on-closures-to-inference-variables.rs
internally change regions to be covariant
[rust.git] / tests / ui / regions / regions-relate-bound-regions-on-closures-to-inference-variables.rs
1 // run-pass
2 #![allow(dead_code)]
3 // Test that this fairly specialized, but also reasonable, pattern
4 // typechecks. The pattern involves regions bound in closures that
5 // wind up related to inference variables.
6 //
7 // NB. Changes to the region implementations have broken this pattern
8 // a few times, but it happens to be used in the compiler so those
9 // changes were caught. However, those uses in the compiler could
10 // easily get changed or refactored away in the future.
11
12 struct Ctxt<'tcx> {
13     x: &'tcx Vec<isize>
14 }
15
16 struct Foo<'a,'tcx:'a> {
17     cx: &'a Ctxt<'tcx>,
18 }
19
20 impl<'a,'tcx> Foo<'a,'tcx> {
21     fn bother(&mut self) -> isize {
22         self.elaborate_bounds(Box::new(|this| {
23             // (*) Here: type of `this` is `&'f0 Foo<&'f1, '_2>`,
24             // where `'f0` and `'f1` are fresh, free regions that
25             // result from the bound regions on the closure, and `'2`
26             // is a region inference variable created by the call. Due
27             // to the constraints on the type, we find that `'_2 : 'f1
28             // + 'f2` must hold (and can be assumed by the callee).
29             // Region inference has to do some clever stuff to avoid
30             // inferring `'_2` to be `'static` in this case, because
31             // it is created outside the closure but then related to
32             // regions bound by the closure itself. See the
33             // `region_constraints.rs` file (and the `givens` field, in
34             // particular) for more details.
35             this.foo()
36         }))
37     }
38
39     fn foo(&mut self) -> isize {
40         22
41     }
42
43     fn elaborate_bounds(
44         &mut self,
45         mut mk_cand: Box<dyn for<'b> FnMut(&mut Foo<'b, 'tcx>) -> isize>)
46         -> isize
47     {
48         mk_cand(self)
49     }
50 }
51
52 fn main() {
53     let v = vec![];
54     let cx = Ctxt { x: &v };
55     let mut foo = Foo { cx: &cx };
56     assert_eq!(foo.bother(), 22); // just so the code is not dead, basically
57 }