]>
Commit | Line | Data |
---|---|---|
ff7c6d11 XL |
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 | // Test closure that: | |
12 | // | |
13 | // - takes an argument `y` | |
14 | // - stores `y` into another, longer-lived spot | |
15 | // | |
16 | // but is invoked with a spot that doesn't live long | |
17 | // enough to store `y`. | |
18 | // | |
19 | // The error is reported in the caller: invoking the closure links the | |
20 | // lifetime of the variable that is given as `y` (via subtyping) and | |
21 | // thus forces the corresponding borrow to live too long. This is | |
22 | // basically checking that the MIR type checker correctly enforces the | |
23 | // closure signature. | |
24 | ||
0531ce1d | 25 | // compile-flags:-Znll -Zborrowck=mir -Zverbose |
ff7c6d11 XL |
26 | |
27 | #![feature(rustc_attrs)] | |
28 | ||
29 | #[rustc_regions] | |
30 | fn test() { | |
31 | let x = 44; | |
32 | let mut p = &x; | |
33 | ||
34 | { | |
35 | let y = 22; | |
36 | let mut closure = expect_sig(|p, y| *p = y); | |
37 | closure(&mut p, &y); | |
38 | //~^ ERROR `y` does not live long enough [E0597] | |
39 | } | |
40 | ||
41 | deref(p); | |
42 | } | |
43 | ||
44 | fn expect_sig<F>(f: F) -> F | |
45 | where F: for<'a, 'b> FnMut(&'a mut &'b i32, &'b i32) | |
46 | { | |
47 | f | |
48 | } | |
49 | ||
50 | fn deref(_p: &i32) { } | |
51 | ||
52 | fn main() { } |