Skip to content

Commit dec67c2

Browse files
const expression can borrow static items
1 parent 604e682 commit dec67c2

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
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
@@ -48,6 +48,8 @@ const BITS_N_STRINGS: BitsNStrings<'static> = BitsNStrings {
4848
};
4949
```
5050

51+
The final value of a `const` item cannot contain references to anything mutable.
52+
5153
r[items.const.expr-omission]
5254
The constant expression may only be omitted in a [trait definition].
5355

src/items/static-items.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ All access to a static is safe, but there are a number of restrictions on
3737
statics:
3838

3939
r[items.static.sync]
40-
* The type must have the `Sync` trait bound to allow thread-safe access.
41-
42-
r[items.static.const]
43-
* Constants cannot refer to statics.
40+
* The type must have the [`Sync`](std::marker::Sync) trait bound to allow thread-safe access.
4441

4542
r[items.static.init.omission]
4643
The initializer expression must be omitted in an [external block], and must be
@@ -159,7 +156,7 @@ It can be confusing whether or not you should use a constant item or a static
159156
item. Constants should, in general, be preferred over statics unless one of the
160157
following are true:
161158

162-
* Large amounts of data are being stored
159+
* Large amounts of data are being stored.
163160
* The single-address property of statics is required.
164161
* Interior mutability is required.
165162

0 commit comments

Comments
 (0)