Skip to content

Commit dc5247b

Browse files
Rollup merge of rust-lang#106157 - LeSeulArtichaut:106126-thir-unsafeck-good-path-bug-2, r=compiler-errors
Don't trim path for `unsafe_op_in_unsafe_fn` lints Fixes rust-lang#106126, alternative to rust-lang#106127. r? `@ghost` for now.
2 parents f6b0f47 + 2a7d559 commit dc5247b

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_middle::thir::visit::{self, Visitor};
55
use rustc_hir as hir;
66
use rustc_middle::mir::BorrowKind;
77
use rustc_middle::thir::*;
8+
use rustc_middle::ty::print::with_no_trimmed_paths;
89
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt};
910
use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE};
1011
use rustc_session::lint::Level;
@@ -524,17 +525,19 @@ impl UnsafeOpKind {
524525
hir_id: hir::HirId,
525526
span: Span,
526527
) {
528+
// FIXME: ideally we would want to trim the def paths, but this is not
529+
// feasible with the current lint emission API (see issue #106126).
527530
match self {
528-
CallToUnsafeFunction(did) if did.is_some() => tcx.emit_spanned_lint(
531+
CallToUnsafeFunction(Some(did)) => tcx.emit_spanned_lint(
529532
UNSAFE_OP_IN_UNSAFE_FN,
530533
hir_id,
531534
span,
532535
UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe {
533536
span,
534-
function: &tcx.def_path_str(did.unwrap()),
537+
function: &with_no_trimmed_paths!(tcx.def_path_str(*did)),
535538
},
536539
),
537-
CallToUnsafeFunction(..) => tcx.emit_spanned_lint(
540+
CallToUnsafeFunction(None) => tcx.emit_spanned_lint(
538541
UNSAFE_OP_IN_UNSAFE_FN,
539542
hir_id,
540543
span,
@@ -594,7 +597,7 @@ impl UnsafeOpKind {
594597
span,
595598
UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe {
596599
span,
597-
function: &tcx.def_path_str(*did),
600+
function: &with_no_trimmed_paths!(tcx.def_path_str(*did)),
598601
},
599602
),
600603
}
@@ -607,24 +610,24 @@ impl UnsafeOpKind {
607610
unsafe_op_in_unsafe_fn_allowed: bool,
608611
) {
609612
match self {
610-
CallToUnsafeFunction(did) if did.is_some() && unsafe_op_in_unsafe_fn_allowed => {
613+
CallToUnsafeFunction(Some(did)) if unsafe_op_in_unsafe_fn_allowed => {
611614
tcx.sess.emit_err(CallToUnsafeFunctionRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
612615
span,
613-
function: &tcx.def_path_str(did.unwrap()),
616+
function: &tcx.def_path_str(*did),
614617
});
615618
}
616-
CallToUnsafeFunction(did) if did.is_some() => {
619+
CallToUnsafeFunction(Some(did)) => {
617620
tcx.sess.emit_err(CallToUnsafeFunctionRequiresUnsafe {
618621
span,
619-
function: &tcx.def_path_str(did.unwrap()),
622+
function: &tcx.def_path_str(*did),
620623
});
621624
}
622-
CallToUnsafeFunction(..) if unsafe_op_in_unsafe_fn_allowed => {
625+
CallToUnsafeFunction(None) if unsafe_op_in_unsafe_fn_allowed => {
623626
tcx.sess.emit_err(
624627
CallToUnsafeFunctionRequiresUnsafeNamelessUnsafeOpInUnsafeFnAllowed { span },
625628
);
626629
}
627-
CallToUnsafeFunction(..) => {
630+
CallToUnsafeFunction(None) => {
628631
tcx.sess.emit_err(CallToUnsafeFunctionRequiresUnsafeNameless { span });
629632
}
630633
UseOfInlineAssembly if unsafe_op_in_unsafe_fn_allowed => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[macro_export]
2+
macro_rules! foo {
3+
() => {
4+
unsafe fn __unsf() {}
5+
unsafe fn __foo() {
6+
__unsf();
7+
}
8+
};
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Regression test for #106126.
2+
// check-pass
3+
// aux-build:issue-106126.rs
4+
5+
#![deny(unsafe_op_in_unsafe_fn)]
6+
7+
#[macro_use]
8+
extern crate issue_106126;
9+
10+
foo!();
11+
12+
fn main() {}

0 commit comments

Comments
 (0)