Skip to content

Commit 270c2b9

Browse files
authored
Merge pull request #1610 from dingxiangfei2009/const-ref-to-static
`const` expression can borrow static items
2 parents 8a0b8a4 + 2bc4396 commit 270c2b9

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

src/const_eval.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ 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:
46+
* Writes to `static` items are not allowed in any constant evaluation context.
47+
* Reads from `extern` statics are not allowed in any constant evaluation context.
48+
* If the evaluation is *not* carried out in an initializer of a `static` item, then reads from any mutable `static` are not allowed. A mutable `static` is a `static mut` item, or a `static` item with an interior-mutable type.
49+
50+
These requirements are checked only when the constant is evaluated. In other words, having such accesses syntactically occur in const contexts is allowed as long as they never get executed.
4651

4752
r[const-eval.const-expr.tuple]
4853
* [Tuple expressions].
@@ -177,6 +182,7 @@ of whether you are building on a `64` bit or a `32` bit system.
177182
[enum discriminants]: items/enumerations.md#discriminants
178183
[expression statements]: statements.md#expression-statements
179184
[expressions]: expressions.md
185+
[`extern` statics]: items/external-blocks.md#statics
180186
[field]: expressions/field-expr.md
181187
[functions]: items/functions.md
182188
[grouped]: expressions/grouped-expr.md

src/items/constant-items.md

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ const BITS_N_STRINGS: BitsNStrings<'static> = BitsNStrings {
4848
};
4949
```
5050

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

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)