Skip to content

Commit 8f3e876

Browse files
committed
Add note about unsafe functions body not being unsafe
1 parent 62a712a commit 8f3e876

7 files changed

+78
-11
lines changed

compiler/rustc_mir_transform/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ 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)
5858
.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
5960
6061
mir_transform_unused_unsafe = unnecessary `unsafe` block
6162
.label = because it's nested under this `unsafe` block

compiler/rustc_mir_transform/src/check_unsafety.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -571,11 +571,16 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: LocalDefId) {
571571
errors::UnsafeOpInUnsafeFn {
572572
details,
573573
suggest_unsafe_block: suggest_unsafe_block.then(|| {
574+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
575+
let fn_sig = tcx
576+
.hir()
577+
.fn_sig_by_hir_id(hir_id)
578+
.expect("this violation only occurs in fn");
574579
let body = tcx.hir().body_owned_by(def_id);
575580
let body_span = tcx.hir().body(body).value.span;
576581
let start = tcx.sess.source_map().start_point(body_span).shrink_to_hi();
577582
let end = tcx.sess.source_map().end_point(body_span).shrink_to_lo();
578-
(start, end)
583+
(start, end, fn_sig.span)
579584
}),
580585
},
581586
);

compiler/rustc_mir_transform/src/errors.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,12 @@ impl RequiresUnsafeDetail {
131131

132132
pub(crate) struct UnsafeOpInUnsafeFn {
133133
pub details: RequiresUnsafeDetail,
134-
pub suggest_unsafe_block: Option<(Span, Span)>,
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)>,
135140
}
136141

137142
impl<'a> DecorateLint<'a, ()> for UnsafeOpInUnsafeFn {
@@ -146,7 +151,8 @@ impl<'a> DecorateLint<'a, ()> for UnsafeOpInUnsafeFn {
146151
diag.span_label(self.details.span, self.details.label());
147152
diag.note(self.details.note());
148153

149-
if let Some((start, end)) = self.suggest_unsafe_block {
154+
if let Some((start, end, fn_sig)) = self.suggest_unsafe_block {
155+
diag.span_note(fn_sig, crate::fluent_generated::mir_transform_note);
150156
diag.tool_only_multipart_suggestion(
151157
crate::fluent_generated::mir_transform_suggestion,
152158
vec![(start, " unsafe {".into()), (end, "}".into())],

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
@@ -1,23 +1,38 @@
11
// run-rustfix
22

3-
#![deny(unsafe_op_in_unsafe_fn)]
3+
#![deny(unsafe_op_in_unsafe_fn)] //~ NOTE
44

55
unsafe fn unsf() {}
66

77
pub unsafe fn foo() { unsafe {
8+
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
89
unsf(); //~ ERROR call to unsafe function is unsafe
10+
//~^ NOTE
11+
//~| NOTE
912
unsf(); //~ ERROR call to unsafe function is unsafe
13+
//~^ NOTE
14+
//~| NOTE
1015
}}
1116

1217
pub unsafe fn bar(x: *const i32) -> i32 { unsafe {
18+
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
1319
let y = *x; //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
20+
//~^ NOTE
21+
//~| NOTE
1422
y + *x //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
23+
//~^ NOTE
24+
//~| NOTE
1525
}}
1626

1727
static mut BAZ: i32 = 0;
1828
pub unsafe fn baz() -> i32 { unsafe {
29+
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
1930
let y = BAZ; //~ ERROR use of mutable static is unsafe and requires unsafe block
31+
//~^ NOTE
32+
//~| NOTE
2033
y + BAZ //~ ERROR use of mutable static is unsafe and requires unsafe block
34+
//~^ NOTE
35+
//~| NOTE
2136
}}
2237

2338
fn main() {}
+16-1
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
11
// run-rustfix
22

3-
#![deny(unsafe_op_in_unsafe_fn)]
3+
#![deny(unsafe_op_in_unsafe_fn)] //~ NOTE
44

55
unsafe fn unsf() {}
66

77
pub unsafe fn foo() {
8+
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
89
unsf(); //~ ERROR call to unsafe function is unsafe
10+
//~^ NOTE
11+
//~| NOTE
912
unsf(); //~ ERROR call to unsafe function is unsafe
13+
//~^ NOTE
14+
//~| NOTE
1015
}
1116

1217
pub unsafe fn bar(x: *const i32) -> i32 {
18+
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
1319
let y = *x; //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
20+
//~^ NOTE
21+
//~| NOTE
1422
y + *x //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
23+
//~^ NOTE
24+
//~| NOTE
1525
}
1626

1727
static mut BAZ: i32 = 0;
1828
pub unsafe fn baz() -> i32 {
29+
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
1930
let y = BAZ; //~ ERROR use of mutable static is unsafe and requires unsafe block
31+
//~^ NOTE
32+
//~| NOTE
2033
y + BAZ //~ ERROR use of mutable static is unsafe and requires unsafe block
34+
//~^ NOTE
35+
//~| NOTE
2136
}
2237

2338
fn main() {}

tests/ui/unsafe/wrapping-unsafe-block-sugg.stderr

+21-6
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,65 @@
11
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
2-
--> $DIR/wrapping-unsafe-block-sugg.rs:8:5
2+
--> $DIR/wrapping-unsafe-block-sugg.rs:9:5
33
|
44
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/wrapping-unsafe-block-sugg.rs:7:1
10+
|
11+
LL | pub unsafe fn foo() {
12+
| ^^^^^^^^^^^^^^^^^^^
813
note: the lint level is defined here
914
--> $DIR/wrapping-unsafe-block-sugg.rs:3:9
1015
|
1116
LL | #![deny(unsafe_op_in_unsafe_fn)]
1217
| ^^^^^^^^^^^^^^^^^^^^^^
1318

1419
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
15-
--> $DIR/wrapping-unsafe-block-sugg.rs:9:5
20+
--> $DIR/wrapping-unsafe-block-sugg.rs:12:5
1621
|
1722
LL | unsf();
1823
| ^^^^^^ call to unsafe function
1924
|
2025
= note: consult the function's documentation for information on how to avoid undefined behavior
2126

2227
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
23-
--> $DIR/wrapping-unsafe-block-sugg.rs:13:13
28+
--> $DIR/wrapping-unsafe-block-sugg.rs:19:13
2429
|
2530
LL | let y = *x;
2631
| ^^ dereference of raw pointer
2732
|
2833
= 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:17:1
36+
|
37+
LL | pub unsafe fn bar(x: *const i32) -> i32 {
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2939

3040
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
31-
--> $DIR/wrapping-unsafe-block-sugg.rs:14:9
41+
--> $DIR/wrapping-unsafe-block-sugg.rs:22:9
3242
|
3343
LL | y + *x
3444
| ^^ dereference of raw pointer
3545
|
3646
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
3747

3848
error: use of mutable static is unsafe and requires unsafe block (error E0133)
39-
--> $DIR/wrapping-unsafe-block-sugg.rs:19:13
49+
--> $DIR/wrapping-unsafe-block-sugg.rs:30:13
4050
|
4151
LL | let y = BAZ;
4252
| ^^^ use of mutable static
4353
|
4454
= 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:28:1
57+
|
58+
LL | pub unsafe fn baz() -> i32 {
59+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
4560

4661
error: use of mutable static is unsafe and requires unsafe block (error E0133)
47-
--> $DIR/wrapping-unsafe-block-sugg.rs:20:9
62+
--> $DIR/wrapping-unsafe-block-sugg.rs:33:9
4863
|
4964
LL | y + BAZ
5065
| ^^^ use of mutable static

0 commit comments

Comments
 (0)