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
+
88
108
In other words, they are only allowed to refer to *transient* places, to *indirect* places, or to *static* places.
89
109
A place is *transient* if it is based on a local variable whose lifetime is strictly contained inside the current [const context].
110
+
111
+
```rust
112
+
constC: () = {
113
+
letmutx=0;
114
+
let_=&mutx; // Reference to a transient place.
115
+
};
116
+
```
117
+
90
118
A place is *indirect* if it is based on a [dereference expression][dereference operator].
119
+
120
+
```rust
121
+
constC: () = {
122
+
letx=&mut0;
123
+
let_=&mut*x; // Reference to an indirect place.
124
+
};
125
+
```
126
+
91
127
A place is *static* if it is based on a `static` item or a [promoted expression].
92
128
129
+
```rust
130
+
staticmutS:u8=0;
131
+
# #[allow(static_mut_refs)]
132
+
constC:&u8=unsafe { &mutS }; // Borrow of static item.
133
+
```
134
+
135
+
```rust
136
+
staticmutS:u8=0;
137
+
# #[allow(static_mut_refs)]
138
+
constC: () = {
139
+
let_=unsafe { &mutS }; // Borrow of static item.
140
+
};
141
+
```
142
+
143
+
```rust
144
+
constC:&[u8] =&mut []; // Borrow of promoted expression.
145
+
```
146
+
147
+
```rust
148
+
constC: () = {
149
+
let_:&'staticmut [u8] =&mut []; // Borrow of promoted expression.
150
+
};
151
+
```
152
+
93
153
r[const-eval.const-expr.deref]
94
154
* The [dereference operator] except for raw pointers.
0 commit comments