diff options
| -rw-r--r-- | src/tools/miri/tests/pass/tree-borrows/read-only-from-mut.rs | 14 | ||||
| -rw-r--r-- | src/tools/miri/tests/pass/tree-borrows/tree-borrows.rs | 41 |
2 files changed, 41 insertions, 14 deletions
diff --git a/src/tools/miri/tests/pass/tree-borrows/read-only-from-mut.rs b/src/tools/miri/tests/pass/tree-borrows/read-only-from-mut.rs deleted file mode 100644 index 4daf06c777e..00000000000 --- a/src/tools/miri/tests/pass/tree-borrows/read-only-from-mut.rs +++ /dev/null @@ -1,14 +0,0 @@ -//@compile-flags: -Zmiri-tree-borrows - -// Tree Borrows has no issue with several mutable references existing -// at the same time, as long as they are used only immutably. -// I.e. multiple Reserved can coexist. -pub fn main() { - unsafe { - let base = &mut 42u64; - let r1 = &mut *(base as *mut u64); - let r2 = &mut *(base as *mut u64); - let _l = *r1; - let _l = *r2; - } -} diff --git a/src/tools/miri/tests/pass/tree-borrows/tree-borrows.rs b/src/tools/miri/tests/pass/tree-borrows/tree-borrows.rs new file mode 100644 index 00000000000..c8fab745242 --- /dev/null +++ b/src/tools/miri/tests/pass/tree-borrows/tree-borrows.rs @@ -0,0 +1,41 @@ +//@compile-flags: -Zmiri-tree-borrows + +use std::mem; + +fn main() { + aliasing_read_only_mutable_refs(); + string_as_mut_ptr(); +} + +// Tree Borrows has no issue with several mutable references existing +// at the same time, as long as they are used only immutably. +// I.e. multiple Reserved can coexist. +pub fn aliasing_read_only_mutable_refs() { + unsafe { + let base = &mut 42u64; + let r1 = &mut *(base as *mut u64); + let r2 = &mut *(base as *mut u64); + let _l = *r1; + let _l = *r2; + } +} + +pub fn string_as_mut_ptr() { + // This errors in Stacked Borrows since as_mut_ptr restricts the provenance, + // but with Tree Borrows it should work. + unsafe { + let mut s = String::from("hello"); + s.reserve(1); // make the `str` that `s` derefs to not cover the entire `s`. + + // Prevent automatically dropping the String's data + let mut s = mem::ManuallyDrop::new(s); + + let ptr = s.as_mut_ptr(); + let len = s.len(); + let capacity = s.capacity(); + + let s = String::from_raw_parts(ptr, len, capacity); + + assert_eq!(String::from("hello"), s); + } +} |
