Skip to content

Commit 5683791

Browse files
committed
Auto merge of rust-lang#112017 - Nemo157:unsafe-block-rustfix, r=eholk
Add MVP suggestion for `unsafe_op_in_unsafe_fn` Rebase of rust-lang#99827 cc tracking issue rust-lang#71668 No real changes since the original PR, just migrated the new suggestion to use fluent messages and added a couple more testcases, AFAICT from the discussion there were no outstanding changes requested.
2 parents 2ca8d35 + 802c1d5 commit 5683791

8 files changed

+292
-11
lines changed

compiler/rustc_mir_transform/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ mir_transform_unaligned_packed_ref = reference to packed field is unaligned
5555
mir_transform_union_access_label = access to union field
5656
mir_transform_union_access_note = the field may not be properly initialized: using uninitialized data will cause undefined behavior
5757
mir_transform_unsafe_op_in_unsafe_fn = {$details} is unsafe and requires unsafe block (error E0133)
58+
.suggestion = consider wrapping the function body in an unsafe block
59+
.note = an unsafe function restricts its caller, but its body is safe by default
5860
5961
mir_transform_unused_unsafe = unnecessary `unsafe` block
6062
.label = because it's nested under this `unsafe` block

compiler/rustc_mir_transform/src/check_unsafety.rs

+25-6
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,8 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: LocalDefId) {
525525
}
526526

527527
let UnsafetyCheckResult { violations, unused_unsafes, .. } = tcx.unsafety_check_result(def_id);
528+
// Only suggest wrapping the entire function body in an unsafe block once
529+
let mut suggest_unsafe_block = true;
528530

529531
for &UnsafetyViolation { source_info, lint_root, kind, details } in violations.iter() {
530532
let details = errors::RequiresUnsafeDetail { violation: details, span: source_info.span };
@@ -559,12 +561,29 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: LocalDefId) {
559561
op_in_unsafe_fn_allowed,
560562
});
561563
}
562-
UnsafetyViolationKind::UnsafeFn => tcx.emit_spanned_lint(
563-
UNSAFE_OP_IN_UNSAFE_FN,
564-
lint_root,
565-
source_info.span,
566-
errors::UnsafeOpInUnsafeFn { details },
567-
),
564+
UnsafetyViolationKind::UnsafeFn => {
565+
tcx.emit_spanned_lint(
566+
UNSAFE_OP_IN_UNSAFE_FN,
567+
lint_root,
568+
source_info.span,
569+
errors::UnsafeOpInUnsafeFn {
570+
details,
571+
suggest_unsafe_block: suggest_unsafe_block.then(|| {
572+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
573+
let fn_sig = tcx
574+
.hir()
575+
.fn_sig_by_hir_id(hir_id)
576+
.expect("this violation only occurs in fn");
577+
let body = tcx.hir().body_owned_by(def_id);
578+
let body_span = tcx.hir().body(body).value.span;
579+
let start = tcx.sess.source_map().start_point(body_span).shrink_to_hi();
580+
let end = tcx.sess.source_map().end_point(body_span).shrink_to_lo();
581+
(start, end, fn_sig.span)
582+
}),
583+
},
584+
);
585+
suggest_unsafe_block = false;
586+
}
568587
}
569588
}
570589

compiler/rustc_mir_transform/src/errors.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_errors::{
2-
DecorateLint, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Handler, IntoDiagnostic,
2+
Applicability, DecorateLint, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Handler,
3+
IntoDiagnostic,
34
};
45
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
56
use rustc_middle::mir::{AssertKind, UnsafetyViolationDetails};
@@ -130,6 +131,12 @@ impl RequiresUnsafeDetail {
130131

131132
pub(crate) struct UnsafeOpInUnsafeFn {
132133
pub details: RequiresUnsafeDetail,
134+
135+
/// These spans point to:
136+
/// 1. the start of the function body
137+
/// 2. the end of the function body
138+
/// 3. the function signature
139+
pub suggest_unsafe_block: Option<(Span, Span, Span)>,
133140
}
134141

135142
impl<'a> DecorateLint<'a, ()> for UnsafeOpInUnsafeFn {
@@ -138,13 +145,21 @@ impl<'a> DecorateLint<'a, ()> for UnsafeOpInUnsafeFn {
138145
self,
139146
diag: &'b mut DiagnosticBuilder<'a, ()>,
140147
) -> &'b mut DiagnosticBuilder<'a, ()> {
141-
let desc = diag
142-
.handler()
143-
.expect("lint should not yet be emitted")
144-
.eagerly_translate_to_string(self.details.label(), [].into_iter());
148+
let handler = diag.handler().expect("lint should not yet be emitted");
149+
let desc = handler.eagerly_translate_to_string(self.details.label(), [].into_iter());
145150
diag.set_arg("details", desc);
146151
diag.span_label(self.details.span, self.details.label());
147152
diag.note(self.details.note());
153+
154+
if let Some((start, end, fn_sig)) = self.suggest_unsafe_block {
155+
diag.span_note(fn_sig, crate::fluent_generated::mir_transform_note);
156+
diag.tool_only_multipart_suggestion(
157+
crate::fluent_generated::mir_transform_suggestion,
158+
vec![(start, " unsafe {".into()), (end, "}".into())],
159+
Applicability::MaybeIncorrect,
160+
);
161+
}
162+
148163
diag
149164
}
150165

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub unsafe fn unsf() {}
2+
3+
#[macro_export]
4+
macro_rules! unsafe_macro { () => ($crate::unsf()) }

tests/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.mir.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ LL | unsf();
55
| ^^^^^^ call to unsafe function
66
|
77
= note: consult the function's documentation for information on how to avoid undefined behavior
8+
note: an unsafe function restricts its caller, but its body is safe by default
9+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:11:1
10+
|
11+
LL | unsafe fn deny_level() {
12+
| ^^^^^^^^^^^^^^^^^^^^^^
813
note: the lint level is defined here
914
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:4:9
1015
|
@@ -46,6 +51,11 @@ LL | unsf();
4651
| ^^^^^^ call to unsafe function
4752
|
4853
= note: consult the function's documentation for information on how to avoid undefined behavior
54+
note: an unsafe function restricts its caller, but its body is safe by default
55+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:27:1
56+
|
57+
LL | unsafe fn warning_level() {
58+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
4959
note: the lint level is defined here
5060
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:26:8
5161
|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// run-rustfix
2+
// aux-build:external_unsafe_macro.rs
3+
4+
#![deny(unsafe_op_in_unsafe_fn)] //~ NOTE
5+
6+
extern crate external_unsafe_macro;
7+
8+
unsafe fn unsf() {}
9+
10+
pub unsafe fn foo() { unsafe {
11+
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
12+
unsf(); //~ ERROR call to unsafe function is unsafe
13+
//~^ NOTE
14+
//~| NOTE
15+
unsf(); //~ ERROR call to unsafe function is unsafe
16+
//~^ NOTE
17+
//~| NOTE
18+
}}
19+
20+
pub unsafe fn bar(x: *const i32) -> i32 { unsafe {
21+
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
22+
let y = *x; //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
23+
//~^ NOTE
24+
//~| NOTE
25+
y + *x //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
26+
//~^ NOTE
27+
//~| NOTE
28+
}}
29+
30+
static mut BAZ: i32 = 0;
31+
pub unsafe fn baz() -> i32 { unsafe {
32+
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
33+
let y = BAZ; //~ ERROR use of mutable static is unsafe and requires unsafe block
34+
//~^ NOTE
35+
//~| NOTE
36+
y + BAZ //~ ERROR use of mutable static is unsafe and requires unsafe block
37+
//~^ NOTE
38+
//~| NOTE
39+
}}
40+
41+
macro_rules! unsafe_macro { () => (unsf()) }
42+
//~^ ERROR call to unsafe function is unsafe
43+
//~| NOTE
44+
//~| NOTE
45+
//~| ERROR call to unsafe function is unsafe
46+
//~| NOTE
47+
//~| NOTE
48+
49+
pub unsafe fn unsafe_in_macro() { unsafe {
50+
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
51+
unsafe_macro!();
52+
//~^ NOTE
53+
//~| NOTE
54+
unsafe_macro!();
55+
//~^ NOTE
56+
//~| NOTE
57+
}}
58+
59+
pub unsafe fn unsafe_in_external_macro() {
60+
// FIXME: https://github.com/rust-lang/rust/issues/112504
61+
// FIXME: ~^ NOTE an unsafe function restricts its caller, but its body is safe by default
62+
external_unsafe_macro::unsafe_macro!();
63+
external_unsafe_macro::unsafe_macro!();
64+
}
65+
66+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// run-rustfix
2+
// aux-build:external_unsafe_macro.rs
3+
4+
#![deny(unsafe_op_in_unsafe_fn)] //~ NOTE
5+
6+
extern crate external_unsafe_macro;
7+
8+
unsafe fn unsf() {}
9+
10+
pub unsafe fn foo() {
11+
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
12+
unsf(); //~ ERROR call to unsafe function is unsafe
13+
//~^ NOTE
14+
//~| NOTE
15+
unsf(); //~ ERROR call to unsafe function is unsafe
16+
//~^ NOTE
17+
//~| NOTE
18+
}
19+
20+
pub unsafe fn bar(x: *const i32) -> i32 {
21+
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
22+
let y = *x; //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
23+
//~^ NOTE
24+
//~| NOTE
25+
y + *x //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
26+
//~^ NOTE
27+
//~| NOTE
28+
}
29+
30+
static mut BAZ: i32 = 0;
31+
pub unsafe fn baz() -> i32 {
32+
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
33+
let y = BAZ; //~ ERROR use of mutable static is unsafe and requires unsafe block
34+
//~^ NOTE
35+
//~| NOTE
36+
y + BAZ //~ ERROR use of mutable static is unsafe and requires unsafe block
37+
//~^ NOTE
38+
//~| NOTE
39+
}
40+
41+
macro_rules! unsafe_macro { () => (unsf()) }
42+
//~^ ERROR call to unsafe function is unsafe
43+
//~| NOTE
44+
//~| NOTE
45+
//~| ERROR call to unsafe function is unsafe
46+
//~| NOTE
47+
//~| NOTE
48+
49+
pub unsafe fn unsafe_in_macro() {
50+
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
51+
unsafe_macro!();
52+
//~^ NOTE
53+
//~| NOTE
54+
unsafe_macro!();
55+
//~^ NOTE
56+
//~| NOTE
57+
}
58+
59+
pub unsafe fn unsafe_in_external_macro() {
60+
// FIXME: https://github.com/rust-lang/rust/issues/112504
61+
// FIXME: ~^ NOTE an unsafe function restricts its caller, but its body is safe by default
62+
external_unsafe_macro::unsafe_macro!();
63+
external_unsafe_macro::unsafe_macro!();
64+
}
65+
66+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
2+
--> $DIR/wrapping-unsafe-block-sugg.rs:12:5
3+
|
4+
LL | unsf();
5+
| ^^^^^^ call to unsafe function
6+
|
7+
= note: consult the function's documentation for information on how to avoid undefined behavior
8+
note: an unsafe function restricts its caller, but its body is safe by default
9+
--> $DIR/wrapping-unsafe-block-sugg.rs:10:1
10+
|
11+
LL | pub unsafe fn foo() {
12+
| ^^^^^^^^^^^^^^^^^^^
13+
note: the lint level is defined here
14+
--> $DIR/wrapping-unsafe-block-sugg.rs:4:9
15+
|
16+
LL | #![deny(unsafe_op_in_unsafe_fn)]
17+
| ^^^^^^^^^^^^^^^^^^^^^^
18+
19+
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
20+
--> $DIR/wrapping-unsafe-block-sugg.rs:15:5
21+
|
22+
LL | unsf();
23+
| ^^^^^^ call to unsafe function
24+
|
25+
= note: consult the function's documentation for information on how to avoid undefined behavior
26+
27+
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
28+
--> $DIR/wrapping-unsafe-block-sugg.rs:22:13
29+
|
30+
LL | let y = *x;
31+
| ^^ dereference of raw pointer
32+
|
33+
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
34+
note: an unsafe function restricts its caller, but its body is safe by default
35+
--> $DIR/wrapping-unsafe-block-sugg.rs:20:1
36+
|
37+
LL | pub unsafe fn bar(x: *const i32) -> i32 {
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
40+
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
41+
--> $DIR/wrapping-unsafe-block-sugg.rs:25:9
42+
|
43+
LL | y + *x
44+
| ^^ dereference of raw pointer
45+
|
46+
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
47+
48+
error: use of mutable static is unsafe and requires unsafe block (error E0133)
49+
--> $DIR/wrapping-unsafe-block-sugg.rs:33:13
50+
|
51+
LL | let y = BAZ;
52+
| ^^^ use of mutable static
53+
|
54+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
55+
note: an unsafe function restricts its caller, but its body is safe by default
56+
--> $DIR/wrapping-unsafe-block-sugg.rs:31:1
57+
|
58+
LL | pub unsafe fn baz() -> i32 {
59+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
60+
61+
error: use of mutable static is unsafe and requires unsafe block (error E0133)
62+
--> $DIR/wrapping-unsafe-block-sugg.rs:36:9
63+
|
64+
LL | y + BAZ
65+
| ^^^ use of mutable static
66+
|
67+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
68+
69+
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
70+
--> $DIR/wrapping-unsafe-block-sugg.rs:41:36
71+
|
72+
LL | macro_rules! unsafe_macro { () => (unsf()) }
73+
| ^^^^^^ call to unsafe function
74+
...
75+
LL | unsafe_macro!();
76+
| --------------- in this macro invocation
77+
|
78+
= note: consult the function's documentation for information on how to avoid undefined behavior
79+
note: an unsafe function restricts its caller, but its body is safe by default
80+
--> $DIR/wrapping-unsafe-block-sugg.rs:49:1
81+
|
82+
LL | pub unsafe fn unsafe_in_macro() {
83+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
84+
= note: this error originates in the macro `unsafe_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
85+
86+
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
87+
--> $DIR/wrapping-unsafe-block-sugg.rs:41:36
88+
|
89+
LL | macro_rules! unsafe_macro { () => (unsf()) }
90+
| ^^^^^^ call to unsafe function
91+
...
92+
LL | unsafe_macro!();
93+
| --------------- in this macro invocation
94+
|
95+
= note: consult the function's documentation for information on how to avoid undefined behavior
96+
= note: this error originates in the macro `unsafe_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
97+
98+
error: aborting due to 8 previous errors
99+

0 commit comments

Comments
 (0)