-
Notifications
You must be signed in to change notification settings - Fork 279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rewrite references.md #27
Conversation
cc @ubsan |
cc @RalfJung |
src/references.md
Outdated
|
||
Of course, we should probably define what *aliased* means. An aliased-- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems oddly cut off and I had to read it a few times to get what the implied meaning was. It might just be best to say Rust doesn't have an aliasing model but let's talk about what aliasing is.
src/references.md
Outdated
|
||
```rust | ||
fn compute(input: &u32, output: &mut u32) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was a bit confused as to how this worked but it does
src/references.md
Outdated
|
||
```rust | ||
fn compute(input: &u32, output: &mut u32) { | ||
let cached_input = *input; // keep input in a register (probably) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe remove the probably? I think it's implied here that this is an optimization we want made and this isn't a LIR or ASM representation so you can't show the register being kept.
src/references.md
Outdated
if *input > 10 { // true (input == 20) | ||
*output = 1; | ||
} | ||
if *input > 5 { // false (input == 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If *input is 20 how is it now 1?
src/references.md
Outdated
there is no unique path to it. A value with no unique path limits what we can do | ||
with it. | ||
Variables and pointers are generally said to *alias* if they refer to the same | ||
location in memory. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a nit:
It doesn't have to be the same location. Pointer to (unaligned) integers also alias if they overlap. I wouldn't usually say this matters, but the next sentence says this is supposed to be the "broadest definition possible", so well ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah in my mind a pointer points to a region of memory, so those two unaligned pointers have overlapping regions, and a slice overlaps with any pointer-to-an-element.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see that. However, in that case, there is no "single location" in memory that they point to, which the wording in the text somewhat implies -- "refer to the same location" makes it sound like "the location a pointer points to" is a well-defined concept, whereas really it is a set of locations.
src/references.md
Outdated
The key thing to know about aliasing is that writes are the only thing that | ||
really matter. For instance, we have little concern for aliasing in this | ||
modified version of our function, because we've moved the writes to a temporary | ||
variable: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That doesn't sound entirely right to me. Consider the following:
fn foo(x: &i32) -> i32 {
let y = *x;
some_unknown_function();
return y;
}
It should be possible to re-order the load and the function call, without knowing anything about that function (other than it being safe).
Now, one may argue that ultimately, this is about whether the unknown code writes to x
or not. Still, saying that reads "don't really matter" seems to be an overstatement to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea I was trying to get across is that reads only matter insofar as they relate to writes. If there are no writes, we don't care if there are reads.
I've pushed a new commit that addresses the comments, I think. TODO: adjust index to link the new page in |
Thanks, looks good to me! |
This stuff didn't end up being useful. A discussing of aliasing is more useful.
Rebased. I think this is ready to land? |
Sounds good to me! |
This replaces the mostly pointless hand-waiving model with an honest declaration of "no one knows" and a discussion of why aliasing matters.