]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: handle outer attributes in expression parsing
authorlenny.chiadmi-delage <lenny.chiadmi-delage@epita.fr>
Tue, 6 Jan 2026 14:51:14 +0000 (14:51 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Fri, 16 Jan 2026 16:32:07 +0000 (17:32 +0100)
Fix parsing of outer attributes in expressions.

Fixes Rust-GCC/gccrs#3904

gcc/rust/ChangeLog:

* parse/rust-parse-impl-expr.hxx(Parser::null_denotation): Add
HASH case to handle outer attributes in expressions.

Signed-off-by: lenny.chiadmi-delage <lenny.chiadmi-delage@epita.fr>
gcc/rust/parse/rust-parse-impl-expr.hxx

index 96a2686df00fc1f8d2adf1c2660e60151e0902b3..b870273e27c8b948a51447af266ca07b31766b28 100644 (file)
@@ -1901,6 +1901,32 @@ Parser<ManagedTokenSource>::null_denotation (AST::AttrVec outer_attrs,
        return null_denotation_path (std::move (path), std::move (outer_attrs),
                                     restrictions);
       }
+    case HASH:
+      {
+       // Parse outer attributes and then the expression that follows
+       AST::AttrVec attrs = parse_outer_attributes ();
+
+       // Merge with any existing outer attributes
+       if (!outer_attrs.empty ())
+         attrs.insert (attrs.begin (), outer_attrs.begin (),
+                       outer_attrs.end ());
+
+       // Try to parse the expression that should follow the attributes
+       auto expr = parse_expr (std::move (attrs), restrictions);
+       if (!expr)
+         {
+           /* If parsing failed and we're at a semicolon, provide a better
+            * error
+            */
+           const_TokenPtr next_tok = lexer.peek_token ();
+           if (next_tok->get_id () == SEMICOLON)
+             add_error (Error (next_tok->get_locus (),
+                               "expected expression, found %<;%>"));
+           return tl::unexpected<Parse::Error::Expr> (
+             Parse::Error::Expr::CHILD_ERROR);
+         }
+       return expr;
+      }
     default:
       if (tok->get_id () == LEFT_SHIFT)
        {