diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index 75402302ba1e..4584d13424db 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -840,6 +840,8 @@ class Module : public VisItem // Returns the kind of the module enum ModuleKind get_kind () const { return kind; } + Unsafety get_unsafety () const { return safety; } + // TODO: think of better way to do this - mutable getter seems dodgy const std::vector &get_inner_attrs () const { return inner_attrs; } std::vector &get_inner_attrs () { return inner_attrs; } diff --git a/gcc/rust/checks/errors/rust-ast-validation.cc b/gcc/rust/checks/errors/rust-ast-validation.cc index dad7f5edded6..4b209908f9e5 100644 --- a/gcc/rust/checks/errors/rust-ast-validation.cc +++ b/gcc/rust/checks/errors/rust-ast-validation.cc @@ -17,6 +17,7 @@ // . #include "rust-ast-validation.h" +#include "rust-common.h" #include "rust-diagnostics.h" #include "rust-item.h" #include "rust-keyword-values.h" @@ -136,4 +137,13 @@ ASTValidation::visit (AST::Trait &trait) AST::ContextualASTVisitor::visit (trait); } +void +ASTValidation::visit (AST::Module &module) +{ + if (module.get_unsafety () == Unsafety::Unsafe) + rust_error_at (module.get_locus (), "module cannot be declared unsafe"); + + AST::ContextualASTVisitor::visit (module); +} + } // namespace Rust diff --git a/gcc/rust/checks/errors/rust-ast-validation.h b/gcc/rust/checks/errors/rust-ast-validation.h index 1052168ea728..01d923ceff33 100644 --- a/gcc/rust/checks/errors/rust-ast-validation.h +++ b/gcc/rust/checks/errors/rust-ast-validation.h @@ -34,6 +34,7 @@ class ASTValidation : public AST::ContextualASTVisitor void check (AST::Crate &crate) { AST::ContextualASTVisitor::visit (crate); } + virtual void visit (AST::Module &module); virtual void visit (AST::ConstantItem &const_item); virtual void visit (AST::Lifetime &lifetime); virtual void visit (AST::LoopLabel &label); diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index dd0128292c51..00b02360d206 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -2389,6 +2389,8 @@ std::unique_ptr Parser::parse_module (AST::Visibility vis, AST::AttrVec outer_attrs) { + location_t locus = lexer.peek_token ()->get_locus (); + Unsafety safety = Unsafety::Normal; if (lexer.peek_token ()->get_id () == UNSAFE) { @@ -2396,7 +2398,6 @@ Parser::parse_module (AST::Visibility vis, skip_token (UNSAFE); } - location_t locus = lexer.peek_token ()->get_locus (); skip_token (MOD); const_TokenPtr module_name = expect_token (IDENTIFIER);