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
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.
operators used on integer and floating point types, `bool`, and `char`.
82
82
83
83
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:
85
+
86
+
* Mutable borrows of expressions whose temporary scopes would be extended (see [temporary lifetime extension]) to the end of the program.
87
+
* Shared borrows of expressions whose temporary scopes would be extended to the end of the program when those expressions result in values with interior mutability.
88
+
89
+
```rust,compile_fail,E0764
90
+
// Due to being in tail position, this borrow extends the scope of the
91
+
// temporary to the end of the program. Since the borrow is mutable,
92
+
// this is not allowed in a const expression.
93
+
const C: &[u8] = &mut []; // ERROR not allowed
94
+
```
95
+
96
+
```rust,compile_fail,E0764
97
+
// Const blocks are similar to initializers of `const` items.
98
+
let _: &[u8] = const { &mut [] }; // ERROR not allowed
99
+
```
100
+
101
+
```rust,compile_fail,E0492
102
+
# use core::sync::atomic::AtomicU8;
103
+
// This is not allowed as 1) the temporary scope is extended to the
104
+
// end of the program and 2) the temporary has interior mutability.
105
+
const C: &AtomicU8 = &AtomicU8::new(0); // ERROR not allowed
106
+
```
107
+
108
+
```rust,compile_fail,E0492
109
+
# use core::sync::atomic::AtomicU8;
110
+
// As above.
111
+
let _: &_ = const { &AtomicU8::new(0) }; // ERROR not allowed
112
+
```
113
+
114
+
```rust
115
+
// Even though the borrow is mutable and the temporary lives to the
116
+
// end of the program due to promotion, this is allowed because the
117
+
// borrow is not in tail position and so the scope of the temporary
118
+
// is not extended via temporary lifetime extension.
// This shared borrow of an interior mutable temporary is allowed
127
+
// because its scope is not extended.
128
+
constC: () = { _=&AtomicU8::new(0); }; // OK
129
+
```
130
+
131
+
```rust
132
+
# #![allow(static_mut_refs)]
133
+
// Even though this borrow is mutable, it's not of a temporary, so
134
+
// this is allowed.
135
+
constC:&u8=unsafe { staticmutS:u8=0; &mutS }; // OK
136
+
```
137
+
138
+
```rust
139
+
# usecore::sync::atomic::AtomicU8;
140
+
// Even though this borrow is of a value with interior mutability,
141
+
// it's not of a temporary, so this is allowed.
142
+
constC:&AtomicU8= {
143
+
staticS:AtomicU8=AtomicU8::new(0); &S// OK
144
+
};
145
+
```
146
+
147
+
> [!NOTE]
148
+
> In other words, 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*.
149
+
>
150
+
> 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.
151
+
>
152
+
> ```rust
153
+
> // The borrow is of a variable local to the initializer, therefore
154
+
> // this place expresssion is transient.
155
+
> constC: () = { letmutx=0; _=&mutx; };
156
+
> ```
157
+
>
158
+
> ```rust
159
+
> // The borrow is of a temporary whose scope has not been extended,
160
+
> // therefore this place expression is transient.
161
+
> constC: () = { _=&mut0u8; };
162
+
> ```
163
+
>
164
+
> ```rust
165
+
> // When a temporary is promoted but not lifetime extended, its
166
+
> // place expression is still treated as transient.
0 commit comments