1
Fork 0

Don't allow unsafe statics outside of extern blocks

This commit is contained in:
Michael Goulet 2024-07-18 18:02:09 -04:00
parent 5affbb1715
commit 2f5a84ea16
6 changed files with 35 additions and 2 deletions

View file

@ -438,6 +438,11 @@ impl<'a> AstValidator<'a> {
}
}
/// This ensures that items can only be `unsafe` (or unmarked) outside of extern
/// blocks.
///
/// This additionally ensures that within extern blocks, items can only be
/// `safe`/`unsafe` inside of a `unsafe`-adorned extern block.
fn check_item_safety(&self, span: Span, safety: Safety) {
match self.extern_mod_safety {
Some(extern_safety) => {
@ -1177,6 +1182,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}
ItemKind::Static(box StaticItem { expr, safety, .. }) => {
self.check_item_safety(item.span, *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 {

View file

@ -224,6 +224,13 @@ pub struct InvalidSafetyOnBareFn {
pub span: 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> {