]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/regions-relate-bound-regions-on-closures-to-inference-variables.rs
8eee54b3fec6642288d5ab45729949aac0699610
[rustc.git] / src / test / run-pass / regions-relate-bound-regions-on-closures-to-inference-variables.rs
1 // Copyright 2014 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 that this fairly specialized, but also reasonable, pattern
12 // typechecks. The pattern involves regions bound in closures that
13 // wind up related to inference variables.
14 //
15 // NB. Changes to the region implementations have broken this pattern
16 // a few times, but it happens to be used in the compiler so those
17 // changes were caught. However, those uses in the compiler could
18 // easily get changed or refactored away in the future.
19
20
21 #![allow(unknown_features)]
22 #![feature(box_syntax)]
23
24 struct Ctxt<'tcx> {
25 x: &'tcx Vec<isize>
26 }
27
28 struct Foo<'a,'tcx:'a> {
29 cx: &'a Ctxt<'tcx>,
30 }
31
32 impl<'a,'tcx> Foo<'a,'tcx> {
33 fn bother(&mut self) -> isize {
34 // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
35 self.elaborate_bounds(Box::new(|this| {
36 // (*) Here: type of `this` is `&'f0 Foo<&'f1, '_2>`,
37 // where `'f0` and `'f1` are fresh, free regions that
38 // result from the bound regions on the closure, and `'2`
39 // is a region inference variable created by the call. Due
40 // to the constraints on the type, we find that `'_2 : 'f1
41 // + 'f2` must hold (and can be assumed by the callee).
42 // Region inference has to do some clever stuff to avoid
43 // inferring `'_2` to be `'static` in this case, because
44 // it is created outside the closure but then related to
45 // regions bound by the closure itself. See the
46 // `region_inference.rs` file (and the `givens` field, in
47 // particular) for more details.
48 this.foo()
49 }))
50 }
51
52 fn foo(&mut self) -> isize {
53 22
54 }
55
56 fn elaborate_bounds(
57 &mut self,
58 mut mk_cand: Box<for<'b> FnMut(&mut Foo<'b, 'tcx>) -> isize>)
59 -> isize
60 {
61 mk_cand(self)
62 }
63 }
64
65 fn main() {
66 let v = vec![];
67 let cx = Ctxt { x: &v };
68 let mut foo = Foo { cx: &cx };
69 assert_eq!(foo.bother(), 22); // just so the code is not dead, basically
70 }