rust-lang / nomicon

The Dark Arts of Advanced and Unsafe Rust Programming
https://doc.rust-lang.org/nomicon/
Apache License 2.0
1.75k stars 256 forks source link

Fix wording on the aliasing section #366

Closed JohnTitor closed 2 years ago

JohnTitor commented 2 years ago

It should've said "the value of a local variable cannot alias things...".

Fixes #362

ehuss commented 2 years ago

I've read this over several times, and I'm not clear on this.

The original says: "the value of a local variable can't be aliased by things that existed before it was declared" which I read to be true. The local variable can't be aliased before it is declared because it doesn't exist, yet. There is nothing to alias. That is:

fn compute(input: &u32, output: &mut u32) {
    // Up to this point, nothing can alias `temp` because `temp` doesn't exist, yet.
    let mut temp = *output;
    if *input > 10 {
        temp = 1;
    }
    if *input > 5 {
        temp *= 2;
    }
    *output = temp;
}

The new version says: "the value of a local variable cannot alias things that existed before it was declared." This doesn't seem correct to me because local variables can be created which alias things before their declaration.

fn f(a: &str) {
    let b = a;  // local variable `b` created an alias of `a`
}

Can you help me understand the proposed correction?

JohnTitor commented 2 years ago

Thanks for pointing out, hmm, I was confused then. So, we should say "We're still relying on alias analysis to assume that input doesn't alias temp" instead, right?

JohnTitor commented 2 years ago

Friendly-ping @ehuss it'd be great if you could take another look :)