Auto merge of #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). #127943 does not rebase cleanly, so I've prepared an extremely pared down version of this PR for beta purposes.
This commit is contained in:
commit
b06e8ad970
6 changed files with 46 additions and 5 deletions
|
|
@ -264,6 +264,9 @@ ast_passes_unsafe_negative_impl = negative impls cannot be unsafe
|
|||
.negative = negative because of this
|
||||
.unsafe = unsafe because of this
|
||||
|
||||
ast_passes_unsafe_static =
|
||||
static items cannot be declared with `unsafe` safety qualifier outside of `extern` block
|
||||
|
||||
ast_passes_visibility_not_permitted =
|
||||
visibility qualifiers are not permitted here
|
||||
.enum_variant = enum variants and their fields always share the visibility of the enum they are in
|
||||
|
|
|
|||
|
|
@ -1161,11 +1161,17 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
|
|||
});
|
||||
}
|
||||
}
|
||||
ItemKind::Static(box StaticItem { expr: None, .. }) => {
|
||||
self.dcx().emit_err(errors::StaticWithoutBody {
|
||||
span: item.span,
|
||||
replace_span: self.ending_semi_or_hi(item.span),
|
||||
});
|
||||
ItemKind::Static(box StaticItem { expr, safety, .. }) => {
|
||||
if matches!(safety, Safety::Unsafe(_)) {
|
||||
self.dcx().emit_err(errors::UnsafeStatic { span: item.span });
|
||||
}
|
||||
|
||||
if expr.is_none() {
|
||||
self.dcx().emit_err(errors::StaticWithoutBody {
|
||||
span: item.span,
|
||||
replace_span: self.ending_semi_or_hi(item.span),
|
||||
});
|
||||
}
|
||||
}
|
||||
ItemKind::TyAlias(
|
||||
ty_alias @ box TyAlias { defaultness, bounds, where_clauses, ty, .. },
|
||||
|
|
|
|||
|
|
@ -225,6 +225,13 @@ pub struct InvalidSafetyOnExtern {
|
|||
pub block: Span,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(ast_passes_unsafe_static)]
|
||||
pub struct UnsafeStatic {
|
||||
#[primary_span]
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(ast_passes_bound_in_context)]
|
||||
pub struct BoundInContext<'a> {
|
||||
|
|
|
|||
8
tests/ui/rust-2024/safe-outside-extern.gated.stderr
Normal file
8
tests/ui/rust-2024/safe-outside-extern.gated.stderr
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
error: static items cannot be declared with `unsafe` safety qualifier outside of `extern` block
|
||||
--> $DIR/safe-outside-extern.rs:6:1
|
||||
|
|
||||
LL | unsafe static LOL: u8 = 0;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
9
tests/ui/rust-2024/safe-outside-extern.rs
Normal file
9
tests/ui/rust-2024/safe-outside-extern.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
//@ revisions: gated ungated
|
||||
|
||||
// Very pared down version of the same named test on nightly, since we only want
|
||||
// to validate that `unsafe static` is not being accidentally accepted by the parser.
|
||||
|
||||
unsafe static LOL: u8 = 0;
|
||||
//~^ ERROR: static items cannot be declared with `unsafe` safety qualifier outside of `extern` block
|
||||
|
||||
fn main() {}
|
||||
8
tests/ui/rust-2024/safe-outside-extern.ungated.stderr
Normal file
8
tests/ui/rust-2024/safe-outside-extern.ungated.stderr
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
error: static items cannot be declared with `unsafe` safety qualifier outside of `extern` block
|
||||
--> $DIR/safe-outside-extern.rs:6:1
|
||||
|
|
||||
LL | unsafe static LOL: u8 = 0;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue