Skip to content

Commit f47b990

Browse files
committed
Auto merge of #3964 - RalfJung:write-during-2phase, r=RalfJung
simplify Tree Borrows write-during-2phase example
2 parents 49101c1 + 05b02fa commit f47b990

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

tests/fail/tree_borrows/write-during-2phase.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ impl Foo {
1616

1717
pub fn main() {
1818
let mut f = Foo(0);
19-
let inner = &mut f.0 as *mut u64;
20-
let _res = f.add(unsafe {
21-
let n = f.0;
19+
let alias = &mut f.0 as *mut u64;
20+
let res = f.add(unsafe {
2221
// This is the access at fault, but it's not immediately apparent because
2322
// the reference that got invalidated is not under a Protector.
24-
*inner = 42;
25-
n
23+
*alias = 42;
24+
0
2625
});
26+
// `res` could be optimized to be `0`, since at the time the reference for the `self` argument
27+
// is created, it has value `0`, and then later we add `0` to that. But turns out there is
28+
// a sneaky alias that's used to change the value of `*self` before it is read...
29+
assert_eq!(res, 42);
2730
}

tests/fail/tree_borrows/write-during-2phase.stderr

+7-8
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,25 @@ LL | fn add(&mut self, n: u64) -> u64 {
99
help: the accessed tag <TAG> was created here, in the initial state Reserved
1010
--> tests/fail/tree_borrows/write-during-2phase.rs:LL:CC
1111
|
12-
LL | let _res = f.add(unsafe {
13-
| ^
12+
LL | let res = f.add(unsafe {
13+
| ^
1414
help: the accessed tag <TAG> later transitioned to Disabled due to a foreign write access at offsets [0x0..0x8]
1515
--> tests/fail/tree_borrows/write-during-2phase.rs:LL:CC
1616
|
17-
LL | *inner = 42;
17+
LL | *alias = 42;
1818
| ^^^^^^^^^^^
1919
= help: this transition corresponds to a loss of read and write permissions
2020
= note: BACKTRACE (of the first span):
2121
= note: inside `Foo::add` at tests/fail/tree_borrows/write-during-2phase.rs:LL:CC
2222
note: inside `main`
2323
--> tests/fail/tree_borrows/write-during-2phase.rs:LL:CC
2424
|
25-
LL | let _res = f.add(unsafe {
26-
| ________________^
27-
LL | | let n = f.0;
25+
LL | let res = f.add(unsafe {
26+
| _______________^
2827
LL | | // This is the access at fault, but it's not immediately apparent because
2928
LL | | // the reference that got invalidated is not under a Protector.
30-
LL | | *inner = 42;
31-
LL | | n
29+
LL | | *alias = 42;
30+
LL | | 0
3231
LL | | });
3332
| |______^
3433

0 commit comments

Comments
 (0)