From ecc6f33b8047980388e09c1a684c9fb5fc9ba8eb Mon Sep 17 00:00:00 2001 From: beamandala Date: Wed, 26 Mar 2025 11:47:48 -0500 Subject: [PATCH] parser: Ensure `let-else` does not end with `}` gcc/rust/ChangeLog: * parse/rust-parse-impl.h: Emit error if `let-else` ends with `}` --- gcc/rust/parse/rust-parse-impl.h | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index bd5011329142..aacab09ca567 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -6145,13 +6145,32 @@ Parser::parse_let_stmt (AST::AttrVec outer_attrs, } // parse expression to set variable to (optional) + location_t end_curly_loc = UNKNOWN_LOCATION; std::unique_ptr expr = nullptr; if (lexer.peek_token ()->get_id () == EQUAL) { // must have an expression lexer.skip_token (); - expr = parse_expr (); + if (lexer.peek_token ()->get_id () == LEFT_CURLY) + { + expr = parse_block_expr (); + + if (expr != nullptr) + { + const Rust::AST::BlockExpr& block = static_cast(*expr); + end_curly_loc = block.get_end_locus (); + } + else + { + end_curly_loc = lexer.peek_token ()->get_locus (); + } + } + else + { + expr = parse_expr (); + } + if (expr == nullptr) { Error error (lexer.peek_token ()->get_locus (), @@ -6165,7 +6184,16 @@ Parser::parse_let_stmt (AST::AttrVec outer_attrs, tl::optional> else_expr = tl::nullopt; if (maybe_skip_token (ELSE)) - else_expr = parse_block_expr (); + { + if (end_curly_loc != UNKNOWN_LOCATION) + { + Error error (end_curly_loc, + "right curly brace `}` before `else` in a `let...else`"); + add_error (std::move (error)); + } + + else_expr = parse_block_expr (); + } if (restrictions.consume_semi) {