Skip to content

Commit 91e223c

Browse files
committed
move const/static section to other promotion contexts
1 parent 9d36e9a commit 91e223c

File tree

1 file changed

+34
-35
lines changed

1 file changed

+34
-35
lines changed

promotion.md

+34-35
Original file line numberDiff line numberDiff line change
@@ -44,41 +44,7 @@ attribute, introduced in
4444
specify these parameters and (aggressively, see below) try to promote the
4545
corresponding arguments.
4646

47-
## Implicit and explicit promotion
48-
49-
On top of what applies to [consts](const.md), promoteds suffer from the additional issue that *the user did not ask for them to be evaluated at compile-time*.
50-
Thus, if CTFE fails but the code would have worked fine at run-time, we broke the user's code for no good reason.
51-
Even if we are sure we found an error in the user's code, we are only allowed to [emit a warning, not a hard error][warn-rfc].
52-
We call this *implicit* promotion, and we have to be very conservative with what can and cannot be implicitly promoted.
53-
54-
CTFE of implicitly promoted code must never fail to evaluate except if the
55-
run-time code also would have failed. This means we cannot permit calling
56-
arbitrary `const fn`, as discussed in detail in
57-
[rust-lang/const-eval#19](https://github.com/rust-lang/const-eval/issues/19).
58-
Thus, only functions marked `#[rustc_promotable]` are implicitly promotable (see
59-
below).
60-
61-
On the other hand, when a user passes an expression to a function with
62-
`#[rustc_args_required_const]`, the only way for this code to compile is to
63-
promote it. In that sense, the user is explicitly asking for that expression to
64-
be evaluated at compile-time even though they have not written it in a `const`
65-
declaration. We can thus be less conservative. This is called *explicit*
66-
promotion.
67-
68-
Currently, the following are considered explicit promotion contexts:
69-
* `#[rustc_args_required_const]` arguments
70-
* the bodies of `const` and `static` items and array lengths
71-
72-
Everything else is an implicit promotion context, including `const fn` bodies and non-`Copy` array initializers.
73-
74-
In an explicit promotion context, we promote every closed expression (i.e.,
75-
expressions that do not depend on other variables) of reference type, subject to
76-
the usual restrictions of [consts](const.md). This means that interior
77-
mutability and values that need dropping are not promoted.
78-
79-
[warn-rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1229-compile-time-asserts.md
80-
81-
## Promotion inside `const` and `static`
47+
### Promotion inside `const` and `static`
8248

8349
Lifetime extension is also responsible for making code like this work:
8450

@@ -108,6 +74,39 @@ the "enclosing scope", similar to how `let x = &mut x;` creates a reference
10874
whose lifetime lasts for the enclosing scope. This is decided during MIR
10975
building already, and does not involve lifetime extension.
11076

77+
## Implicit and explicit promotion
78+
79+
On top of what applies to [consts](const.md), promoteds suffer from the additional issue that *the user did not ask for them to be evaluated at compile-time*.
80+
Thus, if CTFE fails but the code would have worked fine at run-time, we broke the user's code for no good reason.
81+
Even if we are sure we found an error in the user's code, we are only allowed to [emit a warning, not a hard error][warn-rfc].
82+
We call this *implicit* promotion, and we have to be very conservative with what can and cannot be implicitly promoted.
83+
84+
CTFE of implicitly promoted code must never fail to evaluate except if the
85+
run-time code also would have failed. This means we cannot permit calling
86+
arbitrary `const fn`, as discussed in detail in
87+
[rust-lang/const-eval#19](https://github.com/rust-lang/const-eval/issues/19).
88+
Thus, only functions marked `#[rustc_promotable]` are implicitly promotable (see
89+
below).
90+
91+
On the other hand, when a user passes an expression to a function with
92+
`#[rustc_args_required_const]`, the only way for this code to compile is to
93+
promote it. In that sense, the user is explicitly asking for that expression to
94+
be evaluated at compile-time even though they have not written it in a `const`
95+
declaration. We can thus be less conservative. This is called *explicit*
96+
promotion.
97+
98+
Currently, the following are considered explicit promotion contexts:
99+
* `#[rustc_args_required_const]` arguments
100+
* lifetime extension in the bodies of `const` and `static` items and array lengths
101+
102+
Everything else is an implicit promotion context, including `const fn` bodies and non-`Copy` array initializers.
103+
104+
In an explicit promotion context, we consider almost everything promotable,
105+
subject to the usual restrictions of [consts](const.md). This means that
106+
interior mutability and values that need dropping are not promoted.
107+
108+
[warn-rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1229-compile-time-asserts.md
109+
111110
## Promotability
112111

113112
We have described the circumstances where promotion is desirable, but what

0 commit comments

Comments
 (0)