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
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion reference/src/glossary.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,49 @@
## Glossary

#### Aliasing

(Please note: a full aliasing model for Rust has not yet been constructed, but
at the moment we can give the following definition.)

*Aliasing* is any time one pointer or reference points to a "span" of memory
that overlaps with the span of another pointer or reference. A span of memory is
similar to how a slice works: there's a base byte address as well as a length in
bytes.

Consider the following example:

```rust
fn main() {
let u: u64 = 7_u64;
let r: &u64 = &u;
let s: &[u8] = unsafe {
core::slice::from_raw_parts(&u as *const u64 as *const u8, 8)
};
let (head, tail) = s.split_first().unwrap();
}
```

In this case, both `r` and `s` alias each other, since they both point to all of
the bytes of `u`.

However, `head` and `tail` do not alias each other: `head` points to the first
byte of `u` and `tail` points to the other seven bytes of `u` after it. Also,
both `head` and `tail` alias `s`.

* The span length of `&T`, `&mut T`, `*const T`, or `*mut T` when `T` is
[`Sized`](https://doc.rust-lang.org/core/marker/trait.Sized.html) is
`size_of<T>()`.
* When `T` is not `Sized` the span length is `size_of_val(t)`.

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.

It is also important to know that LLVM IR has a `noalias` attribute that works
somewhat differently from this definition. However, that's considered a low
level detail of a particular Rust implementation. When programming Rust, the
Abstract Rust Machine is intended to operate according to the definition here.

#### Interior mutability

*Interior Mutation* means mutating memory where there also exists a live shared reference pointing to the same memory; or mutating memory through a pointer derived from a shared reference.
Expand Down Expand Up @@ -79,4 +123,4 @@ niche.
* *tag*
* *rvalue*
* *lvalue*
* *representation*
* *representation*