about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2024-10-11 12:38:58 +0200
committerRalf Jung <post@ralfj.de>2024-10-11 12:39:51 +0200
commitccdea3ee363ad579923ad33bf3fb027a78ed46c5 (patch)
tree366acad86fc416d0ccf537374a4085c223aca50f
parent256d63f444d24f62bb239b9d183bb4bdbfe07d62 (diff)
downloadrust-ccdea3ee363ad579923ad33bf3fb027a78ed46c5.tar.gz
rust-ccdea3ee363ad579923ad33bf3fb027a78ed46c5.zip
simplify Tree Borrows write-during-2phase example
-rw-r--r--src/tools/miri/tests/fail/tree_borrows/write-during-2phase.rs13
-rw-r--r--src/tools/miri/tests/fail/tree_borrows/write-during-2phase.stderr15
2 files changed, 15 insertions, 13 deletions
diff --git a/src/tools/miri/tests/fail/tree_borrows/write-during-2phase.rs b/src/tools/miri/tests/fail/tree_borrows/write-during-2phase.rs
index 3f8c9b6219a..a47bb671e32 100644
--- a/src/tools/miri/tests/fail/tree_borrows/write-during-2phase.rs
+++ b/src/tools/miri/tests/fail/tree_borrows/write-during-2phase.rs
@@ -16,12 +16,15 @@ impl Foo {
 
 pub fn main() {
     let mut f = Foo(0);
-    let inner = &mut f.0 as *mut u64;
-    let _res = f.add(unsafe {
-        let n = f.0;
+    let alias = &mut f.0 as *mut u64;
+    let res = f.add(unsafe {
         // This is the access at fault, but it's not immediately apparent because
         // the reference that got invalidated is not under a Protector.
-        *inner = 42;
-        n
+        *alias = 42;
+        0
     });
+    // `res` could be optimized to be `0`, since at the time the reference for the `self` argument
+    // is created, it has value `0`, and then later we add `0` to that. But turns out there is
+    // a sneaky alias that's used to change the value of `*self` before it is read...
+    assert_eq!(res, 42);
 }
diff --git a/src/tools/miri/tests/fail/tree_borrows/write-during-2phase.stderr b/src/tools/miri/tests/fail/tree_borrows/write-during-2phase.stderr
index b85aac7db76..21178dad050 100644
--- a/src/tools/miri/tests/fail/tree_borrows/write-during-2phase.stderr
+++ b/src/tools/miri/tests/fail/tree_borrows/write-during-2phase.stderr
@@ -9,12 +9,12 @@ LL |     fn add(&mut self, n: u64) -> u64 {
 help: the accessed tag <TAG> was created here, in the initial state Reserved
   --> tests/fail/tree_borrows/write-during-2phase.rs:LL:CC
    |
-LL |     let _res = f.add(unsafe {
-   |                ^
+LL |     let res = f.add(unsafe {
+   |               ^
 help: the accessed tag <TAG> later transitioned to Disabled due to a foreign write access at offsets [0x0..0x8]
   --> tests/fail/tree_borrows/write-during-2phase.rs:LL:CC
    |
-LL |         *inner = 42;
+LL |         *alias = 42;
    |         ^^^^^^^^^^^
    = help: this transition corresponds to a loss of read and write permissions
    = note: BACKTRACE (of the first span):
@@ -22,13 +22,12 @@ LL |         *inner = 42;
 note: inside `main`
   --> tests/fail/tree_borrows/write-during-2phase.rs:LL:CC
    |
-LL |       let _res = f.add(unsafe {
-   |  ________________^
-LL | |         let n = f.0;
+LL |       let res = f.add(unsafe {
+   |  _______________^
 LL | |         // This is the access at fault, but it's not immediately apparent because
 LL | |         // the reference that got invalidated is not under a Protector.
-LL | |         *inner = 42;
-LL | |         n
+LL | |         *alias = 42;
+LL | |         0
 LL | |     });
    | |______^