Skip to content

Commit 9d66d5f

Browse files
committed
Document that extern statics are never eligible for promotion
Reference update for rust-lang/rust#157641: a borrow of an extern static is never eligible for constant promotion, even when the reference itself is never read from.
1 parent 86635e3 commit 9d66d5f

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

src/destructors.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,17 @@ r[destructors.scope.const-promotion]
378378

379379
Promotion of a value expression to a `'static` slot occurs when the expression could be written in a constant and borrowed, and that borrow could be dereferenced where the expression was originally written, without changing the runtime behavior. That is, the promoted expression can be evaluated at compile-time and the resulting value does not contain [interior mutability] or [destructors] (these properties are determined based on the value where possible, e.g. `&None` always has the type `&'static Option<_>`, as it contains nothing disallowed).
380380

381+
r[destructors.scope.const-promotion.extern-static]
382+
A borrow of an [`extern` static] cannot be promoted, even if the resulting reference is never read from.
383+
384+
```rust,compile_fail
385+
unsafe extern "C" {
386+
static X: i32;
387+
}
388+
389+
static mut FOO: *const &i32 = [unsafe { &X }].as_ptr();
390+
```
391+
381392
r[destructors.scope.lifetime-extension]
382393
### Temporary lifetime extension
383394

@@ -648,6 +659,7 @@ There is one additional case to be aware of: when a panic reaches a [non-unwindi
648659
[destructors]: destructors.md
649660
[destructuring assignment]: expr.assign.destructure
650661
[expression]: expressions.md
662+
[`extern` static]: items/external-blocks.md#statics
651663
[guard condition operand]: expressions/match-expr.md#match-guard-chains
652664
[identifier pattern]: patterns.md#identifier-patterns
653665
[initialized]: glossary.md#initialized

0 commit comments

Comments
 (0)