You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: promotion.md
+77-18
Original file line number
Diff line number
Diff line change
@@ -1,25 +1,32 @@
1
1
# Const promotion
2
2
3
-
"Promotion" is a mechanism that affects code like `&3`: Instead of putting it on
4
-
the stack, the `3` is allocated in global static memory and a reference with
5
-
lifetime `'static` is provided. This is essentially an automatic transformation
6
-
turning `&EXPR` into `{ const _PROMOTED = &EXPR; EXPR }`, but only if `EXPR`
7
-
qualifies.
3
+
["(Implicit) Promotion"][promotion-rfc] is a mechanism that affects code like `&3`:
4
+
Instead of putting it on the stack, the `3` is allocated in global static memory
5
+
and a reference with lifetime `'static` is provided. This is essentially an
6
+
automatic transformation turning `&EXPR` into
7
+
`{ const _PROMOTED = &EXPR; EXPR}`, but only if `EXPR`qualifies.
8
8
9
9
Note that promotion happens on the MIR, not on surface-level syntax. This is
10
10
relevant when discussing e.g. handling of panics caused by overflowing
11
11
arithmetic.
12
12
13
+
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*.
14
+
Thus, if CTFE fails but the code would have worked fine at run-time, we broke the user's code for no good reason.
15
+
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].
16
+
That's why we have to be very conservative with what can and cannot be promoted.
0 commit comments