Skip to content

Commit

Permalink
Clarify mut vs non-mut statics
Browse files Browse the repository at this point in the history
  • Loading branch information
Coder-256 committed Jul 29, 2020
1 parent aa309aa commit 2d6b851
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
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:

* 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

0 comments on commit 2d6b851

Please sign in to comment.