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
+30-30
Original file line number
Diff line number
Diff line change
@@ -44,6 +44,36 @@ attribute, introduced in
44
44
specify these parameters and (aggressively, see below) try to promote the
45
45
corresponding arguments.
46
46
47
+
### Promotion inside `const` and `static`
48
+
49
+
Lifetime extension is also responsible for making code like this work:
50
+
51
+
```rust
52
+
constFOO:&'statici32= {
53
+
letx=&13;
54
+
x
55
+
};
56
+
```
57
+
58
+
Like in run-time code, a part of the MIR (the one computing `13`) is spliced
59
+
into a separate MIR body, and evaluated like a separate constant. In the case
60
+
of `const` and `static` initializers, this does not affect how the code is
61
+
evaluated (everything happens at compile-time), but it still affects the
62
+
lifetimes.
63
+
64
+
Notice that some code involving `&`*looks* like it relies on lifetime
65
+
extension but actually does not:
66
+
67
+
```rust
68
+
constEMPTY_BYTES:&Vec<u8> =&Vec::new(); // Ok without lifetime extension
69
+
```
70
+
71
+
`Vec::new()` cannot get promoted because it needs dropping. And yet this
72
+
compiles. Why that? The reason is that the reference obtains the lifetime of
73
+
the "enclosing scope", similar to how `let x = &mut x;` creates a reference
74
+
whose lifetime lasts for the enclosing scope. This is decided during MIR
75
+
building already, and does not involve lifetime extension.
76
+
47
77
## Implicit and explicit promotion
48
78
49
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*.
@@ -78,36 +108,6 @@ mutability and values that need dropping are not promoted.
0 commit comments