Skip to content

Commit aa999e9

Browse files
authored
Merge pull request #2058 from rust-lang/TC/add-const-mut-exceptions
Add caveats about mutable references in consts
2 parents a48cad1 + 53f4392 commit aa999e9

File tree

1 file changed

+113
-8
lines changed

1 file changed

+113
-8
lines changed

src/items/constant-items.md

Lines changed: 113 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,135 @@ const BITS_N_STRINGS: BitsNStrings<'static> = BitsNStrings {
4949
```
5050

5151
r[items.const.no-mut-refs]
52-
The final value of a `const` item cannot contain any mutable references.
52+
The final value of a `const` item, after the initializer is evaluated to a value that has the declared type of the constant, cannot contain any mutable references except as described below.
5353

5454
```rust
5555
# #![allow(static_mut_refs)]
5656
static mut S: u8 = 0;
57-
const C: &u8 = unsafe { &mut S }; // OK
57+
const _: &u8 = unsafe { &mut S }; // OK.
58+
// ^^^^^^
59+
// Allowed since this is coerced to `&S`.
5860
```
5961

6062
```rust
6163
# use core::sync::atomic::AtomicU8;
6264
static S: AtomicU8 = AtomicU8::new(0);
63-
const C: &AtomicU8 = &S; // OK
65+
const _: &AtomicU8 = &S; // OK.
66+
// ^^
67+
// Allowed even though the shared reference is to an interior
68+
// mutable value.
6469
```
6570

6671
```rust,compile_fail,E0080
6772
# #![allow(static_mut_refs)]
6873
static mut S: u8 = 0;
69-
const C: &mut u8 = unsafe { &mut S }; // ERROR not allowed
74+
const _: &mut u8 = unsafe { &mut S }; // ERROR.
75+
// ^^^^^^
76+
// Not allowed as the mutable reference appears in the final value.
7077
```
7178

7279
> [!NOTE]
73-
> We also disallow, in the final value, shared references to mutable statics created in the initializer for a separate reason. Consider:
80+
> Constant initializers can be thought of, in most cases, as being inlined wherever the constant appears. If a constant whose value contains a mutable reference to a mutable static were to appear twice, and this were to be allowed, that would create two mutable references, each having `'static` lifetime, to the same place. This could produce undefined behavior.
81+
>
82+
> Constants that contain mutable references to temporaries whose scopes have been extended to the end of the program have that same problem and an additional one.
83+
>
84+
> ```rust,compile_fail,E0764
85+
> const _: &mut u8 = &mut 0; // ERROR.
86+
> // ^^^^^^
87+
> // Not allowed as the mutable reference appears in the final value and
88+
> // because the constant expression contains a mutable borrow of an
89+
> // expression whose temporary scope would be extended to the end of
90+
> // the program.
91+
> ```
92+
>
93+
> Here, the value `0` is a temporary whose scope is extended to the end of the program (see [destructors.scope.lifetime-extension.static]). Such temporaries cannot be mutably borrowed in constant expressions (see [const-eval.const-expr.borrows]).
94+
>
95+
> To allow this, we'd have to decide whether each use of the constant creates a new `u8` value or whether each use shares the same lifetime-extended temporary. The latter choice, though closer to how `rustc` thinks about this today, would break the conceptual model that, in most cases, the constant initializer can be thought of as being inlined wherever the constant is used. Since we haven't decided, and due to the other problem mentioned, this is not allowed.
96+
97+
```rust,compile_fail,E0080
98+
# #![allow(static_mut_refs)]
99+
static mut S: u8 = 0;
100+
const _: &dyn Send = &unsafe { &mut S }; // ERROR.
101+
// ^^^^^^
102+
// Not allowed as the mutable reference appears in the final value,
103+
// even though type erasure occurs.
104+
```
105+
106+
Mutable references where the referent is a value of a [zero-sized type] are allowed.
107+
108+
```rust
109+
# #![allow(static_mut_refs)]
110+
static mut S: () = ();
111+
const _: &mut () = unsafe { &mut S }; // OK.
112+
// ^^ This is a zero-sized type.
113+
```
114+
115+
```rust
116+
# #![allow(static_mut_refs)]
117+
static mut S: [u8; 0] = [0; 0];
118+
const _: &mut [u8; 0] = unsafe { &mut S }; // OK.
119+
// ^^^^^^^ This is a zero-sized type.
120+
```
121+
122+
> [!NOTE]
123+
> This is allowed as, for a value of a zero-sized type, no bytes can actually be mutated. We must accept this as `&mut []` is [promoted].
124+
125+
Values of [union type] are not considered to contain any references; for this purpose, a value of union type is treated as a sequence of untyped bytes.
126+
127+
```rust
128+
# #![allow(static_mut_refs)]
129+
union U { f: &'static mut u8 }
130+
static mut S: u8 = 0;
131+
const _: U = unsafe { U { f: &mut S }}; // OK.
132+
// ^^^^^^^^^^^^^^^
133+
// This is treated as a sequence of untyped bytes.
134+
```
135+
136+
Mutable references contained within a [mutable static] may be referenced in the final value of a constant.
137+
138+
```rust
139+
# #![allow(static_mut_refs)]
140+
static mut S: &mut u8 = unsafe { static mut I: u8 = 0; &mut I };
141+
const _: &&mut u8 = unsafe { &S }; // OK.
142+
// ^^^^^^^
143+
// This mutable reference comes from a `static mut`.
144+
```
145+
146+
> [!NOTE]
147+
> This is allowed as it's separately not allowed to read from a mutable static during constant evaluation. See [const-eval.const-expr.path-static].
148+
149+
Mutable references contained within an [external static] may be referenced in the final value of a constant.
150+
151+
```rust
152+
# #![allow(static_mut_refs)]
153+
unsafe extern "C" { static S: &'static mut u8; }
154+
const _: &&mut u8 = unsafe { &S }; // OK.
155+
// ^^^^^^^
156+
// This mutable references comes from an extern static.
157+
```
158+
159+
> [!NOTE]
160+
> This is allowed as it's separately not allowed to read from an external static during constant evaluation. See [const-eval.const-expr.path-static].
161+
162+
> [!NOTE]
163+
> As described above, we accept, in the final value of constant items, shared references to static items whose values have interior mutability.
164+
>
165+
> ```rust
166+
> # use core::sync::atomic::AtomicU8;
167+
> static S: AtomicU8 = AtomicU8::new(0);
168+
> const _: &AtomicU8 = &S; // OK.
169+
> ```
170+
>
171+
> However, we disallow similar code when the interior mutable value is created in the initializer.
74172
>
75173
> ```rust,compile_fail,E0492
76174
> # use core::sync::atomic::AtomicU8;
77-
> const C: &AtomicU8 = &AtomicU8::new(0); // ERROR
175+
> const _: &AtomicU8 = &AtomicU8::new(0); // ERROR.
78176
> ```
79177
>
80-
> Here, the `AtomicU8` is a temporary that is lifetime extended to `'static` (see [destructors.scope.lifetime-extension.static]), and references to lifetime-extended temporaries with interior mutability are not allowed in the final value of a constant expression (see [const-eval.const-expr.borrows]).
178+
> Here, the `AtomicU8` is a temporary whose scope is extended to the end of the program (see [destructors.scope.lifetime-extension.static]). Such temporaries with interior mutability cannot be borrowed in constant expressions (see [const-eval.const-expr.borrows]).
179+
>
180+
> To allow this, we'd have to decide whether each use of the constant creates a new `AtomicU8` or whether each use shares the same lifetime-extended temporary. The latter choice, though closer to how `rustc` thinks about this today, would break the conceptual model that, in most cases, the constant initializer can be thought of as being inlined wherever the constant is used. Since we haven't decided, this is not allowed.
81181
82182
r[items.const.expr-omission]
83183
The constant expression may only be omitted in a [trait definition].
@@ -154,10 +254,15 @@ fn unused_generic_function<T>() {
154254
[const_eval]: ../const_eval.md
155255
[associated constant]: ../items/associated-items.md#associated-constants
156256
[constant value]: ../const_eval.md#constant-expressions
257+
[external static]: items.extern.static
157258
[free]: ../glossary.md#free-item
158259
[static lifetime elision]: ../lifetime-elision.md#const-and-static-elision
159260
[trait definition]: traits.md
160261
[underscore imports]: use-declarations.md#underscore-imports
161262
[`Copy`]: ../special-types-and-traits.md#copy
162263
[value namespace]: ../names/namespaces.md
163-
[promotion]: ../destructors.md#constant-promotion
264+
[mutable static]: items.static.mut
265+
[promoted]: destructors.scope.const-promotion
266+
[promotion]: destructors.scope.const-promotion
267+
[union type]: type.union
268+
[zero-sized type]: layout.properties.size

0 commit comments

Comments
 (0)