Uh oh!
There was an error while loading. Please reload this page.
Do not promote extern statics - #157641
Conversation
rustbot
commented
Jun 9, 2026
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
rustbot
commented
Jun 9, 2026
r? @oli-obk rustbot has assigned @oli-obk. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
6257168 to
80ac224Compare
This comment has been minimized.
This comment has been minimized.
80ac224 to
8877f4cCompareUh oh!
There was an error while loading. Please reload this page.
This comment has been minimized.
This comment has been minimized.
rustbot
commented
Jun 10, 2026
Some changes occurred to the CTFE machinery Some changes occurred to constck cc @fee1-dead |
oli-obk
commented
Jun 10, 2026
So... This is technically a breaking change, as we now have a new const check, which previously only errored if actually evaluated. So an associated const could now error, if it previously was unused in the current crate. A different oddity is that we don't check this in const fns, as those can be fine at runtime, as long as the extern static is never read at compile time. Thoughts @rust-lang/wg-const-eval? |
lcnr
commented
Jun 11, 2026
crater than FCP merge in that case? |
The PR changes const-checking, not promotion. Something seems wrong, what is this actually trying to fix? This looks like an ad-hoc special case in const checking; I am not sure what this buys us since it can be trivially bypassed (by creating a pointer and then deref'ing it). To fix #143174, I would have expected this to change promotion ( |
RalfJung
commented
Jun 11, 2026
We already avoid promotion in this: fnmain(){unsafe{let f:&Foo = &Foo(BAR);}}Why do we promote in the other case? |
oli-obk
commented
Jun 11, 2026
This PR originally had a promotion time check. Preventing it in const check covers exactly the problematic use case, as everything else doesn't get promoted. |
I don't think we should fix a promotion bug by changing const-checking. That's a very non-local invariant relying on the subtle interplay of which exact code gets rejected where. Promotion on its own is supposed to only accept code that cannot fail to evaluate. It apparently does not do this job properly. |
The original version of this PR actually did modify promotion; the diff is available here: 8877f4c. We decided to change it because the original logic felt too messy: #157641 (comment), and I just forgot to change the PR description after making the change.
Promotion is gated behind fn main case never gets promoted, even though both desugar to "deref of a static addr." |
That check already special-cases thread-local statics here: It is very common in the interpreter to treat thread-local statics and extern statics the same, so what I would have expected is that we add |
I actually did try that at first, just adding since For instance, in the non-problematic case, extern"C"{staticX:i32;}staticmutFOO:*const&i32 = [unsafe{&X}].as_ptr();gets lowered to after promotion, and since extern"C"{staticBAR:i32;}staticFOO:&(i32,) = unsafe{&(BAR,)};gets lowered to where the |
RalfJung
commented
Jun 12, 2026
Yeah I think we should reject such promotion (unless crater disagrees). People can use |
oli-obk
commented
Jun 12, 2026
Cool, let's try that then and crater it. Seems very niche |
168eebd to
3e8d36aComparesjwang05
commented
Jun 30, 2026
Reference PR is here: rust-lang/reference#2302 |
rust-rfcbot
commented
Jul 4, 2026
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. |
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.
📋 This PR cannot be approved because it currently has the following labels: |
traviscross
commented
Aug 7, 2026
…promotion, r=oli-obk,traviscross Do not promote extern statics We already reject thread-local statics during promotion, so extend that check to extern statics as well. fixesrust-lang#143174
…uwer Rollup of 21 pull requests Successful merges: - #159784 (Hint that memchr returns an in-bounds index) - #150885 (Revive L4Re target) - #159643 (Add support for splatted function pointers) - #160433 (delegation: add support for wrapping of the return value with `From::from`) - #160530 (refactor handling of target features in Session) - #160606 (bootstrap: Store and use an explicit CheckKind in `check::Rustc`) - #160628 (fix ICE in `suggest_add_reference_to_arg` for non-callable items) - #160634 (miri subtree update) - #157641 (Do not promote extern statics) - #158904 (Fix FutureDropPoll shim for by-move async closures) - #160103 (Add regression test for GAT bound mismatched type error) - #160335 (dlopen offload) - #160445 (codegen: classify localized MSVC linker progress as linker_info) - #160499 (rustc_resolve: move diagnostic attribute linting to attr parsing) - #160504 (cleanup borrowck, improve c-variadic handling) - #160577 (expand: Feature gate AST-based attribute macros on expressions and statements) - #160587 (Add regression test for associated type outlives bound at call site) - #160625 (platform-support/netbsd.md: No longer mention 8.x, due to EoL.) - #160636 (derive(Diagnostic): link to proper docs) - #160644 (Clean up some manual debug impls) - #160649 (move naked function ui tests)
…promotion, r=oli-obk,traviscross Do not promote extern statics We already reject thread-local statics during promotion, so extend that check to extern statics as well. fixesrust-lang#143174
…uwer Rollup of 20 pull requests Successful merges: - #159784 (Hint that memchr returns an in-bounds index) - #150885 (Revive L4Re target) - #159643 (Add support for splatted function pointers) - #160433 (delegation: add support for wrapping of the return value with `From::from`) - #160530 (refactor handling of target features in Session) - #160606 (bootstrap: Store and use an explicit CheckKind in `check::Rustc`) - #160628 (fix ICE in `suggest_add_reference_to_arg` for non-callable items) - #160634 (miri subtree update) - #157641 (Do not promote extern statics) - #158904 (Fix FutureDropPoll shim for by-move async closures) - #160103 (Add regression test for GAT bound mismatched type error) - #160335 (dlopen offload) - #160445 (codegen: classify localized MSVC linker progress as linker_info) - #160499 (rustc_resolve: move diagnostic attribute linting to attr parsing) - #160504 (cleanup borrowck, improve c-variadic handling) - #160577 (expand: Feature gate AST-based attribute macros on expressions and statements) - #160587 (Add regression test for associated type outlives bound at call site) - #160625 (platform-support/netbsd.md: No longer mention 8.x, due to EoL.) - #160636 (derive(Diagnostic): link to proper docs) - #160644 (Clean up some manual debug impls)
…promotion, r=oli-obk,traviscross Do not promote extern statics We already reject thread-local statics during promotion, so extend that check to extern statics as well. fixesrust-lang#143174
…uwer Rollup of 28 pull requests Successful merges: - #159784 (Hint that memchr returns an in-bounds index) - #160673 (Improve `canonical_param_env_cache`) - #150885 (Revive L4Re target) - #159643 (Add support for splatted function pointers) - #160433 (delegation: add support for wrapping of the return value with `From::from`) - #160530 (refactor handling of target features in Session) - #160606 (bootstrap: Store and use an explicit CheckKind in `check::Rustc`) - #160628 (fix ICE in `suggest_add_reference_to_arg` for non-callable items) - #160683 (Add regression test for unknown feaeture name reported with other errors) - #157641 (Do not promote extern statics) - #158904 (Fix FutureDropPoll shim for by-move async closures) - #159816 (added note/help about iterator invalidation when mutating a collection inside a for loop) - #160103 (Add regression test for GAT bound mismatched type error) - #160335 (dlopen offload) - #160445 (codegen: classify localized MSVC linker progress as linker_info) - #160499 (rustc_resolve: move diagnostic attribute linting to attr parsing) - #160504 (cleanup borrowck, improve c-variadic handling) - #160577 (expand: Feature gate AST-based attribute macros on expressions and statements) - #160587 (Add regression test for associated type outlives bound at call site) - #160625 (platform-support/netbsd.md: No longer mention 8.x, due to EoL.) - #160633 (delegation: fix determining wrong `FnKind` when delegation is inside const arg) - #160636 (derive(Diagnostic): link to proper docs) - #160644 (Clean up some manual debug impls) - #160649 (move naked function ui tests) - #160672 (Improve `MaybeLiveLocals`) - #160693 (Add branch config for perf. unrolling in bors) - #160696 (rustc_codegen_llvm: handle sm_101* features being an alias) - #160706 (renovate: clarify that vulnerability PRs are opened automatically)
Uh oh!
There was an error while loading. Please reload this page.
…uwer Rollup of 28 pull requests Successful merges: - rust-lang/rust#159784 (Hint that memchr returns an in-bounds index) - rust-lang/rust#160673 (Improve `canonical_param_env_cache`) - rust-lang/rust#150885 (Revive L4Re target) - rust-lang/rust#159643 (Add support for splatted function pointers) - rust-lang/rust#160433 (delegation: add support for wrapping of the return value with `From::from`) - rust-lang/rust#160530 (refactor handling of target features in Session) - rust-lang/rust#160606 (bootstrap: Store and use an explicit CheckKind in `check::Rustc`) - rust-lang/rust#160628 (fix ICE in `suggest_add_reference_to_arg` for non-callable items) - rust-lang/rust#160683 (Add regression test for unknown feaeture name reported with other errors) - rust-lang/rust#157641 (Do not promote extern statics) - rust-lang/rust#158904 (Fix FutureDropPoll shim for by-move async closures) - rust-lang/rust#159816 (added note/help about iterator invalidation when mutating a collection inside a for loop) - rust-lang/rust#160103 (Add regression test for GAT bound mismatched type error) - rust-lang/rust#160335 (dlopen offload) - rust-lang/rust#160445 (codegen: classify localized MSVC linker progress as linker_info) - rust-lang/rust#160499 (rustc_resolve: move diagnostic attribute linting to attr parsing) - rust-lang/rust#160504 (cleanup borrowck, improve c-variadic handling) - rust-lang/rust#160577 (expand: Feature gate AST-based attribute macros on expressions and statements) - rust-lang/rust#160587 (Add regression test for associated type outlives bound at call site) - rust-lang/rust#160625 (platform-support/netbsd.md: No longer mention 8.x, due to EoL.) - rust-lang/rust#160633 (delegation: fix determining wrong `FnKind` when delegation is inside const arg) - rust-lang/rust#160636 (derive(Diagnostic): link to proper docs) - rust-lang/rust#160644 (Clean up some manual debug impls) - rust-lang/rust#160649 (move naked function ui tests) - rust-lang/rust#160672 (Improve `MaybeLiveLocals`) - rust-lang/rust#160693 (Add branch config for perf. unrolling in bors) - rust-lang/rust#160696 (rustc_codegen_llvm: handle sm_101* features being an alias) - rust-lang/rust#160706 (renovate: clarify that vulnerability PRs are opened automatically)
View all comments
Reference PR:
externstatics reference#2302We already reject thread-local statics during promotion, so extend that check to extern statics as well.
fixes#143174