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
mutable borrows and shared borrows to values with interior mutability
86
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
87
88
+
```rust,compile_fail,E0492
89
+
# use core::sync::atomic::AtomicU8;
90
+
const C: &AtomicU8 = &AtomicU8::new(0); // ERROR not allowed
91
+
```
92
+
93
+
```rust,compile_fail,E0764
94
+
# use core::sync::atomic::AtomicU8;
95
+
const C: &AtomicU8 = &mut AtomicU8::new(0); // ERROR not allowed
96
+
```
97
+
98
+
```rust,compile_fail,E0492
99
+
# use core::sync::atomic::AtomicU8;
100
+
let _: &'static _ = const { &AtomicU8::new(0) }; // ERROR not allowed
101
+
```
102
+
103
+
```rust
104
+
# usecore::sync::atomic::AtomicU8;
105
+
constC: () = { _=&AtomicU8::new(0); }; // OK
106
+
```
107
+
108
+
```rust
109
+
# usecore::sync::atomic::AtomicU8;
110
+
staticS:AtomicU8=AtomicU8::new(0);
111
+
constC:&AtomicU8=&S; // OK
112
+
```
113
+
88
114
In other words, they are only allowed to refer to *transient* places, to *indirect* places, or to *static* places.
89
115
A place is *transient* if it is based on a local variable whose lifetime is strictly contained inside the current [const context].
116
+
117
+
```rust
118
+
constC: () = {
119
+
letmutx=0;
120
+
let_=&mutx; // Reference to a transient place.
121
+
};
122
+
```
123
+
90
124
A place is *indirect* if it is based on a [dereference expression][dereference operator].
125
+
126
+
```rust
127
+
constC: () = {
128
+
letx=&mut0;
129
+
let_=&mut*x; // Reference to an indirect place.
130
+
};
131
+
```
132
+
91
133
A place is *static* if it is based on a `static` item or a [promoted expression].
92
134
135
+
```rust
136
+
staticmutS:u8=0;
137
+
# #[allow(static_mut_refs)]
138
+
constC:&u8=unsafe { &mutS }; // Borrow of static item.
139
+
```
140
+
141
+
```rust
142
+
staticmutS:u8=0;
143
+
# #[allow(static_mut_refs)]
144
+
constC: () = {
145
+
let_=unsafe { &mutS }; // Borrow of static item.
146
+
};
147
+
```
148
+
149
+
```rust
150
+
constC:&[u8] =&mut []; // Borrow of promoted expression.
151
+
```
152
+
153
+
```rust
154
+
constC:&[u8] =&[]; // Borrow of promoted expression.
155
+
```
156
+
157
+
```rust
158
+
constC: () = {
159
+
let_:&'staticmut [u8] =&mut []; // Borrow of promoted expression.
160
+
};
161
+
```
162
+
93
163
r[const-eval.const-expr.deref]
94
164
* The [dereference operator] except for raw pointers.
0 commit comments