Skip to content

Commit 4703770

Browse files
committed
add test comparing free region to bound region
suggested by arielb1
1 parent 86e7b5c commit 4703770

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 a case where we setup relationships like `'x: 'a` or `'a: 'x`,
12+
// where `'x` is bound in closure type but `'a` is free. This forces
13+
// us to approximate `'x` one way or the other.
14+
15+
// compile-flags:-Znll -Zborrowck=mir -Zverbose
16+
17+
#![feature(rustc_attrs)]
18+
19+
use std::cell::Cell;
20+
21+
fn foo<'a, F>(_cell: Cell<&'a u32>, _f: F)
22+
where
23+
F: for<'x> FnOnce(Cell<&'a u32>, Cell<&'x u32>),
24+
{
25+
}
26+
27+
#[rustc_regions]
28+
fn case1() {
29+
let a = 0;
30+
let cell = Cell::new(&a);
31+
foo(cell, |cell_a, cell_x| {
32+
//~^ WARNING not reporting region error due to -Znll
33+
cell_a.set(cell_x.get()); // forces 'x: 'a, error in closure
34+
//~^ ERROR free region `'_#2r` does not outlive free region `'_#1r`
35+
})
36+
}
37+
38+
#[rustc_regions]
39+
fn case2() {
40+
let a = 0;
41+
let cell = Cell::new(&a);
42+
43+
// As you can see in the stderr output, this closure propoagates a
44+
// requirement that `'a: 'static'.
45+
//
46+
// FIXME(#45827) However, because of shortcomings in the MIR type
47+
// checker, this does not result in errors later on (yet).
48+
foo(cell, |cell_a, cell_x| {
49+
cell_x.set(cell_a.get()); // forces 'a: 'x, implies 'a = 'static -> borrow error
50+
})
51+
}
52+
53+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
warning: not reporting region error due to -Znll
2+
--> $DIR/propagate-approximated-shorter-to-static-comparing-against-free.rs:31:5
3+
|
4+
31 | foo(cell, |cell_a, cell_x| {
5+
| ^^^
6+
7+
error: free region `'_#2r` does not outlive free region `'_#1r`
8+
--> $DIR/propagate-approximated-shorter-to-static-comparing-against-free.rs:33:9
9+
|
10+
33 | cell_a.set(cell_x.get()); // forces 'x: 'a, error in closure
11+
| ^^^^^^
12+
13+
note: External requirements
14+
--> $DIR/propagate-approximated-shorter-to-static-comparing-against-free.rs:31:15
15+
|
16+
31 | foo(cell, |cell_a, cell_x| {
17+
| _______________^
18+
32 | | //~^ WARNING not reporting region error due to -Znll
19+
33 | | cell_a.set(cell_x.get()); // forces 'x: 'a, error in closure
20+
34 | | //~^ ERROR free region `'_#2r` does not outlive free region `'_#1r`
21+
35 | | })
22+
| |_____^
23+
|
24+
= note: defining type: DefId(0/1:12 ~ propagate_approximated_shorter_to_static_comparing_against_free[317d]::case1[0]::{{closure}}[0]) with closure substs [
25+
i32,
26+
for<'r> extern "rust-call" fn((std::cell::Cell<&'_#1r u32>, std::cell::Cell<&ReLateBound(DebruijnIndex { depth: 1 }, BrNamed(crate0:DefIndex(0:0), 'r)) u32>))
27+
]
28+
= note: number of external vids: 2
29+
30+
note: No external requirements
31+
--> $DIR/propagate-approximated-shorter-to-static-comparing-against-free.rs:28:1
32+
|
33+
28 | / fn case1() {
34+
29 | | let a = 0;
35+
30 | | let cell = Cell::new(&a);
36+
31 | | foo(cell, |cell_a, cell_x| {
37+
... |
38+
35 | | })
39+
36 | | }
40+
| |_^
41+
|
42+
= note: defining type: DefId(0/0:5 ~ propagate_approximated_shorter_to_static_comparing_against_free[317d]::case1[0]) with substs []
43+
44+
note: External requirements
45+
--> $DIR/propagate-approximated-shorter-to-static-comparing-against-free.rs:48:15
46+
|
47+
48 | foo(cell, |cell_a, cell_x| {
48+
| _______________^
49+
49 | | cell_x.set(cell_a.get()); // forces 'a: 'x, implies 'a = 'static -> borrow error
50+
50 | | })
51+
| |_____^
52+
|
53+
= note: defining type: DefId(0/1:13 ~ propagate_approximated_shorter_to_static_comparing_against_free[317d]::case2[0]::{{closure}}[0]) with closure substs [
54+
i32,
55+
for<'r> extern "rust-call" fn((std::cell::Cell<&'_#1r u32>, std::cell::Cell<&ReLateBound(DebruijnIndex { depth: 1 }, BrNamed(crate0:DefIndex(0:0), 'r)) u32>))
56+
]
57+
= note: number of external vids: 2
58+
= note: where '_#1r: '_#0r
59+
60+
note: No external requirements
61+
--> $DIR/propagate-approximated-shorter-to-static-comparing-against-free.rs:39:1
62+
|
63+
39 | / fn case2() {
64+
40 | | let a = 0;
65+
41 | | let cell = Cell::new(&a);
66+
42 | |
67+
... |
68+
50 | | })
69+
51 | | }
70+
| |_^
71+
|
72+
= note: defining type: DefId(0/0:6 ~ propagate_approximated_shorter_to_static_comparing_against_free[317d]::case2[0]) with substs []
73+
74+
error: aborting due to previous error
75+

0 commit comments

Comments
 (0)