]> git.proxmox.com Git - rustc.git/blob - src/test/ui/nll/closure-requirements/propagate-approximated-ref.rs
New upstream version 1.27.1+dfsg1
[rustc.git] / src / test / ui / nll / closure-requirements / propagate-approximated-ref.rs
1 // Copyright 2016 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 // Rather convoluted setup where we infer a relationship between two
12 // free regions in the closure signature (`'a` and `'b`) on the basis
13 // of a relationship between two bound regions (`'x` and `'y`).
14 //
15 // The idea is that, thanks to invoking `demand_y`, `'x: 'y` must
16 // hold, where `'x` and `'y` are bound regions. The closure can't
17 // prove that directly, and because `'x` and `'y` are bound it cannot
18 // ask the caller to prove it either. But it has bounds on `'x` and
19 // `'y` in terms of `'a` and `'b`, and it can propagate a relationship
20 // between `'a` and `'b` to the caller.
21 //
22 // Note: the use of `Cell` here is to introduce invariance. One less
23 // variable.
24
25 // compile-flags:-Zborrowck=mir -Zverbose
26
27 #![feature(rustc_attrs)]
28
29 use std::cell::Cell;
30
31 // Callee knows that:
32 //
33 // 'x: 'a
34 // 'b: 'y
35 //
36 // so if we are going to ensure that `'x: 'y`, then `'a: 'b` must
37 // hold.
38 fn establish_relationships<'a, 'b, F>(_cell_a: &Cell<&'a u32>, _cell_b: &Cell<&'b u32>, _closure: F)
39 where
40 F: for<'x, 'y> FnMut(
41 &Cell<&'a &'x u32>, // shows that 'x: 'a
42 &Cell<&'y &'b u32>, // shows that 'b: 'y
43 &Cell<&'x u32>,
44 &Cell<&'y u32>,
45 ),
46 {
47 }
48
49 fn demand_y<'x, 'y>(_cell_x: &Cell<&'x u32>, _cell_y: &Cell<&'y u32>, _y: &'y u32) {}
50
51 #[rustc_regions]
52 fn supply<'a, 'b>(cell_a: Cell<&'a u32>, cell_b: Cell<&'b u32>) {
53 establish_relationships(&cell_a, &cell_b, |_outlives1, _outlives2, x, y| {
54 //~^ ERROR lifetime mismatch
55
56 // Only works if 'x: 'y:
57 demand_y(x, y, x.get()) //~ WARNING not reporting region error due to nll
58 });
59 }
60
61 fn main() {}