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