Skip to content

Commit 2f739ec

Browse files
Don't display function path for unsafe_op_in_unsafe_fn lints
1 parent 14e2fe4 commit 2f739ec

File tree

8 files changed

+65
-68
lines changed

8 files changed

+65
-68
lines changed

compiler/rustc_error_messages/locales/en-US/mir_build.ftl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ mir_build_unconditional_recursion = function cannot return without recursing
55
mir_build_unconditional_recursion_call_site_label = recursive call site
66
77
mir_build_unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe =
8-
call to unsafe function `{$function}` is unsafe and requires unsafe block (error E0133)
9-
.note = consult the function's documentation for information on how to avoid undefined behavior
10-
.label = call to unsafe function
11-
12-
mir_build_unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe_nameless =
138
call to unsafe function is unsafe and requires unsafe block (error E0133)
149
.note = consult the function's documentation for information on how to avoid undefined behavior
1510
.label = call to unsafe function
@@ -56,7 +51,7 @@ mir_build_unsafe_op_in_unsafe_fn_borrow_of_layout_constrained_field_requires_uns
5651
.label = borrow of layout constrained field with interior mutability
5752
5853
mir_build_unsafe_op_in_unsafe_fn_call_to_fn_with_requires_unsafe =
59-
call to function `{$function}` with `#[target_feature]` is unsafe and requires unsafe block (error E0133)
54+
call to function with `#[target_feature]` is unsafe and requires unsafe block (error E0133)
6055
.note = can only be called if the required target features are available
6156
.label = call to function with `#[target_feature]`
6257

compiler/rustc_mir_build/src/check_unsafety.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -525,20 +525,15 @@ impl UnsafeOpKind {
525525
span: Span,
526526
) {
527527
match self {
528-
CallToUnsafeFunction(did) if did.is_some() => tcx.emit_spanned_lint(
528+
// The current linting API doesn't allow us to delay calling `def_path_str`
529+
// until the lint is actually emitted, which risks triggering a "good path ICE"
530+
// if the lint gets cancelled (for example, inside an external macro).
531+
// For now, don't include the function name in the diagnostic.
532+
CallToUnsafeFunction(_) => tcx.emit_spanned_lint(
529533
UNSAFE_OP_IN_UNSAFE_FN,
530534
hir_id,
531535
span,
532-
UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe {
533-
span,
534-
function: &tcx.def_path_str(did.unwrap()),
535-
},
536-
),
537-
CallToUnsafeFunction(..) => tcx.emit_spanned_lint(
538-
UNSAFE_OP_IN_UNSAFE_FN,
539-
hir_id,
540-
span,
541-
UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless { span },
536+
UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe { span },
542537
),
543538
UseOfInlineAssembly => tcx.emit_spanned_lint(
544539
UNSAFE_OP_IN_UNSAFE_FN,
@@ -588,14 +583,12 @@ impl UnsafeOpKind {
588583
span,
589584
UnsafeOpInUnsafeFnBorrowOfLayoutConstrainedFieldRequiresUnsafe { span },
590585
),
591-
CallToFunctionWith(did) => tcx.emit_spanned_lint(
586+
// We don't use the provided `DefId` for the same reason as `CallToUnsafeFunction` above.
587+
CallToFunctionWith(_) => tcx.emit_spanned_lint(
592588
UNSAFE_OP_IN_UNSAFE_FN,
593589
hir_id,
594590
span,
595-
UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe {
596-
span,
597-
function: &tcx.def_path_str(*did),
598-
},
591+
UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe { span },
599592
),
600593
}
601594
}
@@ -607,24 +600,24 @@ impl UnsafeOpKind {
607600
unsafe_op_in_unsafe_fn_allowed: bool,
608601
) {
609602
match self {
610-
CallToUnsafeFunction(did) if did.is_some() && unsafe_op_in_unsafe_fn_allowed => {
603+
CallToUnsafeFunction(Some(did)) if unsafe_op_in_unsafe_fn_allowed => {
611604
tcx.sess.emit_err(CallToUnsafeFunctionRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
612605
span,
613-
function: &tcx.def_path_str(did.unwrap()),
606+
function: &tcx.def_path_str(*did),
614607
});
615608
}
616-
CallToUnsafeFunction(did) if did.is_some() => {
609+
CallToUnsafeFunction(Some(did)) => {
617610
tcx.sess.emit_err(CallToUnsafeFunctionRequiresUnsafe {
618611
span,
619-
function: &tcx.def_path_str(did.unwrap()),
612+
function: &tcx.def_path_str(*did),
620613
});
621614
}
622-
CallToUnsafeFunction(..) if unsafe_op_in_unsafe_fn_allowed => {
615+
CallToUnsafeFunction(None) if unsafe_op_in_unsafe_fn_allowed => {
623616
tcx.sess.emit_err(
624617
CallToUnsafeFunctionRequiresUnsafeNamelessUnsafeOpInUnsafeFnAllowed { span },
625618
);
626619
}
627-
CallToUnsafeFunction(..) => {
620+
CallToUnsafeFunction(None) => {
628621
tcx.sess.emit_err(CallToUnsafeFunctionRequiresUnsafeNameless { span });
629622
}
630623
UseOfInlineAssembly if unsafe_op_in_unsafe_fn_allowed => {

compiler/rustc_mir_build/src/errors.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,7 @@ pub struct UnconditionalRecursion {
2020
#[derive(LintDiagnostic)]
2121
#[diag(mir_build_unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe)]
2222
#[note]
23-
pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe<'a> {
24-
#[label]
25-
pub span: Span,
26-
pub function: &'a str,
27-
}
28-
29-
#[derive(LintDiagnostic)]
30-
#[diag(mir_build_unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe_nameless)]
31-
#[note]
32-
pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless {
23+
pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe {
3324
#[label]
3425
pub span: Span,
3526
}
@@ -100,10 +91,9 @@ pub struct UnsafeOpInUnsafeFnBorrowOfLayoutConstrainedFieldRequiresUnsafe {
10091
#[derive(LintDiagnostic)]
10192
#[diag(mir_build_unsafe_op_in_unsafe_fn_call_to_fn_with_requires_unsafe)]
10293
#[note]
103-
pub struct UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe<'a> {
94+
pub struct UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe {
10495
#[label]
10596
pub span: Span,
106-
pub function: &'a str,
10797
}
10898

10999
#[derive(Diagnostic)]
Lines changed: 9 additions & 0 deletions
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+
}
Lines changed: 12 additions & 0 deletions
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() {}

src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.mir.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ LL | #![deny(unsafe_op_in_unsafe_fn)]
1212
| ^^^^^^^^^^^^^^^^^^^^^^
1313

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

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

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

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

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

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

7272
error: unnecessary `unsafe` block
73-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:35:5
73+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:33:5
7474
|
7575
LL | unsafe {}
7676
| ^^^^^^ unnecessary `unsafe` block
7777

7878
error: unnecessary `unsafe` block
79-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:49:5
79+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:47:5
8080
|
8181
LL | unsafe { unsafe { unsf() } }
8282
| ^^^^^^ unnecessary `unsafe` block
8383

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

9292
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
93-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:81:9
93+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:79:9
9494
|
9595
LL | unsf();
9696
| ^^^^^^ call to unsafe function

src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ static mut VOID: () = ();
1010

1111
unsafe fn deny_level() {
1212
unsf();
13-
//[mir]~^ ERROR call to unsafe function is unsafe and requires unsafe block
14-
//[thir]~^^ ERROR call to unsafe function `unsf` is unsafe and requires unsafe block
13+
//~^ ERROR call to unsafe function is unsafe and requires unsafe block
1514
*PTR;
1615
//~^ ERROR dereference of raw pointer is unsafe and requires unsafe block
1716
VOID = ();
@@ -26,8 +25,7 @@ unsafe fn deny_level() {
2625
#[deny(warnings)]
2726
unsafe fn warning_level() {
2827
unsf();
29-
//[mir]~^ ERROR call to unsafe function is unsafe and requires unsafe block
30-
//[thir]~^^ ERROR call to unsafe function `unsf` is unsafe and requires unsafe block
28+
//~^ ERROR call to unsafe function is unsafe and requires unsafe block
3129
*PTR;
3230
//~^ ERROR dereference of raw pointer is unsafe and requires unsafe block
3331
VOID = ();

src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.thir.stderr

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: call to unsafe function `unsf` is unsafe and requires unsafe block (error E0133)
1+
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
22
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:12:5
33
|
44
LL | unsf();
@@ -12,23 +12,23 @@ LL | #![deny(unsafe_op_in_unsafe_fn)]
1212
| ^^^^^^^^^^^^^^^^^^^^^^
1313

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

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

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

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

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

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

7272
error: unnecessary `unsafe` block
73-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:35:5
73+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:33:5
7474
|
7575
LL | unsafe {}
7676
| ^^^^^^ unnecessary `unsafe` block
7777

7878
error: unnecessary `unsafe` block
79-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:49:14
79+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:47:14
8080
|
8181
LL | unsafe { unsafe { unsf() } }
8282
| ------ ^^^^^^ unnecessary `unsafe` block
8383
| |
8484
| because it's nested under this `unsafe` block
8585

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

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

0 commit comments

Comments
 (0)