Skip to content

Commit 3f490ca

Browse files
committed
convert region-liveness-drop{-,-no-}may-dangle.rs into ui tests
The "match exact bits of CFG" approach was fragile and uninformative.
1 parent cba8256 commit 3f490ca

File tree

5 files changed

+78
-66
lines changed

5 files changed

+78
-66
lines changed

Diff for: src/test/mir-opt/nll/region-liveness-drop-no-may-dangle.rs

-52
This file was deleted.

Diff for: src/test/mir-opt/nll/region-liveness-drop-may-dangle.rs renamed to src/test/ui/nll/drop-may-dangle.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
// in the type of `p` includes the points after `&v[0]` up to (but not
1313
// including) the call to `use_x`. The `else` branch is not included.
1414

15-
// compile-flags:-Znll -Zverbose
16-
// ^^^^^^^^^ force compiler to dump more region information
15+
// compile-flags:-Znll -Zborrowck=mir
16+
// must-compile-successfully
1717

1818
#![allow(warnings)]
1919
#![feature(dropck_eyepatch)]
@@ -23,28 +23,24 @@ fn use_x(_: usize) -> bool { true }
2323

2424
fn main() {
2525
let mut v = [1, 2, 3];
26-
let p: Wrap<& /* R4 */ usize> = Wrap { value: &v[0] };
26+
let p: WrapMayDangle<& /* R4 */ usize> = WrapMayDangle { value: &v[0] };
2727
if true {
28+
// `p` will get dropped at end of this block. However, because of
29+
// the `#[may_dangle]` attribute, we do not need to consider R4
30+
// live after this point.
2831
use_x(*p.value);
2932
} else {
33+
v[0] += 1;
3034
use_x(22);
3135
}
3236

33-
// `p` will get dropped here. However, because of the
34-
// `#[may_dangle]` attribute, we do not need to consider R4 live.
37+
v[0] += 1;
3538
}
3639

37-
struct Wrap<T> {
40+
struct WrapMayDangle<T> {
3841
value: T
3942
}
4043

41-
unsafe impl<#[may_dangle] T> Drop for Wrap<T> {
44+
unsafe impl<#[may_dangle] T> Drop for WrapMayDangle<T> {
4245
fn drop(&mut self) { }
4346
}
44-
45-
// END RUST SOURCE
46-
// START rustc.main.nll.0.mir
47-
// | '_#6r | {bb2[3..=5], bb3[0..=1]}
48-
// ...
49-
// let _2: Wrap<&'_#6r usize>;
50-
// END rustc.main.nll.0.mir

Diff for: src/test/ui/nll/drop-may-dangle.stderr

Whitespace-only changes.

Diff for: src/test/ui/nll/drop-no-may-dangle.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2012-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+
// Basic test for liveness constraints: the region (`R1`) that appears
12+
// in the type of `p` must include everything until `p` is dropped
13+
// because of destructor. (Note that the stderr also identifies this
14+
// destructor in the error message.)
15+
16+
// compile-flags:-Znll -Zborrowck=mir -Znll-dump-cause
17+
18+
#![allow(warnings)]
19+
#![feature(dropck_eyepatch)]
20+
#![feature(generic_param_attrs)]
21+
22+
fn use_x(_: usize) -> bool { true }
23+
24+
fn main() {
25+
let mut v = [1, 2, 3];
26+
let p: WrapMayNotDangle<&usize> = WrapMayNotDangle { value: &v[0] };
27+
if true {
28+
use_x(*p.value);
29+
} else {
30+
use_x(22);
31+
v[0] += 1; //~ ERROR cannot assign to `v[..]` because it is borrowed
32+
}
33+
34+
v[0] += 1; //~ ERROR cannot assign to `v[..]` because it is borrowed
35+
}
36+
37+
struct WrapMayNotDangle<T> {
38+
value: T
39+
}
40+
41+
impl<T> Drop for WrapMayNotDangle<T> {
42+
fn drop(&mut self) { }
43+
}

Diff for: src/test/ui/nll/drop-no-may-dangle.stderr

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0506]: cannot assign to `v[..]` because it is borrowed
2+
--> $DIR/drop-no-may-dangle.rs:31:9
3+
|
4+
26 | let p: WrapMayNotDangle<&usize> = WrapMayNotDangle { value: &v[0] };
5+
| ----- borrow of `v[..]` occurs here
6+
...
7+
31 | v[0] += 1; //~ ERROR cannot assign to `v[..]` because it is borrowed
8+
| ^^^^^^^^^ assignment to borrowed `v[..]` occurs here
9+
...
10+
35 | }
11+
| - borrow later used here, when `p` is dropped
12+
13+
error[E0506]: cannot assign to `v[..]` because it is borrowed
14+
--> $DIR/drop-no-may-dangle.rs:34:5
15+
|
16+
26 | let p: WrapMayNotDangle<&usize> = WrapMayNotDangle { value: &v[0] };
17+
| ----- borrow of `v[..]` occurs here
18+
...
19+
34 | v[0] += 1; //~ ERROR cannot assign to `v[..]` because it is borrowed
20+
| ^^^^^^^^^ assignment to borrowed `v[..]` occurs here
21+
35 | }
22+
| - borrow later used here, when `p` is dropped
23+
24+
error: aborting due to 2 previous errors
25+

0 commit comments

Comments
 (0)