-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Taking a raw pointer on a union field is a safe operation #16079
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
base: master
Are you sure you want to change the base?
Conversation
|
You should switch away from We also need to do decide on what to do with the MSRV here since this changed as of |
a548e5a to
16bc237
Compare
|
Doing it with a |
16bc237 to
a728e87
Compare
This is only true as of |
|
Yes, I'm testing the MSRV check right now. |
|
|
It will be counted as one unsafe operation starting from Rust 1.92 (because of I think we should beta-nominate this PR to avoid a new false positive in Rust 1.92, while fixing the fact that |
|
That was in reference to your comment about it being unlikely to appear in an unsafe block. |
3a8c121 to
54a5bb4
Compare
|
@Jarcho This convinced me. I switched the code to a visitor, and determine the MSRV only when we detect a union field access under a raw pointer (and possible other fields accesses). |
|
No changes for ae5274d |
|
@rustbot note Beta nomination This issue fixes a regression in the However, this happens in a @rustbot label beta-nominated |
ec28694 to
6dd5b0c
Compare
|
Before merging, do we really want to take the MSRV into account for this? It's an operation that was unsafe as an oversight, not something that is unsafe. Given that the intent of the lint is to essentially mark each unsafe operation, requiring this to be marked separately means the safety comment it would get is basically Basically this complicates the code and it doesn't seem to benefit anybody. |
| ExprKind::AddrOf(BorrowKind::Raw, _, _) => { | ||
| self.under_raw_ptr = UnderRawPtr::Yes; | ||
| }, |
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.
Without the MSRV handling this could be:
ExprKind::AddrOf(BorrowKind::Raw, _, mut e) => {
while let ExprKind::Field(base, _) = e.kind
&& self.typeck_results.expr_adjustments(base).is_empty() // No deref
{ e = base; }
return self.visit_expr(e);
}This removes all the complexity of Self::under_raw_ptr.
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.
Sure, but with Rust < 1.92 we would not flag multiple unsafe operations in the block as the lint indicates, while not letting the user get the ref to union field outside of the unsafe block because Rust won't allow it. That would be a regression in itself, as it could break some #[expect] that would have been placed on the block/function.
The MSRV logic is about 5 lines long + declarations in order to implement some caching, so it is neither complex nor particularly inefficient.
But I don't have a strong opinion about it, I'll let @y21 weigh in.
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.
We don't guarantee expect will continue to trigger when updating so that's not, on it's own, a regression. Not taking the MSRV doesn't inconvenience anyone who already split the blocks, and anyone with an expect thought it was better to keep it as a single block. Anybody who separates this into a new block will also get rustc's unused_unsafe warning (they probably shouldn't). Who is actually in the set of people who actually want this MSRV dependent behaviour.
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 wasn't trying to say the logic is particularly complex in this case. It isn't, but it is additional logic that seems to benefit nobody.
6dd5b0c to
2c73f92
Compare
|
Since it was easy to do that (thanks to jj), I've separated the PR into two commits:
@y21 We can either keep both commits or just the first one if you prefer not to have proper MSRV handling. |
2c73f92 to
ae5274d
Compare
|
☔ The latest upstream changes (possibly c48592e) made this pull request unmergeable. Please resolve the merge conflicts. |
changelog: [
multiple_unsafe_ops_per_block]: taking a raw pointer on a union field is a safe operationFixes #16076
Summary Notes
Managed by
@rustbot—see help for details