Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/destructors.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,17 @@ r[destructors.scope.const-promotion]

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).

r[destructors.scope.const-promotion.extern-static]
A borrow of an [`extern` static] cannot be promoted, even if the resulting reference is never read from.

```rust,compile_fail
unsafe extern "C" {
static X: i32;
}

static mut FOO: *const &i32 = [unsafe { &X }].as_ptr();
```

Comment on lines +381 to +391

@traviscross traviscross Jul 28, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the error produced has an error code, we'll want to add that to the infostring line.

Probably we also want to add some comments to this example to help make clear what's going on. And maybe it's worth adding an admonition that mentions the theory behind why this is rejected (see, e.g., what I wrote in rust-lang/rust#157641 (comment)).

View changes since the review

r[destructors.scope.lifetime-extension]
### Temporary lifetime extension

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