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

Clarify rules about immutable statics #860

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion src/items/external-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ of initializing the static.

Extern statics can be either immutable or mutable just like [statics] outside of external blocks.
An immutable static *must* be initialized before any Rust code is executed. It is not enough for
the static to be initialized before Rust code reads from it.
the static to be initialized before Rust code reads from it. Mutating a non-`mut`,
non-interior-`mut` external static is undefined behavior, even if that mutation happens by non-Rust
code.

## ABI

Expand Down
14 changes: 9 additions & 5 deletions src/items/static-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
A *static item* is similar to a [constant], except that it represents a precise
memory location in the program. All references to the static refer to the same
memory location. Static items have the `static` lifetime, which outlives all
other lifetimes in a Rust program. Non-`mut` static items that contain a type
that is not [interior mutable] may be placed in read-only memory. Static items
do not call [`drop`] at the end of the program.
other lifetimes in a Rust program. Static items do not call [`drop`] at the end
of the program. Note that [external statics] have additional restrictions.

All access to a static is safe, but there are a number of restrictions on
statics:
## Non-`mut` statics

Non-`mut` static items that contain a type
that is not [interior mutable] may be placed in read-only memory.

All access to a non-`mut` static is safe, but there are a number of restrictions:
Copy link
Contributor

Choose a reason for hiding this comment

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

The rules below here seem to apply to both mut and non-mut static, right?

Note that I removed the second item in #867, as it is out-of-date.


* The type must have the `Sync` trait bound to allow thread-safe access.
* Statics allow using paths to statics in the [constant expression] used to
Expand Down Expand Up @@ -71,6 +74,7 @@ following are true:
[constant]: constant-items.md
[`drop`]: ../destructors.md
[constant expression]: ../const_eval.md#constant-expressions
[external statics]: external-blocks.md#statics
[interior mutable]: ../interior-mutability.md
[IDENTIFIER]: ../identifiers.md
[_Type_]: ../types.md#type-expressions
Expand Down