aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/parse/rust-parse.h
diff options
context:
space:
mode:
authorArthur Cohen <arthur.cohen@embecosm.com>2022-03-21 17:27:05 +0100
committerArthur Cohen <arthur.cohen@embecosm.com>2022-03-22 16:00:30 +0100
commitef5638186202daac03feed7a20eb975991965403 (patch)
tree247ff565c1d06636f05afbd5cd7af555e6d160bb /gcc/rust/parse/rust-parse.h
parentcc6e405912c83aee41efd3015d9157cdbe9134fe (diff)
downloadgcc-ef5638186202daac03feed7a20eb975991965403.zip
gcc-ef5638186202daac03feed7a20eb975991965403.tar.gz
gcc-ef5638186202daac03feed7a20eb975991965403.tar.bz2
parser: Add better restrictions around semicolons in statements
When parsing macro invocations, rustc does not actually consume the statement's trailing semicolon. Let's take the following example: ```rust macro_rules! one_stmt { ($s:stmt) => {}; } macro_rules! one_or_more_stmt { ($($s:stmt)*) => {}; } one_stmt!(let a = 1); one_stmt!(let b = 2;); // error one_or_more_stmt!(;); // valid one_or_more_stmt!(let a = 15;); // valid, two statements! one_or_more_stmt!(let a = 15 let b = 13); // valid, two statements again ``` A semicolon can count as a valid empty statement, but cannot be part of a statement (in macro invocations). This commit adds more restrictions that allow the parser to not always expect a semicolon token after the statement. Furthermore, this fixes a test that was previously accepted by the compiler but not by rustc.
Diffstat (limited to 'gcc/rust/parse/rust-parse.h')
-rw-r--r--gcc/rust/parse/rust-parse.h16
1 files changed, 9 insertions, 7 deletions
diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h
index 9a31fb6..c00dc11 100644
--- a/gcc/rust/parse/rust-parse.h
+++ b/gcc/rust/parse/rust-parse.h
@@ -81,6 +81,7 @@ struct ParseRestrictions
bool entered_from_unary = false;
bool expr_can_be_null = false;
bool expr_can_be_stmt = false;
+ bool consume_semi = true;
};
// Parser implementation for gccrs.
@@ -129,11 +130,9 @@ public:
* | LetStatement
* | ExpressionStatement
* | MacroInvocationSemi
- *
- * @param allow_no_semi Allow the parser to not parse a semicolon after
- * the statement without erroring out
*/
- std::unique_ptr<AST::Stmt> parse_stmt (bool allow_no_semi = false);
+ std::unique_ptr<AST::Stmt> parse_stmt (ParseRestrictions restrictions
+ = ParseRestrictions ());
std::unique_ptr<AST::Type> parse_type ();
std::unique_ptr<AST::ExternalItem> parse_external_item ();
std::unique_ptr<AST::TraitItem> parse_trait_item ();
@@ -616,14 +615,17 @@ private:
* semicolon to follow it
*/
std::unique_ptr<AST::LetStmt> parse_let_stmt (AST::AttrVec outer_attrs,
- bool allow_no_semi = false);
+ ParseRestrictions restrictions
+ = ParseRestrictions ());
std::unique_ptr<AST::ExprStmt> parse_expr_stmt (AST::AttrVec outer_attrs,
- bool allow_no_semi = false);
+ ParseRestrictions restrictions
+ = ParseRestrictions ());
std::unique_ptr<AST::ExprStmtWithBlock>
parse_expr_stmt_with_block (AST::AttrVec outer_attrs);
std::unique_ptr<AST::ExprStmtWithoutBlock>
parse_expr_stmt_without_block (AST::AttrVec outer_attrs,
- bool allow_no_semi = false);
+ ParseRestrictions restrictions
+ = ParseRestrictions ());
ExprOrStmt parse_stmt_or_expr_without_block ();
ExprOrStmt parse_stmt_or_expr_with_block (AST::AttrVec outer_attrs);
ExprOrStmt parse_macro_invocation_maybe_semi (AST::AttrVec outer_attrs);