File tree Expand file tree Collapse file tree 3 files changed +38
-2
lines changed Expand file tree Collapse file tree 3 files changed +38
-2
lines changed Original file line number Diff line number Diff line change 88
99struct Foo ( u64 ) ;
1010impl Foo {
11- # [ rustfmt :: skip ] // rustfmt is wrong about which line contains an error
12- fn add ( & mut self , n : u64 ) -> u64 { //~ ERROR: /reborrow through .* is forbidden/
11+ fn add ( & mut self , n : u64 ) -> u64 {
12+ //~^ ERROR: /reborrow through .* is forbidden/
1313 self . 0 + n
1414 }
1515}
Original file line number Diff line number Diff line change 1+ // FIXME: this miscompiles with optimizations, see <https://github.com/rust-lang/rust/issues/132898>.
2+ //@compile-flags: -Zmir-opt-level=0
3+
14trait S : Sized {
25 fn tpb ( & mut self , _s : Self ) { }
36}
@@ -75,6 +78,25 @@ fn with_interior_mutability() {
7578 } ) ;
7679}
7780
81+ // This one really shouldn't be accepted, but since we treat 2phase as raw, we do accept it.
82+ // Tree Borrows rejects it.
83+ fn aliasing_violation ( ) {
84+ struct Foo ( u64 ) ;
85+ impl Foo {
86+ fn add ( & mut self , n : u64 ) -> u64 {
87+ self . 0 + n
88+ }
89+ }
90+
91+ let mut f = Foo ( 0 ) ;
92+ let alias = & mut f. 0 as * mut u64 ;
93+ let res = f. add ( unsafe {
94+ * alias = 42 ;
95+ 0
96+ } ) ;
97+ assert_eq ! ( res, 42 ) ;
98+ }
99+
78100fn main ( ) {
79101 two_phase1 ( ) ;
80102 two_phase2 ( ) ;
@@ -84,4 +106,5 @@ fn main() {
84106 with_interior_mutability ( ) ;
85107 two_phase_overlapping1 ( ) ;
86108 two_phase_overlapping2 ( ) ;
109+ aliasing_violation ( ) ;
87110}
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ fn main() {
88 mut_raw_then_mut_shr ( ) ;
99 mut_shr_then_mut_raw ( ) ;
1010 mut_raw_mut ( ) ;
11+ mut_raw_mut2 ( ) ;
1112 partially_invalidate_mut ( ) ;
1213 drop_after_sharing ( ) ;
1314 // direct_mut_to_const_raw();
@@ -96,6 +97,18 @@ fn mut_raw_mut() {
9697 assert_eq ! ( x, 4 ) ;
9798}
9899
100+ // A variant of `mut_raw_mut` that does *not* get accepted by Tree Borrows.
101+ // It's kind of an accident that we accept it in Stacked Borrows...
102+ fn mut_raw_mut2 ( ) {
103+ unsafe {
104+ let mut root = 0 ;
105+ let to = & mut root as * mut i32 ;
106+ * to = 0 ;
107+ let _val = root;
108+ * to = 0 ;
109+ }
110+ }
111+
99112fn partially_invalidate_mut ( ) {
100113 let data = & mut ( 0u8 , 0u8 ) ;
101114 let reborrow = & mut * data as * mut ( u8 , u8 ) ;
You can’t perform that action at this time.
0 commit comments