Skip to content

Commit bd5c1d4

Browse files
committed
tree borrows: test String::as_mut_ptr
1 parent 3e8900b commit bd5c1d4

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

src/tools/miri/tests/pass/tree-borrows/read-only-from-mut.rs

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//@compile-flags: -Zmiri-tree-borrows
2+
3+
use std::mem;
4+
5+
fn main() {
6+
aliasing_read_only_mutable_refs();
7+
string_as_mut_ptr();
8+
}
9+
10+
// Tree Borrows has no issue with several mutable references existing
11+
// at the same time, as long as they are used only immutably.
12+
// I.e. multiple Reserved can coexist.
13+
pub fn aliasing_read_only_mutable_refs() {
14+
unsafe {
15+
let base = &mut 42u64;
16+
let r1 = &mut *(base as *mut u64);
17+
let r2 = &mut *(base as *mut u64);
18+
let _l = *r1;
19+
let _l = *r2;
20+
}
21+
}
22+
23+
pub fn string_as_mut_ptr() {
24+
// This errors in Stacked Borrows since as_mut_ptr restricts the provenance,
25+
// but with Tree Borrows it should work.
26+
unsafe {
27+
let mut s = String::from("hello");
28+
s.reserve(1); // make the `str` that `s` derefs to not cover the entire `s`.
29+
30+
// Prevent automatically dropping the String's data
31+
let mut s = mem::ManuallyDrop::new(s);
32+
33+
let ptr = s.as_mut_ptr();
34+
let len = s.len();
35+
let capacity = s.capacity();
36+
37+
let s = String::from_raw_parts(ptr, len, capacity);
38+
39+
assert_eq!(String::from("hello"), s);
40+
}
41+
}

0 commit comments

Comments
 (0)