Skip to content

Commit e2b7027

Browse files
authored
Merge pull request rust-lang#4028 from RalfJung/borrow-tests
stacked borrows tests: add those that fail under TB
2 parents 7c7371a + 8ddc896 commit e2b7027

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/tools/miri/tests/fail/tree_borrows/write-during-2phase.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
struct Foo(u64);
1010
impl Foo {
11-
#[rustfmt::skip] // rustfmt is wrong about which line contains an error
12-
fn add(&mut self, n: u64) -> u64 { //~ ERROR: /reborrow through .* is forbidden/
11+
fn add(&mut self, n: u64) -> u64 {
12+
//~^ ERROR: /reborrow through .* is forbidden/
1313
self.0 + n
1414
}
1515
}

src/tools/miri/tests/pass/stacked-borrows/2phase.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// FIXME: this miscompiles with optimizations, see <https://github.com/rust-lang/rust/issues/132898>.
2+
//@compile-flags: -Zmir-opt-level=0
3+
14
trait S: Sized {
25
fn tpb(&mut self, _s: Self) {}
36
}
@@ -75,6 +78,25 @@ fn with_interior_mutability() {
7578
});
7679
}
7780

81+
// This one really shouldn't be accepted, but since we treat 2phase as raw, we do accept it.
82+
// Tree Borrows rejects it.
83+
fn aliasing_violation() {
84+
struct Foo(u64);
85+
impl Foo {
86+
fn add(&mut self, n: u64) -> u64 {
87+
self.0 + n
88+
}
89+
}
90+
91+
let mut f = Foo(0);
92+
let alias = &mut f.0 as *mut u64;
93+
let res = f.add(unsafe {
94+
*alias = 42;
95+
0
96+
});
97+
assert_eq!(res, 42);
98+
}
99+
78100
fn main() {
79101
two_phase1();
80102
two_phase2();
@@ -84,4 +106,5 @@ fn main() {
84106
with_interior_mutability();
85107
two_phase_overlapping1();
86108
two_phase_overlapping2();
109+
aliasing_violation();
87110
}

src/tools/miri/tests/pass/stacked-borrows/stacked-borrows.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ fn main() {
88
mut_raw_then_mut_shr();
99
mut_shr_then_mut_raw();
1010
mut_raw_mut();
11+
mut_raw_mut2();
1112
partially_invalidate_mut();
1213
drop_after_sharing();
1314
// direct_mut_to_const_raw();
@@ -96,6 +97,18 @@ fn mut_raw_mut() {
9697
assert_eq!(x, 4);
9798
}
9899

100+
// A variant of `mut_raw_mut` that does *not* get accepted by Tree Borrows.
101+
// It's kind of an accident that we accept it in Stacked Borrows...
102+
fn mut_raw_mut2() {
103+
unsafe {
104+
let mut root = 0;
105+
let to = &mut root as *mut i32;
106+
*to = 0;
107+
let _val = root;
108+
*to = 0;
109+
}
110+
}
111+
99112
fn partially_invalidate_mut() {
100113
let data = &mut (0u8, 0u8);
101114
let reborrow = &mut *data as *mut (u8, u8);

0 commit comments

Comments
 (0)