Skip to content

Commit

Permalink
Don't display function path for unsafe_op_in_unsafe_fn lints
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSeulArtichaut committed Dec 24, 2022
1 parent 14e2fe4 commit 2f739ec
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 68 deletions.
7 changes: 1 addition & 6 deletions compiler/rustc_error_messages/locales/en-US/mir_build.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ mir_build_unconditional_recursion = function cannot return without recursing
mir_build_unconditional_recursion_call_site_label = recursive call site
mir_build_unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe =
call to unsafe function `{$function}` is unsafe and requires unsafe block (error E0133)
.note = consult the function's documentation for information on how to avoid undefined behavior
.label = call to unsafe function
mir_build_unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe_nameless =
call to unsafe function is unsafe and requires unsafe block (error E0133)
.note = consult the function's documentation for information on how to avoid undefined behavior
.label = call to unsafe function
Expand Down Expand Up @@ -56,7 +51,7 @@ mir_build_unsafe_op_in_unsafe_fn_borrow_of_layout_constrained_field_requires_uns
.label = borrow of layout constrained field with interior mutability
mir_build_unsafe_op_in_unsafe_fn_call_to_fn_with_requires_unsafe =
call to function `{$function}` with `#[target_feature]` is unsafe and requires unsafe block (error E0133)
call to function with `#[target_feature]` is unsafe and requires unsafe block (error E0133)
.note = can only be called if the required target features are available
.label = call to function with `#[target_feature]`
Expand Down
37 changes: 15 additions & 22 deletions compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,20 +525,15 @@ impl UnsafeOpKind {
span: Span,
) {
match self {
CallToUnsafeFunction(did) if did.is_some() => tcx.emit_spanned_lint(
// The current linting API doesn't allow us to delay calling `def_path_str`
// until the lint is actually emitted, which risks triggering a "good path ICE"
// if the lint gets cancelled (for example, inside an external macro).
// For now, don't include the function name in the diagnostic.
CallToUnsafeFunction(_) => tcx.emit_spanned_lint(
UNSAFE_OP_IN_UNSAFE_FN,
hir_id,
span,
UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe {
span,
function: &tcx.def_path_str(did.unwrap()),
},
),
CallToUnsafeFunction(..) => tcx.emit_spanned_lint(
UNSAFE_OP_IN_UNSAFE_FN,
hir_id,
span,
UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless { span },
UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe { span },
),
UseOfInlineAssembly => tcx.emit_spanned_lint(
UNSAFE_OP_IN_UNSAFE_FN,
Expand Down Expand Up @@ -588,14 +583,12 @@ impl UnsafeOpKind {
span,
UnsafeOpInUnsafeFnBorrowOfLayoutConstrainedFieldRequiresUnsafe { span },
),
CallToFunctionWith(did) => tcx.emit_spanned_lint(
// We don't use the provided `DefId` for the same reason as `CallToUnsafeFunction` above.
CallToFunctionWith(_) => tcx.emit_spanned_lint(
UNSAFE_OP_IN_UNSAFE_FN,
hir_id,
span,
UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe {
span,
function: &tcx.def_path_str(*did),
},
UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe { span },
),
}
}
Expand All @@ -607,24 +600,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 => {
Expand Down
14 changes: 2 additions & 12 deletions compiler/rustc_mir_build/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,7 @@ pub struct UnconditionalRecursion {
#[derive(LintDiagnostic)]
#[diag(mir_build_unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe)]
#[note]
pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe<'a> {
#[label]
pub span: Span,
pub function: &'a str,
}

#[derive(LintDiagnostic)]
#[diag(mir_build_unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe_nameless)]
#[note]
pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless {
pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe {
#[label]
pub span: Span,
}
Expand Down Expand Up @@ -100,10 +91,9 @@ pub struct UnsafeOpInUnsafeFnBorrowOfLayoutConstrainedFieldRequiresUnsafe {
#[derive(LintDiagnostic)]
#[diag(mir_build_unsafe_op_in_unsafe_fn_call_to_fn_with_requires_unsafe)]
#[note]
pub struct UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe<'a> {
pub struct UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe {
#[label]
pub span: Span,
pub function: &'a str,
}

#[derive(Diagnostic)]
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/unsafe/auxiliary/issue-106126.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[macro_export]
macro_rules! foo {
() => {
unsafe fn __unsf() {}
unsafe fn __foo() {
__unsf();
}
};
}
12 changes: 12 additions & 0 deletions src/test/ui/unsafe/issue-106126-good-path-bug.rs
Original file line number Diff line number Diff line change
@@ -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() {}
22 changes: 11 additions & 11 deletions src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.mir.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ LL | #![deny(unsafe_op_in_unsafe_fn)]
| ^^^^^^^^^^^^^^^^^^^^^^

error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:15:5
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:14:5
|
LL | *PTR;
| ^^^^ dereference of raw pointer
|
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior

error: use of mutable static is unsafe and requires unsafe block (error E0133)
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:17:5
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:16:5
|
LL | VOID = ();
| ^^^^^^^^^ use of mutable static
|
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior

error: unnecessary `unsafe` block
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:20:5
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:19:5
|
LL | unsafe {}
| ^^^^^^ unnecessary `unsafe` block
Expand All @@ -40,57 +40,57 @@ LL | #![deny(unused_unsafe)]
| ^^^^^^^^^^^^^

error: call to unsafe function is unsafe and requires unsafe block (error E0133)
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:28:5
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:27:5
|
LL | unsf();
| ^^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior
note: the lint level is defined here
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:26:8
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:25:8
|
LL | #[deny(warnings)]
| ^^^^^^^^
= note: `#[deny(unsafe_op_in_unsafe_fn)]` implied by `#[deny(warnings)]`

error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:31:5
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:29:5
|
LL | *PTR;
| ^^^^ dereference of raw pointer
|
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior

error: use of mutable static is unsafe and requires unsafe block (error E0133)
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:33:5
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:31:5
|
LL | VOID = ();
| ^^^^^^^^^ use of mutable static
|
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior

error: unnecessary `unsafe` block
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:35:5
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:33:5
|
LL | unsafe {}
| ^^^^^^ unnecessary `unsafe` block

error: unnecessary `unsafe` block
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:49:5
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:47:5
|
LL | unsafe { unsafe { unsf() } }
| ^^^^^^ unnecessary `unsafe` block

error[E0133]: call to unsafe function is unsafe and requires unsafe block
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:76:5
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:74:5
|
LL | unsf();
| ^^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior

error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:81:9
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:79:9
|
LL | unsf();
| ^^^^^^ call to unsafe function
Expand Down
6 changes: 2 additions & 4 deletions src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ static mut VOID: () = ();

unsafe fn deny_level() {
unsf();
//[mir]~^ ERROR call to unsafe function is unsafe and requires unsafe block
//[thir]~^^ ERROR call to unsafe function `unsf` is unsafe and requires unsafe block
//~^ ERROR call to unsafe function is unsafe and requires unsafe block
*PTR;
//~^ ERROR dereference of raw pointer is unsafe and requires unsafe block
VOID = ();
Expand All @@ -26,8 +25,7 @@ unsafe fn deny_level() {
#[deny(warnings)]
unsafe fn warning_level() {
unsf();
//[mir]~^ ERROR call to unsafe function is unsafe and requires unsafe block
//[thir]~^^ ERROR call to unsafe function `unsf` is unsafe and requires unsafe block
//~^ ERROR call to unsafe function is unsafe and requires unsafe block
*PTR;
//~^ ERROR dereference of raw pointer is unsafe and requires unsafe block
VOID = ();
Expand Down
26 changes: 13 additions & 13 deletions src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.thir.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: call to unsafe function `unsf` is unsafe and requires unsafe block (error E0133)
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:12:5
|
LL | unsf();
Expand All @@ -12,23 +12,23 @@ LL | #![deny(unsafe_op_in_unsafe_fn)]
| ^^^^^^^^^^^^^^^^^^^^^^

error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:15:5
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:14:5
|
LL | *PTR;
| ^^^^ dereference of raw pointer
|
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior

error: use of mutable static is unsafe and requires unsafe block (error E0133)
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:17:5
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:16:5
|
LL | VOID = ();
| ^^^^ use of mutable static
|
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior

error: unnecessary `unsafe` block
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:20:5
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:19:5
|
LL | unsafe {}
| ^^^^^^ unnecessary `unsafe` block
Expand All @@ -39,60 +39,60 @@ note: the lint level is defined here
LL | #![deny(unused_unsafe)]
| ^^^^^^^^^^^^^

error: call to unsafe function `unsf` is unsafe and requires unsafe block (error E0133)
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:28:5
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:27:5
|
LL | unsf();
| ^^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior
note: the lint level is defined here
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:26:8
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:25:8
|
LL | #[deny(warnings)]
| ^^^^^^^^
= note: `#[deny(unsafe_op_in_unsafe_fn)]` implied by `#[deny(warnings)]`

error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:31:5
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:29:5
|
LL | *PTR;
| ^^^^ dereference of raw pointer
|
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior

error: use of mutable static is unsafe and requires unsafe block (error E0133)
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:33:5
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:31:5
|
LL | VOID = ();
| ^^^^ use of mutable static
|
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior

error: unnecessary `unsafe` block
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:35:5
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:33:5
|
LL | unsafe {}
| ^^^^^^ unnecessary `unsafe` block

error: unnecessary `unsafe` block
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:49:14
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:47:14
|
LL | unsafe { unsafe { unsf() } }
| ------ ^^^^^^ unnecessary `unsafe` block
| |
| because it's nested under this `unsafe` block

error[E0133]: call to unsafe function `unsf` is unsafe and requires unsafe block
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:76:5
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:74:5
|
LL | unsf();
| ^^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior

error[E0133]: call to unsafe function `unsf` is unsafe and requires unsafe function or block
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:81:9
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:79:9
|
LL | unsf();
| ^^^^^^ call to unsafe function
Expand Down

0 comments on commit 2f739ec

Please sign in to comment.