-
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
[mir] Special treatment for dereferencing a borrow to a static definition #74945
[mir] Special treatment for dereferencing a borrow to a static definition #74945
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @oli-obk (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. |
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.
cc @rust-lang/wg-mir-opt this PR uses self.temps[local]
, similarly to how we treat reborrows (&*foo
if foo
is a reference). I believe this is preferrable to the other possible solutions, namely:
- optimizing the MIR before promotion (and thus borrowck!) to convert
*FOO
toBAR
whereconst FOO: &'static T = &BAR;
. While I believe this optimization is strictly correct, doing such optimizations before borrowck seems dangerous - changing MIR building to not emit those constants. This is very hard, because when building the MIR for a
BAR
access, we don't know whether it's going to be for a reference or for a read.
@@ -0,0 +1,15 @@ | |||
// run-pass |
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.
This can just be // check-pass
, as the error message used to be emitted during borrowchecking.
Please leave a comment with a link to the issue and a word or two about why this test exists
I commented on this directly in the diff. I don't believe any problematic situations can occur today, but better safe than sorry.
That's a good place. We have loads of tests that do not emit errors. Since this is about promotion it could also live in |
@bors r+ Thanks! |
📌 Commit c511454 has been approved by |
☀️ Test successful - checks-actions, checks-azure |
cc @rust-lang/wg-const-eval (sorry for the late ping). This PR undoes a regression in implicit promotion within static items |
Ah I guess this is fallout from making pointers-to-statics into const pointer literals? Is there anything else that this promotes now that used to not get promoted? |
no, this just reverts that regression, it doesn't promote more things than were promoted before the regression was introduced |
Fix #70584.
As suggested by @oli-obk in this comment, one can chase the definition of the local variable being de-referenced and check if it is a true static variable. If that is the case,
validate_place
will admit the promotion.This is my first time to contribute to
rustc
, and I have two questions.validate_place
recursively decent into inner projection operations. I have put thoughts into its correctness but I am not totally sure about it.src/test/ui/mir
orsrc/test/ui/borrowck
because it does not generate errors while others do. It is tentatively placed insrc/test/ui/statics
for now.Thank you for any comments and suggestions!