From 2a7d5599706f127c1ebe6e5f992f9245d7f09b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lanteri=20Thauvin?= Date: Mon, 26 Dec 2022 18:49:19 +0100 Subject: [PATCH] Don't trim path for `unsafe_op_in_unsafe_fn` lints --- .../rustc_mir_build/src/check_unsafety.rs | 23 +++++++++++-------- src/test/ui/unsafe/auxiliary/issue-106126.rs | 9 ++++++++ .../ui/unsafe/issue-106126-good-path-bug.rs | 12 ++++++++++ 3 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 src/test/ui/unsafe/auxiliary/issue-106126.rs create mode 100644 src/test/ui/unsafe/issue-106126-good-path-bug.rs diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index 3c311729a52aa..03a7f2d70faeb 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -5,6 +5,7 @@ use rustc_middle::thir::visit::{self, Visitor}; use rustc_hir as hir; use rustc_middle::mir::BorrowKind; use rustc_middle::thir::*; +use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt}; use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE}; use rustc_session::lint::Level; @@ -524,17 +525,19 @@ impl UnsafeOpKind { hir_id: hir::HirId, span: Span, ) { + // FIXME: ideally we would want to trim the def paths, but this is not + // feasible with the current lint emission API (see issue #106126). match self { - CallToUnsafeFunction(did) if did.is_some() => tcx.emit_spanned_lint( + CallToUnsafeFunction(Some(did)) => tcx.emit_spanned_lint( UNSAFE_OP_IN_UNSAFE_FN, hir_id, span, UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe { span, - function: &tcx.def_path_str(did.unwrap()), + function: &with_no_trimmed_paths!(tcx.def_path_str(*did)), }, ), - CallToUnsafeFunction(..) => tcx.emit_spanned_lint( + CallToUnsafeFunction(None) => tcx.emit_spanned_lint( UNSAFE_OP_IN_UNSAFE_FN, hir_id, span, @@ -594,7 +597,7 @@ impl UnsafeOpKind { span, UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe { span, - function: &tcx.def_path_str(*did), + function: &with_no_trimmed_paths!(tcx.def_path_str(*did)), }, ), } @@ -607,24 +610,24 @@ impl UnsafeOpKind { unsafe_op_in_unsafe_fn_allowed: bool, ) { match self { - CallToUnsafeFunction(did) if did.is_some() && unsafe_op_in_unsafe_fn_allowed => { + CallToUnsafeFunction(Some(did)) if unsafe_op_in_unsafe_fn_allowed => { tcx.sess.emit_err(CallToUnsafeFunctionRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span, - function: &tcx.def_path_str(did.unwrap()), + function: &tcx.def_path_str(*did), }); } - CallToUnsafeFunction(did) if did.is_some() => { + CallToUnsafeFunction(Some(did)) => { tcx.sess.emit_err(CallToUnsafeFunctionRequiresUnsafe { span, - function: &tcx.def_path_str(did.unwrap()), + function: &tcx.def_path_str(*did), }); } - CallToUnsafeFunction(..) if unsafe_op_in_unsafe_fn_allowed => { + CallToUnsafeFunction(None) if unsafe_op_in_unsafe_fn_allowed => { tcx.sess.emit_err( CallToUnsafeFunctionRequiresUnsafeNamelessUnsafeOpInUnsafeFnAllowed { span }, ); } - CallToUnsafeFunction(..) => { + CallToUnsafeFunction(None) => { tcx.sess.emit_err(CallToUnsafeFunctionRequiresUnsafeNameless { span }); } UseOfInlineAssembly if unsafe_op_in_unsafe_fn_allowed => { diff --git a/src/test/ui/unsafe/auxiliary/issue-106126.rs b/src/test/ui/unsafe/auxiliary/issue-106126.rs new file mode 100644 index 0000000000000..091a3edb756ad --- /dev/null +++ b/src/test/ui/unsafe/auxiliary/issue-106126.rs @@ -0,0 +1,9 @@ +#[macro_export] +macro_rules! foo { + () => { + unsafe fn __unsf() {} + unsafe fn __foo() { + __unsf(); + } + }; +} diff --git a/src/test/ui/unsafe/issue-106126-good-path-bug.rs b/src/test/ui/unsafe/issue-106126-good-path-bug.rs new file mode 100644 index 0000000000000..93f478ee35802 --- /dev/null +++ b/src/test/ui/unsafe/issue-106126-good-path-bug.rs @@ -0,0 +1,12 @@ +// Regression test for #106126. +// check-pass +// aux-build:issue-106126.rs + +#![deny(unsafe_op_in_unsafe_fn)] + +#[macro_use] +extern crate issue_106126; + +foo!(); + +fn main() {}