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

update aliasing rules section of the reference #1290

Merged
merged 5 commits into from
Nov 25, 2022
Merged
Changes from 2 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
11 changes: 9 additions & 2 deletions src/behavior-considered-undefined.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ code.
* Evaluating a [dereference expression] (`*expr`) on a raw pointer that is
[dangling] or unaligned, even in [place expression context]
(e.g. `addr_of!(&*expr)`).
* Breaking the [pointer aliasing rules]. `&mut T` and `&T` follow LLVM’s scoped
[noalias] model, except if the `&T` contains an [`UnsafeCell<U>`].
* Breaking the [pointer aliasing rules]. `Box<T>`, `&mut T` and `&T` follow LLVM’s
scoped [noalias] model, except if the `&T` contains an [`UnsafeCell<U>`].
References must not be dangling while they are live. (The exact liveness
duration is not specified, but it is certainly upper-bounded by the syntactic
lifetime assigned by the borrow checker. When a reference is passed to a
function, it is live at least as long as that function call, again except if
Copy link

Choose a reason for hiding this comment

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

... Does this apply to Box? (The intent is obviously no, but what about for what we're telling LLVM?)

Because it's obviously safe to deallocate a Box allocation within a function,
fn example(x: Box<Data>) {
    drop(x);
    // here -- is `x` still considered `dereferencable`?
}

does this mean that Box needs to be marked with the hypothetical LLVM-dereferenceable_on_entry instead of just LLVM-dereferenceable? I suspect this is a nonissue in practice (memory allocation is either a very side-effectful built-in or an opaque environment call preventing code motion over it) but it's definitely relevant.

Copy link
Member Author

@RalfJung RalfJung Oct 31, 2022

Choose a reason for hiding this comment

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

See rust-lang/rust#66600 (linked in the PR description) :)

But I will clarify the text, thanks!

the `&T` contains an [`UnsafeCell<U>`].) All this also applies when values of
these types are passed in a (nested) field of a compound type, but not behind
pointer indirections.
* Mutating immutable data. All data inside a [`const`] item is immutable. Moreover, all
data reached through a shared reference or data owned by an immutable binding
is immutable, unless that data is contained within an [`UnsafeCell<U>`].
Expand Down