Skip to content

Commit 417088a

Browse files
committed
add interior-mutability-through-const-body example
1 parent e2ec55a commit 417088a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

promotion.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,21 @@ An access to a `static` is only promotable within the initializer of another
247247
`static`. This is for the same reason that `const` initializers
248248
[cannot access statics](const.md#reading-statics).
249249

250+
Crucially, however, the following is *not* legal:
251+
252+
```rust
253+
const X: Cell<i32> = Cell::new(5); // ok
254+
const XREF: &Cell<i32> = &X; // not ok
255+
fn main() {
256+
let x: &'static _ = &X; // not ok
257+
}
258+
```
259+
260+
Just like allowing `XREF` would be a problem because, by the inlining semantics,
261+
every user of `XREF` should get their own `Cell`; it would also be a problem to
262+
promote here because if that code getes executed multiple times (e.g. inside a
263+
loop), it should get a new `Cell` each time.
264+
250265
### Named locals
251266

252267
Promotable expressions cannot refer to named locals. This is not a technical

0 commit comments

Comments
 (0)