Skip to content

Commit 8711347

Browse files
committed
Do not allow safe usafe on static and fn items
1 parent cb8a7ea commit 8711347

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

compiler/rustc_ast_passes/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ ast_passes_extern_fn_qualifiers = functions in `extern` blocks cannot have quali
6767
.label = in this `extern` block
6868
.suggestion = remove this qualifier
6969
70+
ast_passes_item_invalid_safety = items cannot be declared with `safe` safety qualifier
71+
.suggestion = remove safe to this item
72+
7073
ast_passes_extern_invalid_safety = items in unadorned `extern` blocks cannot have safety qualifiers
7174
.suggestion = add unsafe to this `extern` block
7275

compiler/rustc_ast_passes/src/ast_validation.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,12 @@ impl<'a> AstValidator<'a> {
456456
}
457457
}
458458

459+
fn check_item_safety(&self, span: Span, safety: Safety) {
460+
if matches!(safety, Safety::Safe(_)) {
461+
self.dcx().emit_err(errors::InvalidSafetyOnItem { span });
462+
}
463+
}
464+
459465
fn check_foreign_item_safety(&self, item_span: Span, safety: Safety) {
460466
if matches!(safety, Safety::Unsafe(_) | Safety::Safe(_))
461467
&& (self.extern_mod_safety == Some(Safety::Default)
@@ -1015,6 +1021,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10151021
}
10161022
ItemKind::Fn(box Fn { defaultness, sig, generics, body }) => {
10171023
self.check_defaultness(item.span, *defaultness);
1024+
self.check_item_safety(item.span, sig.header.safety);
10181025

10191026
if body.is_none() {
10201027
self.dcx().emit_err(errors::FnWithoutBody {
@@ -1174,11 +1181,15 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11741181
});
11751182
}
11761183
}
1177-
ItemKind::Static(box StaticItem { expr: None, .. }) => {
1178-
self.dcx().emit_err(errors::StaticWithoutBody {
1179-
span: item.span,
1180-
replace_span: self.ending_semi_or_hi(item.span),
1181-
});
1184+
ItemKind::Static(box StaticItem { expr, safety, .. }) => {
1185+
self.check_item_safety(item.span, *safety);
1186+
1187+
if expr.is_none() {
1188+
self.dcx().emit_err(errors::StaticWithoutBody {
1189+
span: item.span,
1190+
replace_span: self.ending_semi_or_hi(item.span),
1191+
});
1192+
}
11821193
}
11831194
ItemKind::TyAlias(
11841195
ty_alias @ box TyAlias { defaultness, bounds, where_clauses, ty, .. },

compiler/rustc_ast_passes/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ pub struct InvalidSafetyOnExtern {
225225
pub block: Span,
226226
}
227227

228+
#[derive(Diagnostic)]
229+
#[diag(ast_passes_item_invalid_safety)]
230+
pub struct InvalidSafetyOnItem {
231+
#[primary_span]
232+
pub span: Span,
233+
}
234+
228235
#[derive(Diagnostic)]
229236
#[diag(ast_passes_bound_in_context)]
230237
pub struct BoundInContext<'a> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
safe fn foo() {}
2+
//~^ ERROR: items cannot be declared with `safe` safety qualifier
3+
4+
safe static FOO: i32 = 1;
5+
//~^ ERROR: items cannot be declared with `safe` safety qualifier
6+
7+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: items cannot be declared with `safe` safety qualifier
2+
--> $DIR/safe-outside-extern.rs:1:1
3+
|
4+
LL | safe fn foo() {}
5+
| ^^^^^^^^^^^^^^^^
6+
7+
error: items cannot be declared with `safe` safety qualifier
8+
--> $DIR/safe-outside-extern.rs:4:1
9+
|
10+
LL | safe static FOO: i32 = 1;
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+

0 commit comments

Comments
 (0)