File tree Expand file tree Collapse file tree 2 files changed +41
-14
lines changed
src/tools/miri/tests/pass/tree-borrows Expand file tree Collapse file tree 2 files changed +41
-14
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments