diff options
| author | Michael Goulet <michael@errs.io> | 2024-07-18 18:02:09 -0400 |
|---|---|---|
| committer | Michael Goulet <michael@errs.io> | 2024-07-18 18:02:29 -0400 |
| commit | 2f5a84ea16f232f8a0709ea567bca0cfd90b44cf (patch) | |
| tree | dfa97ce2859075ccf2a6609fb4b4cb97fb7c4bf1 /compiler/rustc_ast_passes/src | |
| parent | 5affbb17153bc69a9d5d8d2faa4e399a014a211e (diff) | |
| download | rust-2f5a84ea16f232f8a0709ea567bca0cfd90b44cf.tar.gz rust-2f5a84ea16f232f8a0709ea567bca0cfd90b44cf.zip | |
Don't allow unsafe statics outside of extern blocks
Diffstat (limited to 'compiler/rustc_ast_passes/src')
| -rw-r--r-- | compiler/rustc_ast_passes/src/ast_validation.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_ast_passes/src/errors.rs | 7 |
2 files changed, 15 insertions, 0 deletions
diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 83249dea82a..34aac6e4473 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -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 { diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index 460da254653..783bca6b695 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -225,6 +225,13 @@ pub struct InvalidSafetyOnBareFn { } #[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> { #[primary_span] |
