-
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
Open
samueltardieu
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
samueltardieu:issues/16076
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+307
−80
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,16 @@ | ||
| use clippy_utils::desugar_await; | ||
| use clippy_config::Conf; | ||
| use clippy_utils::diagnostics::span_lint_and_then; | ||
| use clippy_utils::visitors::{Descend, Visitable, for_each_expr}; | ||
| use core::ops::ControlFlow::Continue; | ||
| use clippy_utils::msrvs::Msrv; | ||
| use clippy_utils::{desugar_await, msrvs}; | ||
| use hir::def::{DefKind, Res}; | ||
| use hir::{BlockCheckMode, ExprKind, QPath, UnOp}; | ||
| use rustc_ast::Mutability; | ||
| use rustc_ast::{BorrowKind, Mutability}; | ||
| use rustc_hir as hir; | ||
| use rustc_hir::intravisit::{Visitor, walk_body, walk_expr}; | ||
| use rustc_lint::{LateContext, LateLintPass}; | ||
| use rustc_middle::ty; | ||
| use rustc_session::declare_lint_pass; | ||
| use rustc_middle::hir::nested_filter; | ||
| use rustc_middle::ty::{self, TypeckResults}; | ||
| use rustc_session::impl_lint_pass; | ||
| use rustc_span::{DesugaringKind, Span}; | ||
|
|
||
| declare_clippy_lint! { | ||
|
|
@@ -60,7 +62,18 @@ declare_clippy_lint! { | |
| restriction, | ||
| "more than one unsafe operation per `unsafe` block" | ||
| } | ||
| declare_lint_pass!(MultipleUnsafeOpsPerBlock => [MULTIPLE_UNSAFE_OPS_PER_BLOCK]); | ||
|
|
||
| pub struct MultipleUnsafeOpsPerBlock { | ||
| msrv: Msrv, | ||
| } | ||
|
|
||
| impl_lint_pass!(MultipleUnsafeOpsPerBlock => [MULTIPLE_UNSAFE_OPS_PER_BLOCK]); | ||
|
|
||
| impl MultipleUnsafeOpsPerBlock { | ||
| pub fn new(conf: &Conf) -> Self { | ||
| Self { msrv: conf.msrv } | ||
| } | ||
| } | ||
|
|
||
| impl<'tcx> LateLintPass<'tcx> for MultipleUnsafeOpsPerBlock { | ||
| fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx hir::Block<'_>) { | ||
|
|
@@ -70,8 +83,7 @@ impl<'tcx> LateLintPass<'tcx> for MultipleUnsafeOpsPerBlock { | |
| { | ||
| return; | ||
| } | ||
| let mut unsafe_ops = vec![]; | ||
| collect_unsafe_exprs(cx, block, &mut unsafe_ops); | ||
| let unsafe_ops = UnsafeExprCollector::collect_unsafe_exprs(cx, block, self.msrv); | ||
| if unsafe_ops.len() > 1 { | ||
| span_lint_and_then( | ||
| cx, | ||
|
|
@@ -91,25 +103,77 @@ impl<'tcx> LateLintPass<'tcx> for MultipleUnsafeOpsPerBlock { | |
| } | ||
| } | ||
|
|
||
| fn collect_unsafe_exprs<'tcx>( | ||
| cx: &LateContext<'tcx>, | ||
| node: impl Visitable<'tcx>, | ||
| unsafe_ops: &mut Vec<(&'static str, Span)>, | ||
| ) { | ||
| for_each_expr(cx, node, |expr| { | ||
| #[derive(Clone, Copy)] | ||
| enum UnderRawPtr { | ||
| /// The expression is not located under a raw pointer | ||
| No, | ||
| /// The expression is located under a raw pointer, MSRV yet unknown | ||
| Yes, | ||
| /// The expression is located under a raw pointer and MSRV has been determined. | ||
| /// `true` means that taking a raw pointer to a union field is a safe operation. | ||
| WithSafeMsrv(bool), | ||
| } | ||
|
|
||
| struct UnsafeExprCollector<'cx, 'tcx> { | ||
| cx: &'cx LateContext<'tcx>, | ||
| typeck_results: &'tcx TypeckResults<'tcx>, | ||
| msrv: Msrv, | ||
| unsafe_ops: Vec<(&'static str, Span)>, | ||
| under_raw_ptr: UnderRawPtr, | ||
| } | ||
|
|
||
| impl<'cx, 'tcx> UnsafeExprCollector<'cx, 'tcx> { | ||
| fn collect_unsafe_exprs( | ||
| cx: &'cx LateContext<'tcx>, | ||
| block: &'tcx hir::Block<'tcx>, | ||
| msrv: Msrv, | ||
| ) -> Vec<(&'static str, Span)> { | ||
| let mut collector = Self { | ||
| cx, | ||
| typeck_results: cx.typeck_results(), | ||
| msrv, | ||
| unsafe_ops: vec![], | ||
| under_raw_ptr: UnderRawPtr::No, | ||
| }; | ||
| collector.visit_block(block); | ||
| collector.unsafe_ops | ||
| } | ||
| } | ||
|
|
||
| impl<'tcx> Visitor<'tcx> for UnsafeExprCollector<'_, 'tcx> { | ||
| type NestedFilter = nested_filter::OnlyBodies; | ||
|
|
||
| fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { | ||
| // `self.under_raw_ptr` is preventively reset, while the current value is | ||
| // preserved in `under_raw_ptr`. | ||
| let under_raw_ptr = self.under_raw_ptr; | ||
| self.under_raw_ptr = UnderRawPtr::No; | ||
|
|
||
| match expr.kind { | ||
| // The `await` itself will desugar to two unsafe calls, but we should ignore those. | ||
| // Instead, check the expression that is `await`ed | ||
| _ if let Some(e) = desugar_await(expr) => { | ||
| collect_unsafe_exprs(cx, e, unsafe_ops); | ||
| return Continue(Descend::No); | ||
| return self.visit_expr(e); | ||
| }, | ||
|
|
||
| ExprKind::InlineAsm(_) => unsafe_ops.push(("inline assembly used here", expr.span)), | ||
| ExprKind::InlineAsm(_) => self.unsafe_ops.push(("inline assembly used here", expr.span)), | ||
|
|
||
| ExprKind::AddrOf(BorrowKind::Raw, _, _) => { | ||
| self.under_raw_ptr = UnderRawPtr::Yes; | ||
| }, | ||
|
|
||
| ExprKind::Field(e, _) => { | ||
| if cx.typeck_results().expr_ty(e).is_union() { | ||
| unsafe_ops.push(("union field access occurs here", expr.span)); | ||
| if self.typeck_results.expr_ty(e).is_union() { | ||
| // Restore `self.under_raw_pointer` and determine safety of taking a raw pointer to | ||
| // a union field if this is not known already. | ||
| self.under_raw_ptr = if matches!(under_raw_ptr, UnderRawPtr::Yes) { | ||
| UnderRawPtr::WithSafeMsrv(self.msrv.meets(self.cx, msrvs::SAFE_RAW_PTR_TO_UNION_FIELD)) | ||
| } else { | ||
| under_raw_ptr | ||
| }; | ||
| if matches!(self.under_raw_ptr, UnderRawPtr::No | UnderRawPtr::WithSafeMsrv(false)) { | ||
| self.unsafe_ops.push(("union field access occurs here", expr.span)); | ||
| } | ||
| } | ||
| }, | ||
|
|
||
|
|
@@ -127,32 +191,32 @@ fn collect_unsafe_exprs<'tcx>( | |
| .. | ||
| }, | ||
| )) => { | ||
| unsafe_ops.push(("access of a mutable static occurs here", expr.span)); | ||
| self.unsafe_ops | ||
| .push(("access of a mutable static occurs here", expr.span)); | ||
| }, | ||
|
|
||
| ExprKind::Unary(UnOp::Deref, e) if cx.typeck_results().expr_ty_adjusted(e).is_raw_ptr() => { | ||
| unsafe_ops.push(("raw pointer dereference occurs here", expr.span)); | ||
| ExprKind::Unary(UnOp::Deref, e) if self.typeck_results.expr_ty_adjusted(e).is_raw_ptr() => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be just |
||
| self.unsafe_ops.push(("raw pointer dereference occurs here", expr.span)); | ||
| }, | ||
|
|
||
| ExprKind::Call(path_expr, _) => { | ||
| let sig = match *cx.typeck_results().expr_ty(path_expr).kind() { | ||
| ty::FnDef(id, _) => cx.tcx.fn_sig(id).skip_binder(), | ||
| ty::FnPtr(sig_tys, hdr) => sig_tys.with(hdr), | ||
| _ => return Continue(Descend::Yes), | ||
| let opt_sig = match *self.typeck_results.expr_ty(path_expr).kind() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be |
||
| ty::FnDef(id, _) => Some(self.cx.tcx.fn_sig(id).skip_binder()), | ||
| ty::FnPtr(sig_tys, hdr) => Some(sig_tys.with(hdr)), | ||
| _ => None, | ||
| }; | ||
| if sig.safety().is_unsafe() { | ||
| unsafe_ops.push(("unsafe function call occurs here", expr.span)); | ||
| if opt_sig.is_some_and(|sig| sig.safety().is_unsafe()) { | ||
| self.unsafe_ops.push(("unsafe function call occurs here", expr.span)); | ||
| } | ||
| }, | ||
|
|
||
| ExprKind::MethodCall(..) => { | ||
| if let Some(sig) = cx | ||
| .typeck_results() | ||
| let opt_sig = self | ||
| .typeck_results | ||
| .type_dependent_def_id(expr.hir_id) | ||
| .map(|def_id| cx.tcx.fn_sig(def_id)) | ||
| && sig.skip_binder().safety().is_unsafe() | ||
| { | ||
| unsafe_ops.push(("unsafe method call occurs here", expr.span)); | ||
| .map(|def_id| self.cx.tcx.fn_sig(def_id)); | ||
| if opt_sig.is_some_and(|sig| sig.skip_binder().safety().is_unsafe()) { | ||
| self.unsafe_ops.push(("unsafe method call occurs here", expr.span)); | ||
| } | ||
| }, | ||
|
|
||
|
|
@@ -173,15 +237,26 @@ fn collect_unsafe_exprs<'tcx>( | |
| } | ||
| )) | ||
| ) { | ||
| unsafe_ops.push(("modification of a mutable static occurs here", expr.span)); | ||
| collect_unsafe_exprs(cx, rhs, unsafe_ops); | ||
| return Continue(Descend::No); | ||
| self.unsafe_ops | ||
| .push(("modification of a mutable static occurs here", expr.span)); | ||
| return self.visit_expr(rhs); | ||
| } | ||
| }, | ||
|
|
||
| _ => {}, | ||
| } | ||
|
|
||
| Continue::<(), _>(Descend::Yes) | ||
| }); | ||
| walk_expr(self, expr); | ||
| } | ||
|
|
||
| fn visit_body(&mut self, body: &hir::Body<'tcx>) { | ||
| let saved_typeck_results = self.typeck_results; | ||
| self.typeck_results = self.cx.tcx.typeck_body(body.id()); | ||
| walk_body(self, body); | ||
| self.typeck_results = saved_typeck_results; | ||
| } | ||
|
|
||
| fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { | ||
| self.cx.tcx | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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:
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
expectwill 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 anexpectthought it was better to keep it as a single block. Anybody who separates this into a new block will also get rustc'sunused_unsafewarning (they probably shouldn't). Who is actually in the set of people who actually want this MSRV dependent behaviour.Uh oh!
There was an error while loading. Please reload this page.
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.