Skip to content

Commit f0b6de6

Browse files
committed
update docs
1 parent 8d2506c commit f0b6de6

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

promotion.md

+13-14
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,19 @@ declaration. We can thus be less conservative. This is called *explicit*
6969
promotion.
7070

7171
Currently, the following are considered explicit promotion contexts:
72-
* `#[rustc_args_required_const]` arguments and inline assembly `const` operands everywhere
72+
* `#[rustc_args_required_const]` arguments and inline assembly `const` operands everywhere.
73+
* Everything inside the bodies of `const` and `static` items. (Note: this is handled separately from "explicit contexts" in promotion analysis, but the effect is the same.)
7374

74-
In these contexts, we promote calls to arbitrary `const fn`. That is currently
75-
the only difference.
75+
In these contexts, we promote calls to arbitrary `const fn`.
7676

77-
[warn-rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1229-compile-time-asserts.md
78-
79-
## Promotion in const contexts
77+
There is one further special case for the bodies of `const` and `static` items; here we additionally promote union field accesses.
78+
Both of these special cases can lead to promoting things that can fail to evaluate.
79+
Currently, this works out because it just leads to a warning, but longer-term it would be desirable to turn evaluation failures into hard errors, which for these promoteds means we have to guarantee that we only evaluate them on-demand.
8080

81-
When promoting inside a const context (`const` and `static` items and `const fn`), we relax almost all promotion restrictions, except for basic constness restrictions listed below.
82-
We do this for all kinds of promotion.
81+
[See below][static access] for another special case in promotion analysis:
82+
accesses and references to statics are only promoted inside other statics.
8383

84-
For `const fn`, [this is a bug](https://github.com/rust-lang/rust/issues/75586).
85-
86-
For `const` and `static` items, this can lead to promoting things that can fail to evaluate.
87-
Currently, this works out because in the first case it just leads to a warning, but longer-term it would be desirable to turn evaluation failures into hard errors, which for these promoteds means we have to guarantee that we only evaluate them on-demand.
84+
[warn-rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1229-compile-time-asserts.md
8885

8986
## "enclosing scope" rule
9087

@@ -228,6 +225,7 @@ or `const` item and refer to that.
228225
the result of computing a promoted is a value that does not need dropping.
229226

230227
### Access to a `const` or `static`
228+
[access-static]: #access-to-a-const-or-static
231229

232230
When accessing a `const` in a promotable context, its value gets computed
233231
at compile-time anyway, so we do not have to check the initializer. However, the
@@ -248,8 +246,9 @@ const ANSWER: i32 = {
248246
let x: &'static i32 = &ANSWER;
249247
```
250248

251-
An access to a `static` is only promotable within the initializer of another
252-
`static`. This is for the same reason that `const` initializers
249+
An access to a `static`, including just taking references to a `static`, is only
250+
promotable within the initializer of another `static`. This is for the same
251+
reason that `const` initializers
253252
[cannot access statics](const.md#reading-statics).
254253

255254
Crucially, however, the following is *not* legal:

0 commit comments

Comments
 (0)