Skip to content
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

Merged
merged 1 commit into from
Jun 12, 2017
Merged

rewrite references.md #27

merged 1 commit into from
Jun 12, 2017

Conversation

Gankra
Copy link
Contributor

@Gankra Gankra commented May 30, 2017

This replaces the mostly pointless hand-waiving model with an honest declaration of "no one knows" and a discussion of why aliasing matters.

@Gankra
Copy link
Contributor Author

Gankra commented May 30, 2017

cc @ubsan

@Gankra
Copy link
Contributor Author

Gankra commented May 30, 2017

Rendered

@aturon
Copy link
Member

aturon commented May 30, 2017

cc @RalfJung


Of course, we should probably define what *aliased* means. An aliased--

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.


```rust
fn compute(input: &u32, output: &mut u32) {

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


```rust
fn compute(input: &u32, output: &mut u32) {
let cached_input = *input; // keep input in a register (probably)

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.

if *input > 10 { // true (input == 20)
*output = 1;
}
if *input > 5 { // false (input == 1)

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?

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.
Copy link
Member

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 ;)

Copy link
Contributor Author

@Gankra Gankra Jun 8, 2017

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.

Copy link
Member

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.

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:
Copy link
Member

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.

Copy link
Contributor Author

@Gankra Gankra Jun 8, 2017

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.

@Gankra
Copy link
Contributor Author

Gankra commented Jun 8, 2017

I've pushed a new commit that addresses the comments, I think.

TODO: adjust index to link the new page in

@Gankra
Copy link
Contributor Author

Gankra commented Jun 8, 2017

@RalfJung
Copy link
Member

RalfJung commented Jun 8, 2017

Thanks, looks good to me!

This stuff didn't end up being useful. A discussing of aliasing is more
useful.
@Gankra
Copy link
Contributor Author

Gankra commented Jun 11, 2017

Rebased. I think this is ready to land?

@steveklabnik steveklabnik merged commit 794c2d6 into rust-lang:master Jun 12, 2017
@steveklabnik
Copy link
Member

Sounds good to me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants