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
The final value of a `const` itemcannot 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.
53
53
54
54
```rust
55
55
# #![allow(static_mut_refs)]
56
56
staticmutS:u8=0;
57
-
constC:&u8=unsafe { &mutS }; // OK
57
+
const_:&u8=unsafe { &mutS }; // OK.
58
+
// ^^^^^^
59
+
// Allowed since this is coerced to `&S`.
58
60
```
59
61
60
62
```rust
61
63
# usecore::sync::atomic::AtomicU8;
62
64
staticS:AtomicU8=AtomicU8::new(0);
63
-
constC:&AtomicU8=&S; // OK
65
+
const_:&AtomicU8=&S; // OK.
66
+
// ^^
67
+
// Allowed even though the shared reference is to an interior
68
+
// mutable value.
64
69
```
65
70
66
71
```rust,compile_fail,E0080
67
72
# #![allow(static_mut_refs)]
68
73
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.
70
77
```
71
78
72
79
> [!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.
// 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
+
staticmutS: () = ();
111
+
const_:&mut () =unsafe { &mutS }; // OK.
112
+
// ^^ This is a zero-sized type.
113
+
```
114
+
115
+
```rust
116
+
# #![allow(static_mut_refs)]
117
+
staticmutS: [u8; 0] = [0; 0];
118
+
const_:&mut [u8; 0] =unsafe { &mutS }; // 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
+
unionU { f:&'staticmutu8 }
130
+
staticmutS:u8=0;
131
+
const_:U=unsafe { U { f:&mutS }}; // 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.
> 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` isatemporarywhosescopeisextendedtotheendoftheprogram (see [destructors.scope.lifetime-extension.static]).Suchtemporarieswithinteriormutabilitycannotbeborrowedinconstantexpressions (see [const-eval.const-expr.borrows]).
179
+
>
180
+
> Toallowthis, we'dhavetodecidewhethereachuse 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.
81
181
82
182
r[items.const.expr-omission]
83
183
The constant expression may only be omitted in a [trait definition].
0 commit comments