From 48408712cc8cc39cba50c10938a284661ddece87 Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Wed, 10 May 2023 02:08:37 +0100 Subject: gccrs: Fix parsing of block expressions followed by `.` `{ ... }.f;` is parsed as a single statement in rust. This means that we can't determine whether an expression statement will need a semicolon terminator until we finish parsing it. To handle this we change expression parsing to check for this case by inspecting the expression returned from null_denotation and looking ahead for a `.` or `?` token. gcc/rust/ChangeLog: * ast/rust-ast.h (Expr::as_expr_without_block): Remove. (Expr::set_outer_attrs): Make public in base class. * expand/rust-macro-expand.cc: Add fixme comment for pre-existing bug. * hir/tree/rust-hir.h: Remove Expr::as_expr_without_block. * parse/rust-parse-impl.h (Parser::parse_lifetime): Use lifetime_from_token. (Parser::lifetime_from_token): New method. (Parser::null_denotation): Handle labelled loop expressions and for loop expressions. (Parser::parse_loop_label): Make initial token a parameter. (Parser::parse_labelled_loop_expr): Likewise. (Parser::parse_for_loop_expr): Allow FOR token to already be skipped. (Parser::parse_expr): Handle expr_can_be_stmt. (Parser::parse_expr_with_block): Remove. (Parser::parse_expr_stmt_with_block): Remove. (Parser::parse_expr_stmt_without_block): Remove. (Parser::parse_expr_without_block): Remove. (Parser::parse_stmt_or_expr_with_block): Remove. (Parser::parse_expr_stmt): Use parse_expr directly. (Parser::parse_match_expr): Likewise. (Parser::parse_stmt): Use parse_expr_stmt in more cases. (Parser::parse_stmt_or_expr): Rename from parse_stmt_or_expr_without_block, use parse_expr directly. (Parser::parse_block_expr): Update error message. * parse/rust-parse.h: Update declarations. gcc/testsuite/ChangeLog: * rust/compile/for_expr.rs: New test. * rust/compile/issue-407-2.rs: Update compiler output. * rust/compile/issue-407.rs: Update compiler output. * rust/compile/issue-867.rs: Update compiler output. * rust/compile/issue-2189.rs: New test. * rust/compile/macro_call_statement.rs: New test. * rust/compile/stmt_with_block_dot.rs: New test. * rust/compile/torture/loop8.rs: New test. Signed-off-by: Matthew Jasper --- gcc/rust/hir/tree/rust-hir.h | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'gcc/rust/hir/tree') diff --git a/gcc/rust/hir/tree/rust-hir.h b/gcc/rust/hir/tree/rust-hir.h index 31f1494..0258f5f 100644 --- a/gcc/rust/hir/tree/rust-hir.h +++ b/gcc/rust/hir/tree/rust-hir.h @@ -288,10 +288,6 @@ public: return std::unique_ptr (clone_expr_impl ()); } - /* HACK: downcasting without dynamic_cast (if possible) via polymorphism - - * overrided in subclasses of ExprWithoutBlock */ - virtual ExprWithoutBlock *as_expr_without_block () const { return nullptr; } - // TODO: make pure virtual if move out outer attributes to derived classes virtual std::string as_string () const; @@ -354,13 +350,6 @@ public: return std::unique_ptr (clone_expr_without_block_impl ()); } - /* downcasting hack from expr to use pratt parsing with - * parse_expr_without_block */ - ExprWithoutBlock *as_expr_without_block () const override - { - return clone_expr_without_block_impl (); - } - BlockType get_block_expr_type () const final override { return BlockType::WITHOUT_BLOCK; -- cgit v1.1