-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix regression in promotion of rvalues referencing a static #44442
Conversation
This commit makes librustc_passes::consts::CheckCrateVisitor properly mark expressions as promotable if they reference a static, as it's perfectly fine for one static to reference another. It fixes a regression that prevented a temporary rvalue from referencing a static if it was itself declared within a static. Prior to commit rust-lang@b8c05fe90bc, `region::ScopeTree` would only register a 'terminating scope' for function bodies. Thus, while rvalues in a static that referenced a static would be marked unpromotable, the lack of enclosing scope would cause mem_categorization::MemCategorizationContext::cat_rvalue_node to compute a 'temporary scope' of `ReStatic`. Since this had the same effect as explicitly selecting a scope of `ReStatic` due to the rvalue being marked by CheckCrateVisitor as promotable, no issue occurred. However, commit rust-lang@b8c05fe90bc made ScopeTree unconditionally register a 'terminating scope' Since mem_categorization would now compute a non-static 'temporary scope', the aforementioned rvalues would be erroneously marked as living for too short a time. By fixing the behavior of CheckCrateVisitor, this commit avoids changing mem_categorization's behavior, while ensuring that temporary values in statics are still allowed to reference other statics. Fixes issue rust-lang#44373
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @arielb1 (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
r? @eddyb |
Ah, changing the MIR side like I suggested in the issue isn't enough. Still, you should do it. |
src/librustc_passes/consts.rs
Outdated
if !thread_local { | ||
debug!("Allowing promotion of reference to Static(id={:?})", did); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this will allow promoting reads from statics, which is bad. Can you make sure to only promote if the mode is Static
? i.e. only a static
can refer to another static
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, wait, this is old borrowck, how is this even a problem at all? I need to investigate further. Realized [&TEST]
is what doesn't get promoted in your original testcase, so this makes sense, but, again, it should be limited to static
s referring to other static
s.
Hopefully the MIR constant checking will prevent edge cases from compiling.
@eddyb: Done |
@@ -502,6 +517,7 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) { | |||
tcx, | |||
tables: &ty::TypeckTables::empty(None), | |||
in_fn: false, | |||
in_static: false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, there's no mode anymore, I guess this is fine.
@eddyb: It looks like the MIR-side change you suggested broke a few tests. While it looks like it should be possible to refactor some of the constant-related code to fix them, I think it would be simpler to leave the PR as-is. The original regression was caused by improper logic at the HIR level, so I think it would be better to fix that before making additional changes to the MIR logic. |
First off, you should not leave revert commits around but rather remove the commit (e.g. As for the errors, it's very important to note that they result in promotion of Anyway, I'll warn @oli-obk that your fix and new test might break miri - and of course, it will break the new MIR borrowck but that is not on by default yet, and I'll investigate |
a60f6ce
to
813b323
Compare
@bors r+ Thanks! |
📌 Commit 813b323 has been approved by |
src/librustc_passes/consts.rs
Outdated
Def::Fn(..) | Def::Method(..) => {} | ||
Def::Fn(..) | Def::Method(..) => {} | ||
|
||
// References to a static are inherently promotable, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment doesn't match code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
References to statics outside of a static aren't promotable.
@bors r- Please have the comment match the code |
@arielb1: Done |
@bors r+ |
📌 Commit fb540e3 has been approved by |
Fix regression in promotion of rvalues referencing a static This commit makes librustc_passes::consts::CheckCrateVisitor properly mark expressions as promotable if they reference a static, as it's perfectly fine for one static to reference another. It fixes a regression that prevented a temporary rvalue from referencing a static if it was itself declared within a static. Prior to commit b8c05fe90bc, `region::ScopeTree` would only register a 'terminating scope' for function bodies. Thus, while rvalues in a static that referenced a static would be marked unpromotable, the lack of enclosing scope would cause mem_categorization::MemCategorizationContext::cat_rvalue_node to compute a 'temporary scope' of `ReStatic`. Since this had the same effect as explicitly selecting a scope of `ReStatic` due to the rvalue being marked by CheckCrateVisitor as promotable, no issue occurred. However, commit b8c05fe90bc made ScopeTree unconditionally register a 'terminating scope' Since mem_categorization would now compute a non-static 'temporary scope', the aforementioned rvalues would be erroneously marked as living for too short a time. By fixing the behavior of CheckCrateVisitor, this commit avoids changing mem_categorization's behavior, while ensuring that temporary values in statics are still allowed to reference other statics. Fixes issue #44373
☀️ Test successful - status-appveyor, status-travis |
This commit makes librustc_passes::consts::CheckCrateVisitor properly
mark expressions as promotable if they reference a static, as it's
perfectly fine for one static to reference another. It fixes a
regression that prevented a temporary rvalue from referencing a static
if it was itself declared within a static.
Prior to commit b8c05fe90bc,
region::ScopeTree
would only register a 'terminating scope' for functionbodies. Thus, while rvalues in a static that referenced a static would be marked
unpromotable, the lack of enclosing scope would cause
mem_categorization::MemCategorizationContext::cat_rvalue_node
to compute a 'temporary scope' of
ReStatic
. Since this had the sameeffect as explicitly selecting a scope of
ReStatic
due to the rvalue being marked by CheckCrateVisitor as promotable,
no issue occurred.
However, commit b8c05fe90bc
made ScopeTree unconditionally register a 'terminating scope'
Since mem_categorization would now compute a non-static 'temporary scope', the
aforementioned rvalues would be erroneously marked as living for too
short a time.
By fixing the behavior of CheckCrateVisitor, this commit avoids changing
mem_categorization's behavior, while ensuring that temporary values in
statics are still allowed to reference other statics.
Fixes issue #44373