From dfb319d31d5e4e18e81e4a42e5e2d6f4027ca817 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Tue, 21 Nov 2023 18:28:30 +0100 Subject: [PATCH] Parse module safety Unsafe module are rejected at a later stage but could not be parsed properly. This commit changes the parser in order to allow unsafe module in the AST. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_vis_item): Dispatch to parse module when meeting an unsafe module. (Parser::parse_module): Set unsafe status when the parser encounter an unsafe keyword. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/parse/rust-parse-impl.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index b1274fbc53a3..dd0128292c51 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -1411,6 +1411,8 @@ Parser::parse_vis_item (AST::AttrVec outer_attrs) return parse_function (std::move (vis), std::move (outer_attrs)); case IMPL: return parse_impl (std::move (vis), std::move (outer_attrs)); + case MOD: + return parse_module (std::move (vis), std::move (outer_attrs)); default: add_error ( Error (t->get_locus (), @@ -2387,6 +2389,13 @@ std::unique_ptr Parser::parse_module (AST::Visibility vis, AST::AttrVec outer_attrs) { + 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); @@ -2407,7 +2416,7 @@ Parser::parse_module (AST::Visibility vis, // Construct an external module return std::unique_ptr ( new AST::Module (std::move (name), std::move (vis), - std::move (outer_attrs), locus, Unsafety::Normal, + std::move (outer_attrs), locus, safety, lexer.get_filename (), inline_module_stack)); case LEFT_CURLY: { lexer.skip_token (); @@ -2464,8 +2473,7 @@ Parser::parse_module (AST::Visibility vis, return std::unique_ptr ( new AST::Module (std::move (name), locus, std::move (items), - std::move (vis), Unsafety::Normal, - std::move (inner_attrs), + std::move (vis), safety, std::move (inner_attrs), std::move (outer_attrs))); // module name? } default: