]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/regions-relate-bound-regions-on-closures-to-inference-variables.rs
rustdoc: Replace no-pretty-expanded with pretty-expanded
[rust.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 // pretty-expanded FIXME #23616
21
22 #![allow(unknown_features)]
23 #![feature(box_syntax)]
24
25 struct Ctxt<'tcx> {
26     x: &'tcx Vec<int>
27 }
28
29 struct Foo<'a,'tcx:'a> {
30     cx: &'a Ctxt<'tcx>,
31 }
32
33 impl<'a,'tcx> Foo<'a,'tcx> {
34     fn bother(&mut self) -> int {
35         // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
36         self.elaborate_bounds(Box::new(|this| {
37             // (*) Here: type of `this` is `&'f0 Foo<&'f1, '_2>`,
38             // where `'f0` and `'f1` are fresh, free regions that
39             // result from the bound regions on the closure, and `'2`
40             // is a region inference variable created by the call. Due
41             // to the constraints on the type, we find that `'_2 : 'f1
42             // + 'f2` must hold (and can be assumed by the callee).
43             // Region inference has to do some clever stuff to avoid
44             // inferring `'_2` to be `'static` in this case, because
45             // it is created outside the closure but then related to
46             // regions bound by the closure itself. See the
47             // `region_inference.rs` file (and the `givens` field, in
48             // particular) for more details.
49             this.foo()
50         }))
51     }
52
53     fn foo(&mut self) -> int {
54         22
55     }
56
57     fn elaborate_bounds(
58         &mut self,
59         mut mk_cand: Box<for<'b> FnMut(&mut Foo<'b, 'tcx>) -> int>)
60         -> int
61     {
62         mk_cand(self)
63     }
64 }
65
66 fn main() {
67     let v = vec!();
68     let cx = Ctxt { x: &v };
69     let mut foo = Foo { cx: &cx };
70     assert_eq!(foo.bother(), 22); // just so the code is not dead, basically
71 }