Skip to content

Commit 1e8eba7

Browse files
authored
Merge pull request #3740 from RalfJung/provenance-example
provenance RFC: add example showing the Rust compiler perform this transformation
2 parents c017755 + 55f8d79 commit 1e8eba7

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

text/3559-rust-has-provenance.md

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ Since `p1_ptr` and `p2_ptr` are equal, assuming "pointers are just integers" (i.
7878
However, from the perspective of alias analysis, we want this program to have UB: looking at `p2` and all pointers to it (which is only `p2_ptr`), we can see that none of them are ever written to, so `p2` will always contain its initial value 42.
7979
Therefore, alias analysis would like to conclude that if this program prints anything, it must print 42, and replace `println!("{}", p2)` by `println!("{}", 42)`.
8080
After this transformation, the program might now print nothing or print 42, even though the original program would never print 42.
81+
The Rust compiler does not perform this transformation on the exact program given above (instead, it optimizes away the entire `if`), but [this variant](https://godbolt.org/z/ce4bjqjbM) does indeed print 42.
82+
8183
Changing program behavior in this way is a violation of the "as-if" rule that governs what the compiler may do.
8284
The only way to make that transformation legal is to say that the given program has UB.
8385
The only way to make the given program have UB, while keeping the alternative program (that writes to `p2_ptr`) allowed, is to say that `p1_ptr` and `p2_ptr` are somehow different, and writing through one of these pointers is *not* like writing through the other.

0 commit comments

Comments
 (0)