Do not promote extern statics - #157641
Conversation
|
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
|
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
80ac224
Compare
This comment has been minimized.
This comment has been minimized.
80ac224 to
8877f4c
Compare
This comment has been minimized.
This comment has been minimized.
|
Some changes occurred to the CTFE machinery Some changes occurred to constck cc @fee1-dead |
|
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? |
|
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 ( |
|
We already avoid promotion in this: fn main() { unsafe {
let f: &Foo = &Foo(BAR);
} }Why do we promote in the other case? |
|
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" {
static X: i32;
}
static mut FOO: *const &i32 = [unsafe { &X }].as_ptr();gets lowered to after promotion, and since extern "C" {
static BAR: i32;
}
static FOO: &(i32,) = unsafe { &(BAR,) };gets lowered to where the |
|
Yeah I think we should reject such promotion (unless crater disagrees). People can use |
|
Cool, let's try that then and crater it. Seems very niche |
168eebd to
3e8d36a
Compare
|
@rfcbot reviewed |
3e8d36a to
9ac2606
Compare
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.
|
Reference PR is here: rust-lang/reference#2302 |
|
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: |
…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. fixes rust-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. fixes rust-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)
View all comments
We already reject thread-local statics during promotion, so extend that check to extern statics as well.
fixes #143174