Skip to content

Commit 9c0b89c

Browse files
committed
Auto merge of #1163 - RalfJung:raw-cast, r=RalfJung
Test raw-ptr-cast without reborrow With rust-lang/rust#64588 landed, we can finally test these things adequately. :)
2 parents e7f5c4f + b2c9871 commit 9c0b89c

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Make sure that creating a raw ptr next to a shared ref works
2+
// but the shared ref still gets invalidated when the raw ptr is used for writing.
3+
4+
fn main() { unsafe {
5+
use std::mem;
6+
let x = &mut 0;
7+
let y1: &i32 = mem::transmute(&*x); // launder lifetimes
8+
let y2 = x as *mut _;
9+
let _val = *y2;
10+
let _val = *y1;
11+
*y2 += 1;
12+
let _fail = *y1; //~ ERROR borrow stack
13+
} }

tests/run-pass/stacked-borrows/stacked-borrows.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ fn drop_after_sharing() {
108108

109109
// Make sure that coercing &mut T to *const T produces a writeable pointer.
110110
fn direct_mut_to_const_raw() {
111-
// FIXME: This is currently disabled, waiting on a fix for <https://github.com/rust-lang/rust/issues/56604>
111+
// TODO: This is currently disabled, waiting on a decision on <https://github.com/rust-lang/rust/issues/56604>
112112
/*let x = &mut 0;
113113
let y: *const i32 = x;
114114
unsafe { *(y as *mut i32) = 1; }
@@ -119,26 +119,21 @@ fn direct_mut_to_const_raw() {
119119
// Make sure that we can create two raw pointers from a mutable reference and use them both.
120120
fn two_raw() { unsafe {
121121
let x = &mut 0;
122-
// Given the implicit reborrows, the only reason this currently works is that we
123-
// do not track raw pointers: The creation of `y2` reborrows `x` and thus pops
124-
// `y1` off the stack.
125122
let y1 = x as *mut _;
126123
let y2 = x as *mut _;
127124
*y1 += 2;
128125
*y2 += 1;
129126
} }
130127

131128
// Make sure that creating a *mut does not invalidate existing shared references.
132-
fn shr_and_raw() { /* unsafe {
129+
fn shr_and_raw() { unsafe {
133130
use std::mem;
134-
// FIXME: This is currently disabled because "as *mut _" incurs a reborrow.
135131
let x = &mut 0;
136132
let y1: &i32 = mem::transmute(&*x); // launder lifetimes
137133
let y2 = x as *mut _;
138134
let _val = *y1;
139135
*y2 += 1;
140-
// TODO: Once this works, add compile-fail test that tries to read from y1 again.
141-
} */ }
136+
} }
142137

143138
fn disjoint_mutable_subborrows() {
144139
struct Foo {
@@ -165,5 +160,5 @@ fn disjoint_mutable_subborrows() {
165160
let b = unsafe{ borrow_field_b(ptr) };
166161
b.push(4);
167162
a.push_str(" world");
168-
dbg!(a,b);
163+
eprintln!("{:?} {:?}", a, b);
169164
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
[$DIR/stacked-borrows.rs:168] a = "hello world"
2-
[$DIR/stacked-borrows.rs:168] b = [
3-
0,
4-
1,
5-
2,
6-
4,
7-
]
1+
"hello world" [0, 1, 2, 4]

0 commit comments

Comments
 (0)