Skip to content

Commit 45d4bb2

Browse files
author
Xinglu Chen
committed
Construct test so that it would fail for old code
1 parent 8b36376 commit 45d4bb2

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/tools/miri/tests/pass/tree_borrows/cell-inside-box.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,28 @@ use std::cell::UnsafeCell;
88

99
pub fn main() {
1010
let cell = UnsafeCell::new(42);
11-
let mut root = Box::new(cell);
11+
let box1 = Box::new(cell);
1212

13-
let a = Box::as_mut_ptr(&mut root);
1413
unsafe {
15-
name!(a);
16-
let alloc_id = alloc_id!(a);
14+
let ptr1: *mut UnsafeCell<i32> = Box::into_raw(box1);
15+
name!(ptr1);
16+
17+
let mut box2 = Box::from_raw(ptr1);
18+
// `ptr2` will be a descendant of `ptr1`.
19+
let ptr2: *mut UnsafeCell<i32> = Box::as_mut_ptr(&mut box2);
20+
name!(ptr2);
21+
22+
// We perform a write through `x`.
23+
// Because `ptr1` is ReservedIM, a child write will make it transition to Active.
24+
// Because `ptr2` is ReservedIM, a foreign write doesn't have any effect on it.
25+
let x = (*ptr1).get();
26+
*x = 1;
27+
28+
// We can still read from `ptr2`.
29+
let val = *(*ptr2).get();
30+
assert_eq!(val, 1);
31+
32+
let alloc_id = alloc_id!(ptr1);
1733
print_state!(alloc_id);
1834
}
1935
}

src/tools/miri/tests/pass/tree_borrows/cell-inside-box.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
Warning: this tree is indicative only. Some tags may have been hidden.
33
0.. 4
44
| Act | └─┬──<TAG=root of the allocation>
5-
| ReIM| └────<TAG=a>
5+
| Act | └─┬──<TAG=ptr1>
6+
| ReIM| └────<TAG=ptr2>
67
──────────────────────────────────────────────────

0 commit comments

Comments
 (0)