]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/regions/regions-relate-bound-regions-on-closures-to-inference-variables.rs
New upstream version 1.37.0+dfsg1
[rustc.git] / src / test / run-pass / 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 #![feature(box_syntax)]
13
14 struct Ctxt<'tcx> {
15 x: &'tcx Vec<isize>
16 }
17
18 struct Foo<'a,'tcx:'a> {
19 cx: &'a Ctxt<'tcx>,
20 }
21
22 impl<'a,'tcx> Foo<'a,'tcx> {
23 fn bother(&mut self) -> isize {
24 self.elaborate_bounds(Box::new(|this| {
25 // (*) Here: type of `this` is `&'f0 Foo<&'f1, '_2>`,
26 // where `'f0` and `'f1` are fresh, free regions that
27 // result from the bound regions on the closure, and `'2`
28 // is a region inference variable created by the call. Due
29 // to the constraints on the type, we find that `'_2 : 'f1
30 // + 'f2` must hold (and can be assumed by the callee).
31 // Region inference has to do some clever stuff to avoid
32 // inferring `'_2` to be `'static` in this case, because
33 // it is created outside the closure but then related to
34 // regions bound by the closure itself. See the
35 // `region_constraints.rs` file (and the `givens` field, in
36 // particular) for more details.
37 this.foo()
38 }))
39 }
40
41 fn foo(&mut self) -> isize {
42 22
43 }
44
45 fn elaborate_bounds(
46 &mut self,
47 mut mk_cand: Box<dyn for<'b> FnMut(&mut Foo<'b, 'tcx>) -> isize>)
48 -> isize
49 {
50 mk_cand(self)
51 }
52 }
53
54 fn main() {
55 let v = vec![];
56 let cx = Ctxt { x: &v };
57 let mut foo = Foo { cx: &cx };
58 assert_eq!(foo.bother(), 22); // just so the code is not dead, basically
59 }