File tree 3 files changed +38
-2
lines changed 3 files changed +38
-2
lines changed Original file line number Diff line number Diff line change 8
8
9
9
struct Foo ( u64 ) ;
10
10
impl 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/
13
13
self . 0 + n
14
14
}
15
15
}
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
+
1
4
trait S : Sized {
2
5
fn tpb ( & mut self , _s : Self ) { }
3
6
}
@@ -75,6 +78,25 @@ fn with_interior_mutability() {
75
78
} ) ;
76
79
}
77
80
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
+
78
100
fn main ( ) {
79
101
two_phase1 ( ) ;
80
102
two_phase2 ( ) ;
@@ -84,4 +106,5 @@ fn main() {
84
106
with_interior_mutability ( ) ;
85
107
two_phase_overlapping1 ( ) ;
86
108
two_phase_overlapping2 ( ) ;
109
+ aliasing_violation ( ) ;
87
110
}
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ fn main() {
8
8
mut_raw_then_mut_shr ( ) ;
9
9
mut_shr_then_mut_raw ( ) ;
10
10
mut_raw_mut ( ) ;
11
+ mut_raw_mut2 ( ) ;
11
12
partially_invalidate_mut ( ) ;
12
13
drop_after_sharing ( ) ;
13
14
// direct_mut_to_const_raw();
@@ -96,6 +97,18 @@ fn mut_raw_mut() {
96
97
assert_eq ! ( x, 4 ) ;
97
98
}
98
99
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
+
99
112
fn partially_invalidate_mut ( ) {
100
113
let data = & mut ( 0u8 , 0u8 ) ;
101
114
let reborrow = & mut * data as * mut ( u8 , u8 ) ;
You can’t perform that action at this time.
0 commit comments