Skip to content

Commit c1f8da5

Browse files
committed
Revise, clarify, and add examples
Rather than talking about lifetime-extended temporaries in the top-level scope of an initializer, which is maybe a bit ambiguous, let's speak directly to the result of the lifetime extension, which is that these temporaries disallowed for borrows would have their lifetimes extended to the end of the program. Let's also speak about place expressions, rather than places, as that's more precise here. We'll add examples throughout. Thanks to RalfJ for the substance of many of these.
1 parent 62a33ad commit c1f8da5

File tree

1 file changed

+124
-9
lines changed

1 file changed

+124
-9
lines changed

src/const_eval.md

Lines changed: 124 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,126 @@ r[const-eval.const-expr.builtin-arith-logic]
8181
operators used on integer and floating point types, `bool`, and `char`.
8282

8383
r[const-eval.const-expr.borrows]
84-
* All forms of [borrow]s, including raw borrows, with one limitation:
85-
mutable borrows and shared borrows to values with interior mutability
86-
are not allowed to refer to [lifetime-extended temporaries in the top-level scope of a `const` or `static` initializer expression][lifetime-extension-const].
87-
88-
In other words, they are only allowed to refer to *transient* places, to *indirect* places, or to *static* places.
89-
A place is *transient* if it is based on a local variable whose lifetime is strictly contained inside the current [const context].
90-
A place is *indirect* if it is based on a [dereference expression][dereference operator].
91-
A place is *static* if it is based on a `static` item or a [promoted expression].
84+
* All forms of [borrow]s, including raw borrows, except borrows of expressions whose temporary scopes would be extended (see [temporary lifetime extension]) to the end of the program and which are either:
85+
* Mutable borrows.
86+
* Shared borrows of expressions that result in values with [interior mutability].
87+
88+
```rust,compile_fail,E0764
89+
// Due to being in tail position, this borrow extends the scope of the
90+
// temporary to the end of the program. Since the borrow is mutable,
91+
// this is not allowed in a const expression.
92+
const C: &u8 = &mut 0; // ERROR not allowed
93+
```
94+
95+
```rust,compile_fail,E0764
96+
// Const blocks are similar to initializers of `const` items.
97+
let _: &u8 = const { &mut 0 }; // ERROR not allowed
98+
```
99+
100+
```rust,compile_fail,E0492
101+
# use core::sync::atomic::AtomicU8;
102+
// This is not allowed as 1) the temporary scope is extended to the
103+
// end of the program and 2) the temporary has interior mutability.
104+
const C: &AtomicU8 = &AtomicU8::new(0); // ERROR not allowed
105+
```
106+
107+
```rust,compile_fail,E0492
108+
# use core::sync::atomic::AtomicU8;
109+
// As above.
110+
let _: &_ = const { &AtomicU8::new(0) }; // ERROR not allowed
111+
```
112+
113+
```rust
114+
# #![allow(static_mut_refs)]
115+
// Even though this borrow is mutable, it's not of a temporary, so
116+
// this is allowed.
117+
const C: &u8 = unsafe { static mut S: u8 = 0; &mut S }; // OK
118+
```
119+
120+
```rust
121+
# use core::sync::atomic::AtomicU8;
122+
// Even though this borrow is of a value with interior mutability,
123+
// it's not of a temporary, so this is allowed.
124+
const C: &AtomicU8 = {
125+
static S: AtomicU8 = AtomicU8::new(0); &S // OK
126+
};
127+
```
128+
129+
```rust
130+
# use core::sync::atomic::AtomicU8;
131+
// This shared borrow of an interior mutable temporary is allowed
132+
// because its scope is not extended.
133+
const C: () = { _ = &AtomicU8::new(0); }; // OK
134+
```
135+
136+
```rust
137+
// Even though the borrow is mutable and the temporary lives to the
138+
// end of the program due to promotion, this is allowed because the
139+
// borrow is not in tail position and so the scope of the temporary
140+
// is not extended via temporary lifetime extension.
141+
const C: () = { let _: &'static mut [u8] = &mut []; }; // OK
142+
// ~~
143+
// Promoted temporary.
144+
```
145+
146+
> [!NOTE]
147+
> In other words -- to focus on what's allowed rather than what's not allowed -- shared borrows of interior mutable data and mutable borrows are only allowed in a [const context] when the borrowed [place expression] is *transient*, *indirect*, or *static*.
148+
>
149+
> A place expression is *transient* if it is a variable local to the current const context or an expression whose temporary scope is contained inside the current const context.
150+
>
151+
> ```rust
152+
> // The borrow is of a variable local to the initializer, therefore
153+
> // this place expresssion is transient.
154+
> const C: () = { let mut x = 0; _ = &mut x; };
155+
> ```
156+
>
157+
> ```rust
158+
> // The borrow is of a temporary whose scope has not been extended,
159+
> // therefore this place expression is transient.
160+
> const C: () = { _ = &mut 0u8; };
161+
> ```
162+
>
163+
> ```rust
164+
> // When a temporary is promoted but not lifetime extended, its
165+
> // place expression is still treated as transient.
166+
> const C: () = { let _: &'static mut [u8] = &mut []; };
167+
> ```
168+
>
169+
> A place expression is *indirect* if it is a [dereference expression].
170+
>
171+
> ```rust
172+
> const C: () = { _ = &mut *(&mut 0); };
173+
> ```
174+
>
175+
> A place expression is *static* if it is a `static` item.
176+
>
177+
> ```rust
178+
> # #![allow(static_mut_refs)]
179+
> const C: &u8 = unsafe { static mut S: u8 = 0; &mut S };
180+
> ```
181+
182+
> [!NOTE]
183+
> One surprising consequence of these rules is that we allow this,
184+
>
185+
> ```rust
186+
> const C: &[u8] = { let x: &mut [u8] = &mut []; x }; // OK
187+
> // ~~~~~~~
188+
> // Empty arrays are promoted even behind mutable borrows.
189+
> ```
190+
>
191+
> but we disallow this similar code:
192+
>
193+
> ```rust,compile_fail,E0764
194+
> const C: &[u8] = &mut []; // ERROR
195+
> // ~~~~~~~
196+
> // Tail expression.
197+
> ```
198+
>
199+
> The difference between these is that, in the first, the empty array is [promoted] but its scope does not undergo [temporary lifetime extension], so we consider the [place expression] to be transient (even though after promotion the place indeed lives to the end of the program). In the second, the scope of the empty array temporary does undergo lifetime extension, and so it is rejected due to being a mutable borrow of a lifetime-extended temporary (and therefore borrowing a non-transient place expression).
200+
>
201+
> The effect is surprising because temporary lifetime extension, in this case, causes less code to compile than would without it.
202+
>
203+
> See [issue #143129](https://github.com/rust-lang/rust/issues/143129) for more details.
92204
93205
r[const-eval.const-expr.deref]
94206
* The [dereference operator] except for raw pointers.
@@ -178,6 +290,7 @@ of whether you are building on a `64` bit or a `32` bit system.
178290
[const generic parameters]: items/generics.md#const-generics
179291
[constants]: items/constant-items.md
180292
[Const parameters]: items/generics.md
293+
[dereference expression]: expressions/operator-expr.md#the-dereference-operator
181294
[dereference operator]: expressions/operator-expr.md#the-dereference-operator
182295
[destructors]: destructors.md
183296
[enum discriminants]: items/enumerations.md#discriminants
@@ -190,7 +303,6 @@ of whether you are building on a `64` bit or a `32` bit system.
190303
[interior mutability]: interior-mutability.md
191304
[if]: expressions/if-expr.md#if-expressions
192305
[lazy boolean]: expressions/operator-expr.md#lazy-boolean-operators
193-
[lifetime-extension-const]: destructors.md#r-destructors.scope.lifetime-extension.static
194306
[let statements]: statements.md#let-statements
195307
[literals]: expressions/literal-expr.md
196308
[logical]: expressions/operator-expr.md#arithmetic-and-logical-binary-operators
@@ -200,10 +312,13 @@ of whether you are building on a `64` bit or a `32` bit system.
200312
[overflow]: expressions/operator-expr.md#overflow
201313
[paths]: expressions/path-expr.md
202314
[patterns]: patterns.md
315+
[place expression]: expr.place-value.place-memory-location
203316
[promoted expression]: destructors.md#constant-promotion
317+
[promoted]: destructors.md#constant-promotion
204318
[range expressions]: expressions/range-expr.md
205319
[slice]: types/slice.md
206320
[statics]: items/static-items.md
207321
[struct]: expressions/struct-expr.md
322+
[temporary lifetime extension]: destructors.scope.lifetime-extension
208323
[tuple expressions]: expressions/tuple-expr.md
209324
[while]: expressions/loop-expr.md#predicate-loops

0 commit comments

Comments
 (0)