Skip to content

Commit b06e8ad

Browse files
committed
Auto merge of rust-lang#127944 - compiler-errors:beta-no-unsafe, r=Mark-Simulacrum
Don't allow unsafe statics outside of extern blocks (beta version) This PR fixes a regression where we allowed `unsafe static` items in top-level modules (i.e. outside of `unsafe extern` blocks). rust-lang#127943 does not rebase cleanly, so I've prepared an extremely pared down version of this PR for beta purposes.
2 parents 3ad5ec8 + 42c2364 commit b06e8ad

File tree

6 files changed

+46
-5
lines changed

6 files changed

+46
-5
lines changed

compiler/rustc_ast_passes/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ ast_passes_unsafe_negative_impl = negative impls cannot be unsafe
264264
.negative = negative because of this
265265
.unsafe = unsafe because of this
266266
267+
ast_passes_unsafe_static =
268+
static items cannot be declared with `unsafe` safety qualifier outside of `extern` block
269+
267270
ast_passes_visibility_not_permitted =
268271
visibility qualifiers are not permitted here
269272
.enum_variant = enum variants and their fields always share the visibility of the enum they are in

compiler/rustc_ast_passes/src/ast_validation.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1161,11 +1161,17 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11611161
});
11621162
}
11631163
}
1164-
ItemKind::Static(box StaticItem { expr: None, .. }) => {
1165-
self.dcx().emit_err(errors::StaticWithoutBody {
1166-
span: item.span,
1167-
replace_span: self.ending_semi_or_hi(item.span),
1168-
});
1164+
ItemKind::Static(box StaticItem { expr, safety, .. }) => {
1165+
if matches!(safety, Safety::Unsafe(_)) {
1166+
self.dcx().emit_err(errors::UnsafeStatic { span: item.span });
1167+
}
1168+
1169+
if expr.is_none() {
1170+
self.dcx().emit_err(errors::StaticWithoutBody {
1171+
span: item.span,
1172+
replace_span: self.ending_semi_or_hi(item.span),
1173+
});
1174+
}
11691175
}
11701176
ItemKind::TyAlias(
11711177
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_unsafe_static)]
230+
pub struct UnsafeStatic {
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,8 @@
1+
error: static items cannot be declared with `unsafe` safety qualifier outside of `extern` block
2+
--> $DIR/safe-outside-extern.rs:6:1
3+
|
4+
LL | unsafe static LOL: u8 = 0;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ revisions: gated ungated
2+
3+
// Very pared down version of the same named test on nightly, since we only want
4+
// to validate that `unsafe static` is not being accidentally accepted by the parser.
5+
6+
unsafe static LOL: u8 = 0;
7+
//~^ ERROR: static items cannot be declared with `unsafe` safety qualifier outside of `extern` block
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: static items cannot be declared with `unsafe` safety qualifier outside of `extern` block
2+
--> $DIR/safe-outside-extern.rs:6:1
3+
|
4+
LL | unsafe static LOL: u8 = 0;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)