Skip to content

Commit f5c5cca

Browse files
authored
Rollup merge of #85627 - LeSeulArtichaut:thir-unsafe-fn-lint, r=nikomatsakis
Fix a few details in THIR unsafeck This makes it consistent with RFC 2585 (`unsafe_op_in_unsafe_fn`) and with the MIR unsafeck. r? `@nikomatsakis`
2 parents 27899e3 + f9e08cd commit f5c5cca

File tree

4 files changed

+166
-32
lines changed

4 files changed

+166
-32
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

+25-16
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
4242
self.warn_unused_unsafe(
4343
hir_id,
4444
block_span,
45-
Some(self.tcx.sess.source_map().guess_head_span(enclosing_span)),
45+
Some((self.tcx.sess.source_map().guess_head_span(enclosing_span), "block")),
4646
);
4747
f(self);
4848
} else {
@@ -52,7 +52,15 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
5252
f(self);
5353

5454
if let SafetyContext::UnsafeBlock { used: false, span, hir_id } = self.safety_context {
55-
self.warn_unused_unsafe(hir_id, span, self.body_unsafety.unsafe_fn_sig_span());
55+
self.warn_unused_unsafe(
56+
hir_id,
57+
span,
58+
if self.unsafe_op_in_unsafe_fn_allowed() {
59+
self.body_unsafety.unsafe_fn_sig_span().map(|span| (span, "fn"))
60+
} else {
61+
None
62+
},
63+
);
5664
}
5765
self.safety_context = prev_context;
5866
return;
@@ -72,16 +80,20 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
7280
SafetyContext::UnsafeFn if unsafe_op_in_unsafe_fn_allowed => {}
7381
SafetyContext::UnsafeFn => {
7482
// unsafe_op_in_unsafe_fn is disallowed
75-
struct_span_err!(
76-
self.tcx.sess,
83+
self.tcx.struct_span_lint_hir(
84+
UNSAFE_OP_IN_UNSAFE_FN,
85+
self.hir_context,
7786
span,
78-
E0133,
79-
"{} is unsafe and requires unsafe block",
80-
description,
87+
|lint| {
88+
lint.build(&format!(
89+
"{} is unsafe and requires unsafe block (error E0133)",
90+
description,
91+
))
92+
.span_label(span, description)
93+
.note(note)
94+
.emit();
95+
},
8196
)
82-
.span_label(span, description)
83-
.note(note)
84-
.emit();
8597
}
8698
SafetyContext::Safe => {
8799
let fn_sugg = if unsafe_op_in_unsafe_fn_allowed { " function or" } else { "" };
@@ -104,18 +116,15 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
104116
&self,
105117
hir_id: hir::HirId,
106118
block_span: Span,
107-
enclosing_span: Option<Span>,
119+
enclosing_unsafe: Option<(Span, &'static str)>,
108120
) {
109121
let block_span = self.tcx.sess.source_map().guess_head_span(block_span);
110122
self.tcx.struct_span_lint_hir(UNUSED_UNSAFE, hir_id, block_span, |lint| {
111123
let msg = "unnecessary `unsafe` block";
112124
let mut db = lint.build(msg);
113125
db.span_label(block_span, msg);
114-
if let Some(enclosing_span) = enclosing_span {
115-
db.span_label(
116-
enclosing_span,
117-
format!("because it's nested under this `unsafe` block"),
118-
);
126+
if let Some((span, kind)) = enclosing_unsafe {
127+
db.span_label(span, format!("because it's nested under this `unsafe` {}", kind));
119128
}
120129
db.emit();
121130
});

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

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,90 @@
11
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
2-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:9:5
2+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:12:5
33
|
44
LL | unsf();
55
| ^^^^^^ call to unsafe function
66
|
77
note: the lint level is defined here
8-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:1:9
8+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:4:9
99
|
1010
LL | #![deny(unsafe_op_in_unsafe_fn)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^
1212
= note: consult the function's documentation for information on how to avoid undefined behavior
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:11: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:13: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:16:5
31+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:19:5
3232
|
3333
LL | unsafe {}
3434
| ^^^^^^ unnecessary `unsafe` block
3535
|
3636
note: the lint level is defined here
37-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:2:9
37+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:5:9
3838
|
3939
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:24: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: the lint level is defined here
49-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:22:8
49+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:25:8
5050
|
5151
LL | #[deny(warnings)]
5252
| ^^^^^^^^
5353
= note: `#[deny(unsafe_op_in_unsafe_fn)]` implied by `#[deny(warnings)]`
5454
= note: consult the function's documentation for information on how to avoid undefined behavior
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:26: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:28: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:30: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:44: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: unnecessary `unsafe` block
87-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:55:5
87+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:58:5
8888
|
8989
LL | unsafe fn allow_level() {
9090
| ----------------------- because it's nested under this `unsafe` fn
@@ -93,7 +93,7 @@ LL | unsafe { unsf() }
9393
| ^^^^^^ unnecessary `unsafe` block
9494

9595
error: unnecessary `unsafe` block
96-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:67:9
96+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:70:9
9797
|
9898
LL | unsafe fn nested_allow_level() {
9999
| ------------------------------ because it's nested under this `unsafe` fn
@@ -102,15 +102,15 @@ LL | unsafe { unsf() }
102102
| ^^^^^^ unnecessary `unsafe` block
103103

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

112112
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
113-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:77:9
113+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:80:9
114114
|
115115
LL | unsf();
116116
| ^^^^^^ call to unsafe function

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

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// revisions: mir thir
2+
// [thir]compile-flags: -Zthir-unsafeck
3+
14
#![deny(unsafe_op_in_unsafe_fn)]
25
#![deny(unused_unsafe)]
36

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
2+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:12:5
3+
|
4+
LL | unsf();
5+
| ^^^^^^ call to unsafe function
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:4:9
9+
|
10+
LL | #![deny(unsafe_op_in_unsafe_fn)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^
12+
= note: consult the function's documentation for information on how to avoid undefined behavior
13+
14+
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
15+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:14:5
16+
|
17+
LL | *PTR;
18+
| ^^^^ dereference of raw pointer
19+
|
20+
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
21+
22+
error: use of mutable static is unsafe and requires unsafe block (error E0133)
23+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:16:5
24+
|
25+
LL | VOID = ();
26+
| ^^^^ use of mutable static
27+
|
28+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
29+
30+
error: unnecessary `unsafe` block
31+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:19:5
32+
|
33+
LL | unsafe {}
34+
| ^^^^^^ unnecessary `unsafe` block
35+
|
36+
note: the lint level is defined here
37+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:5:9
38+
|
39+
LL | #![deny(unused_unsafe)]
40+
| ^^^^^^^^^^^^^
41+
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
44+
|
45+
LL | unsf();
46+
| ^^^^^^ call to unsafe function
47+
|
48+
note: the lint level is defined here
49+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:25:8
50+
|
51+
LL | #[deny(warnings)]
52+
| ^^^^^^^^
53+
= note: `#[deny(unsafe_op_in_unsafe_fn)]` implied by `#[deny(warnings)]`
54+
= note: consult the function's documentation for information on how to avoid undefined behavior
55+
56+
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
57+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:29:5
58+
|
59+
LL | *PTR;
60+
| ^^^^ dereference of raw pointer
61+
|
62+
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
63+
64+
error: use of mutable static is unsafe and requires unsafe block (error E0133)
65+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:31:5
66+
|
67+
LL | VOID = ();
68+
| ^^^^ use of mutable static
69+
|
70+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
71+
72+
error: unnecessary `unsafe` block
73+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:33:5
74+
|
75+
LL | unsafe {}
76+
| ^^^^^^ unnecessary `unsafe` block
77+
78+
error: unnecessary `unsafe` block
79+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:47:14
80+
|
81+
LL | unsafe { unsafe { unsf() } }
82+
| ------ ^^^^^^ unnecessary `unsafe` block
83+
| |
84+
| because it's nested under this `unsafe` block
85+
86+
error: unnecessary `unsafe` block
87+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:58:5
88+
|
89+
LL | unsafe fn allow_level() {
90+
| ----------------------- because it's nested under this `unsafe` fn
91+
...
92+
LL | unsafe { unsf() }
93+
| ^^^^^^ unnecessary `unsafe` block
94+
95+
error: unnecessary `unsafe` block
96+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:70:9
97+
|
98+
LL | unsafe fn nested_allow_level() {
99+
| ------------------------------ because it's nested under this `unsafe` fn
100+
...
101+
LL | unsafe { unsf() }
102+
| ^^^^^^ unnecessary `unsafe` block
103+
104+
error[E0133]: call to unsafe function is unsafe and requires unsafe block
105+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:76:5
106+
|
107+
LL | unsf();
108+
| ^^^^^^ call to unsafe function
109+
|
110+
= note: consult the function's documentation for information on how to avoid undefined behavior
111+
112+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
113+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:80:9
114+
|
115+
LL | unsf();
116+
| ^^^^^^ call to unsafe function
117+
|
118+
= note: consult the function's documentation for information on how to avoid undefined behavior
119+
120+
error: aborting due to 13 previous errors
121+
122+
For more information about this error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)