Skip to content

Commit 86e6f2e

Browse files
const expression can borrow static items
1 parent 23ce619 commit 86e6f2e

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

src/const_eval.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ r[const-eval.const-expr.path-item]
4242
Recursively defining constants is not allowed.
4343

4444
r[const-eval.const-expr.path-static]
45-
* Paths to [statics]. These are only allowed within the initializer of a static.
45+
* Paths to [statics] with these restrictions and observations.
46+
* In particular, reads and writes to any `static`, `static mut` or [`extern` statics] is not allowed.
47+
* Immutable borrows and pointers into immutable part of a `static` are allowed and observes the same restriction
48+
on all other forms of [borrow]s as mentioned below.
4649

4750
r[const-eval.const-expr.tuple]
4851
* [Tuple expressions].
@@ -177,6 +180,7 @@ of whether you are building on a `64` bit or a `32` bit system.
177180
[enum discriminants]: items/enumerations.md#discriminants
178181
[expression statements]: statements.md#expression-statements
179182
[expressions]: expressions.md
183+
[`extern` statics]: items/external-blocks.md#statics
180184
[field]: expressions/field-expr.md
181185
[functions]: items/functions.md
182186
[grouped]: expressions/grouped-expr.md

src/items/constant-items.md

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const BITS_N_STRINGS: BitsNStrings<'static> = BitsNStrings {
3939
};
4040
```
4141

42+
The final value of a `const` item cannot contain references to anything mutable.
43+
4244
The constant expression may only be omitted in a [trait definition].
4345

4446
## Constants with Destructors

src/items/static-items.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ Static initializers may refer to other statics.
2222
Non-`mut` static items that contain a type that is not [interior mutable] may
2323
be placed in read-only memory.
2424

25-
All access to a static is safe, but there are a number of restrictions on
26-
statics:
27-
28-
* The type must have the `Sync` trait bound to allow thread-safe access.
29-
* Constants cannot refer to statics.
25+
The type of an immutable static must implement the [`Sync`](std::marker::Sync) trait.
3026

3127
The initializer expression must be omitted in an [external block], and must be
3228
provided for free static items.
@@ -131,7 +127,7 @@ It can be confusing whether or not you should use a constant item or a static
131127
item. Constants should, in general, be preferred over statics unless one of the
132128
following are true:
133129

134-
* Large amounts of data are being stored
130+
* Large amounts of data are being stored.
135131
* The single-address property of statics is required.
136132
* Interior mutability is required.
137133

0 commit comments

Comments
 (0)