From 2d6b85118d8040890ff69fa0757b15f2b4e0fd88 Mon Sep 17 00:00:00 2001 From: Jacob Greenfield Date: Tue, 28 Jul 2020 19:19:08 -0400 Subject: [PATCH] Clarify `mut` vs non-`mut` statics --- src/items/external-blocks.md | 4 +++- src/items/static-items.md | 14 +++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/items/external-blocks.md b/src/items/external-blocks.md index 7acb5ce3c..6ef902b60 100644 --- a/src/items/external-blocks.md +++ b/src/items/external-blocks.md @@ -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 diff --git a/src/items/static-items.md b/src/items/static-items.md index e97e75903..f2cab577a 100644 --- a/src/items/static-items.md +++ b/src/items/static-items.md @@ -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 @@ -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