Skip to content

Commit

Permalink
Emit an error on unsafe modules
Browse files Browse the repository at this point in the history
An error should be emitted on unsafe modules during the AST validation
pass as the syntax allows those even though they're not alowed later down
the line.

gcc/rust/ChangeLog:

	* ast/rust-item.h: Add safety getter to modules.
	* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Check
	a module's safety and emit an error when meeting an unsafe module.
	* checks/errors/rust-ast-validation.h: Add function prototype.
	* parse/rust-parse-impl.h (Parser::parse_module): Move the module locus
	to the first token instead of the mod keyword.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
  • Loading branch information
P-E-P committed Nov 29, 2023
1 parent 3eb9428 commit 26ac74e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions gcc/rust/ast/rust-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Attribute> &get_inner_attrs () const { return inner_attrs; }
std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
Expand Down
10 changes: 10 additions & 0 deletions gcc/rust/checks/errors/rust-ast-validation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// <http://www.gnu.org/licenses/>.

#include "rust-ast-validation.h"
#include "rust-common.h"
#include "rust-diagnostics.h"
#include "rust-item.h"
#include "rust-keyword-values.h"
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions gcc/rust/checks/errors/rust-ast-validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2429,14 +2429,15 @@ std::unique_ptr<AST::Module>
Parser<ManagedTokenSource>::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)
{
safety = Unsafety::Unsafe;
skip_token (UNSAFE);
}

location_t locus = lexer.peek_token ()->get_locus ();
skip_token (MOD);

const_TokenPtr module_name = expect_token (IDENTIFIER);
Expand Down

0 comments on commit 26ac74e

Please sign in to comment.