Skip to content

Commit 2d6b851

Browse files
committed
Clarify mut vs non-mut statics
1 parent aa309aa commit 2d6b851

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/items/external-blocks.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ of initializing the static.
6666

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

7173
## ABI
7274

src/items/static-items.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
A *static item* is similar to a [constant], except that it represents a precise
99
memory location in the program. All references to the static refer to the same
1010
memory location. Static items have the `static` lifetime, which outlives all
11-
other lifetimes in a Rust program. Non-`mut` static items that contain a type
12-
that is not [interior mutable] may be placed in read-only memory. Static items
13-
do not call [`drop`] at the end of the program.
11+
other lifetimes in a Rust program. Static items do not call [`drop`] at the end
12+
of the program. Note that [external statics] have additional restrictions.
1413

15-
All access to a static is safe, but there are a number of restrictions on
16-
statics:
14+
## Non-`mut` statics
15+
16+
Non-`mut` static items that contain a type
17+
that is not [interior mutable] may be placed in read-only memory.
18+
19+
All access to a non-`mut` static is safe, but there are a number of restrictions:
1720

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

0 commit comments

Comments
 (0)