aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-11-21 18:51:21 +0100
committerArthur Cohen <arthur.cohen@embecosm.com>2024-01-30 12:36:47 +0100
commit5959f32d28ff76b3e9c177914f034aae72c9a835 (patch)
treec6f3c8d87bc2277341d1101a1978d6daca69cbf1
parentd6a0d96559b30ffac3dba5be675272f0d8e12a1e (diff)
downloadgcc-5959f32d28ff76b3e9c177914f034aae72c9a835.zip
gcc-5959f32d28ff76b3e9c177914f034aae72c9a835.tar.gz
gcc-5959f32d28ff76b3e9c177914f034aae72c9a835.tar.bz2
gccrs: Emit an error on unsafe modules
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>
-rw-r--r--gcc/rust/ast/rust-item.h2
-rw-r--r--gcc/rust/checks/errors/rust-ast-validation.cc10
-rw-r--r--gcc/rust/checks/errors/rust-ast-validation.h1
-rw-r--r--gcc/rust/parse/rust-parse-impl.h3
4 files changed, 15 insertions, 1 deletions
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index 6c3715e..3bf023b 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -840,6 +840,8 @@ public:
// 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; }
diff --git a/gcc/rust/checks/errors/rust-ast-validation.cc b/gcc/rust/checks/errors/rust-ast-validation.cc
index dad7f5e..4b20990 100644
--- a/gcc/rust/checks/errors/rust-ast-validation.cc
+++ b/gcc/rust/checks/errors/rust-ast-validation.cc
@@ -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"
@@ -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 1052168..01d923c 100644
--- a/gcc/rust/checks/errors/rust-ast-validation.h
+++ b/gcc/rust/checks/errors/rust-ast-validation.h
@@ -34,6 +34,7 @@ public:
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 8087e0c..f83cc12 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -2429,6 +2429,8 @@ 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)
{
@@ -2436,7 +2438,6 @@ Parser<ManagedTokenSource>::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);