From ee3d0b09dc7777cd04587a83f5cfe785101f0dc9 Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Sat, 14 Aug 2021 20:41:26 +0200 Subject: parse if expression with unary minus or not expression An if conditional expression doesn't need brackets, but that means that it doesn't accept struct expressions. Those are not easy to distinquish from the block that follows. What isn't immediately clear from the grammar is that unary conditions like minus '-' or not '!' also shouldn't accept struct expressions (when part of an if conditional expression) because those also cannot be easily distinquished from the block that follows. Add a testcase "ifunaryexpr.rs" that shows a couple of contructs that should be accepted as if conditional expressions and fix the parser to pass the restriction of not accepting struct expressions after a unary expression. --- gcc/rust/parse/rust-parse-impl.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'gcc/rust/parse') diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 731e0b3..fa6d409 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -12547,6 +12547,8 @@ Parser::null_denotation (const_TokenPtr tok, case MINUS: { // unary minus ParseRestrictions entered_from_unary; entered_from_unary.entered_from_unary = true; + if (!restrictions.can_be_struct_expr) + entered_from_unary.can_be_struct_expr = false; std::unique_ptr expr = parse_expr (LBP_UNARY_MINUS, {}, entered_from_unary); @@ -12571,6 +12573,8 @@ Parser::null_denotation (const_TokenPtr tok, case EXCLAM: { // logical or bitwise not ParseRestrictions entered_from_unary; entered_from_unary.entered_from_unary = true; + if (!restrictions.can_be_struct_expr) + entered_from_unary.can_be_struct_expr = false; std::unique_ptr expr = parse_expr (LBP_UNARY_EXCLAM, {}, entered_from_unary); -- cgit v1.1