-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Handle const-checks for &mut
outside of HasMutInterior
#66654
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
r? @eddyb (although I'd like to work on this a bit more, it's not quite ready) |
&mut
outside of HasMutInterior
&mut
outside of HasMutInterior
ba0ce7e
to
b40d0bd
Compare
☔ The latest upstream changes (presumably #66640) made this pull request unmergeable. Please resolve the merge conflicts. |
fn in_static(_cx: &ConstCx<'_, 'tcx>, _static: &Static<'tcx>) -> bool { | ||
// FIXME(eddyb) should we do anything here for value properties? | ||
false | ||
fn in_static(cx: &ConstCx<'_, 'tcx>, statik: &Static<'tcx>) -> bool { |
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.
(btw, a doc comment on these methods would be great)
.as_local() | ||
.map_or(false, |local| self.derived_from_illegal_borrow.contains(local)); | ||
|
||
// Don't emit errors for borrows of values that are *themselves* the result of |
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.
Can we delay_span_bug
just in case?
// Don't emit errors for borrows of values that are *themselves* the result of | ||
// an illegal borrow (e.g., the outermost `&` in `&&Cell::new(42)`). We want to | ||
// point the user to the place where the original illegal borrow occurred, not | ||
// to subsequent borrows of the resulting value. |
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.
I thought this was resolved by simply not having any qualifs on &Cell::new(42)
.
&T
is always Freeze + Copy
, regardless of T
, so no qualif bits need to be set.
The only reason they were before was for promotion, which is why we split promotion into its own check.
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.
I think you're right. This means I can clean up a lot more!
c93bb56
to
639aeec
Compare
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
☔ The latest upstream changes (presumably #66677) made this pull request unmergeable. Please resolve the merge conflicts. |
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.
r=me
639aeec
to
4c1f3b1
Compare
Now, `derived_from_illegal_borrow` is also applied to reborrows, so we don't show the user a useless error message.
4c1f3b1
to
7f35ab6
Compare
Now that #66640 has been merged, the const-checker treats borrows of a @matthewjasper I'm not quite sure why #66587 didn't cause this test to change. How do I check that |
I believe it didn't change in that PR because we don't special case reborrows in |
The |
This error code is never emitted, and the contents of this test are identical to that of `E0017.rs`.
7f35ab6
to
7673400
Compare
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
They now look the same in the MIR after rust-lang#66587.
This checks `static mut` as well for E0017, and blesses tests now that we emit an error for a mut deref.
7673400
to
846be82
Compare
@eddyb I had to make a few changes in |
if let LocalInfo::StaticRef { .. } = decl.local_info { | ||
return None; | ||
} | ||
} |
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.
@matthewjasper isn't this supposed to use a helper method?
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.
Yes, there's body.local_decls[local].is_ref_to_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.
@matthewjasper Resolved in ccb4eed.
| Rvalue::Ref(_, BorrowKind::Shared, ref place) | ||
| Rvalue::Ref(_, BorrowKind::Shallow, ref place) | ||
if matches!(place.base, PlaceBase::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.
Does this make sense anymore, with the static
changes?
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.
No, this shouldn't be reachable, since the only PlaceBase::Static
s are promoted. This maybe should be bug!()
for now.
r? @matthewjasper for all the |
@bors r=eddyb,matthewjasper |
📌 Commit ccb4eed has been approved by |
…eddyb,matthewjasper Handle const-checks for `&mut` outside of `HasMutInterior` Addresses [this comment](rust-lang#64470 (comment)). Const-checking relied on `HasMutInterior` to forbid `&mut` in a const context. This was strange because all we needed to do was look for an `Rvalue::Ref` with a certain `BorrowKind`, whereas the `Qualif` traits are specifically meant to get the qualifs for a *value*. This PR removes that logic from `HasMutInterior` and moves it into `check_consts::Validator`. As a result, we can now properly handle qualifications for `static`s, which had to be ignored previously since you can e.g. borrow a static `Cell` from another `static`. We also remove the `derived_from_illegal_borrow` logic, since it is no longer necessary; we give good errors for subsequent reborrows/borrows of illegal borrows.
Rollup of 5 pull requests Successful merges: - #66245 (Conditional compilation for sanitizers) - #66654 (Handle const-checks for `&mut` outside of `HasMutInterior`) - #66822 (libunwind_panic: adjust miri panic hack) - #66827 (handle diverging functions forwarding their return place) - #66834 (rustbuild fixes) Failed merges: r? @ghost
Addresses this comment.
Const-checking relied on
HasMutInterior
to forbid&mut
in a const context. This was strange because all we needed to do was look for anRvalue::Ref
with a certainBorrowKind
, whereas theQualif
traits are specifically meant to get the qualifs for a value. This PR removes that logic fromHasMutInterior
and moves it intocheck_consts::Validator
.As a result, we can now properly handle qualifications for
static
s, which had to be ignored previously since you can e.g. borrow a staticCell
from anotherstatic
. We also remove thederived_from_illegal_borrow
logic, since it is no longer necessary; we give good errors for subsequent reborrows/borrows of illegal borrows.