Skip to content

Ensure let-else does not end with } #3577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6145,13 +6145,32 @@ Parser<ManagedTokenSource>::parse_let_stmt (AST::AttrVec outer_attrs,
}

// parse expression to set variable to (optional)
location_t end_curly_loc = UNKNOWN_LOCATION;
std::unique_ptr<AST::Expr> 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<const Rust::AST::BlockExpr&>(*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 (),
Expand All @@ -6165,7 +6184,16 @@ Parser<ManagedTokenSource>::parse_let_stmt (AST::AttrVec outer_attrs,

tl::optional<std::unique_ptr<AST::Expr>> 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)
{
Expand Down
Loading