Skip to content

Initial Glossary entry for Aliasing #170

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

Merged
merged 16 commits into from
Jul 25, 2019
Merged

Initial Glossary entry for Aliasing #170

merged 16 commits into from
Jul 25, 2019

Conversation

Lokathor
Copy link
Contributor

@Lokathor Lokathor commented Jul 19, 2019

This is basically the same definition as the Rustonomicon has, but now we're re-stating it here with (I hope) improved clarity.

Of note, it's now written down somewhere visible that ZSTs never alias.

Also I made the whole Glossary alphabetical.

Note: This is not intended to be new guarantees at all, I don't think I wrote anything that can be taken that way, it's just a definition so that people can speak with common terms.

@RalfJung
Copy link
Member

Also I made the whole Glossary alphabetical.

Could you make that a separate PR, for ease of reviewing?

Lokathor added 2 commits July 19, 2019 10:11
other formatting cleanup will be other PRs
@Lokathor
Copy link
Contributor Author

Alright

  • cleanup deferred for now
  • Noted that noalias is an LLVM IR implementation detail that has a different idea of what aliasing is, but that it's a detail of LLVM-Rust, not really a properly of the Rust Abstract Machine.

Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good to me!

Let's see if anyone else has an opinion.

Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. @gnzlbg okay to merge?

Copy link
Contributor

@gnzlbg gnzlbg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Lokathor and others added 5 commits July 25, 2019 02:05
I honestly don't know if this link will work because there isn't a layout.md in the repo, the SUMMARY has always been messed up and mdbook has just been generating a blank page because of it this whole time.
@RalfJung
Copy link
Member

Ah whatever, I think I will just do an editing pass and also define "Tag" while I am at it. ;) Thanks a lot @Lokathor !

@RalfJung RalfJung merged commit 71f8b1c into rust-lang:master Jul 25, 2019
@oberien
Copy link

oberien commented Jul 25, 2019

I think I'm a bit late to this party, but how does the definition here interact with slices of ZSTs (#168)?

A span of memory is similar to how a slice works: there's a base byte address as well as a length in bytes.

One interesting side effect of these rules is that references and pointers to Zero Sized Types never alias each other, because their span length is always 0 bytes.

According to these rules, slices of ZSTs always have a span byte length 0 and thus never alias. But I'd argue that a and b in the following code do alias the same allocation, even though the allocation has a length of 0 bytes:

let foo = [(); 1337];
let a = &foo[..];
let b = &foo[..7];

@RalfJung
Copy link
Member

According to these rules, slices of ZSTs always have a span byte length 0 and thus never alias.

That is correct, and actually explicitly stated further down in this PR.

But I'd argue that a and b in the following code do alias the same allocation, even though the allocation has a length of 0 bytes:

I'd argue you are wrong, then. ;) Also see rust-lang/rust-memory-model#44.
What is the benefit of adding a special case for ZST this way?

@oberien
Copy link

oberien commented Jul 25, 2019

I think my misunderstanding here is that mutable references are highly underdocumented. The only "real" documentation of them I know of is the following part of the nomicon:

There are two kinds of reference:

  • Shared reference: &
  • Mutable reference: &mut

Which obey the following rules:

  • A reference cannot outlive its referent
  • A mutable reference cannot be aliased

That's it. That's the whole model references follow.

Reading through the linked issue, I understand that those are only necessary and not sufficient definitions. I was under the impression that they are sufficient and thus interpreted the noaliasing rule of ZSTs such that you can have multiple mutable references to the same ZST at the same time, which obviously is just wrong. I think I just need to split the meanings of "aliasing" and "references" in my brain.

@RalfJung
Copy link
Member

You can think of there being arbitrarily many ZST "between" any two memory addresses, and references can point to those. So even if two ZST references point to the same location, they don't "overlap".

That pragmatic response is that aliasing is about figuring out if memory operations through one reference can be observed through the other reference, or if they can be freely reordered. But no memory access actually ever happens with a ZST reference.

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