]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Reject `default` functions after parsing.
authorKushal Pal <kushalpal109@gmail.com>
Sat, 24 Feb 2024 11:55:48 +0000 (17:25 +0530)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 14 Apr 2026 21:48:55 +0000 (23:48 +0200)
Functions with `default` qualifier outside of `impl` blocks should be
allowed to parse successfully and are later rejected during the
ASTValidation pass.

gcc/rust/ChangeLog:

* checks/errors/rust-ast-validation.cc (ASTValidation::visit):
Add error for functions with `default` qualifier outside of impl
blocks.
* parse/rust-parse-impl.hxx (Parser::parse_item):
Allow parsing of functions with `default` qualifier.
(Parser::parse_vis_item): Likewise.

Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
gcc/rust/checks/errors/rust-ast-validation.cc
gcc/rust/parse/rust-parse-impl.hxx

index 00a49b6dada335a22f3c5b830aed6ff74f6d2a8e..52831e26eb14ecb5c867bd7df2d2e3d9d96774fd 100644 (file)
@@ -77,6 +77,11 @@ void
 ASTValidation::visit (AST::Function &function)
 {
   const auto &qualifiers = function.get_qualifiers ();
+  if (qualifiers.is_default () && ctx.peek () != Kind::INHERENT_IMPL
+      && ctx.peek () != Kind::TRAIT_IMPL)
+    rust_error_at (
+      function.get_locus (),
+      "%<default%> is only allowed on items within %<impl%> blocks");
   if (qualifiers.is_async () && qualifiers.is_const ())
     rust_error_at (function.get_locus (),
                   "functions cannot be both %<const%> and %<async%>");
index 8c60ac3824513f7a0993bf2a9bae77d55f471d06..8820e3555e0263b0bf4e37cfcacd848a1047ff00 100644 (file)
@@ -431,10 +431,9 @@ Parser<ManagedTokenSource>::parse_item (bool called_from_statement)
       else if (t->get_str () == Values::WeakKeywords::DEFAULT
               && lexer.peek_token (1)->get_id () != EXCLAM)
        {
-         add_error (Error (t->get_locus (),
-                           "%qs is only allowed on items within %qs blocks",
-                           "default", "impl"));
-         return Parse::Error::Item::make_malformed ();
+         // parse normal functions with `default` qualifier
+         // they will be rejected in ASTValidation pass
+         return parse_vis_item (std::move (outer_attrs));
        }
       else if (is_macro_rules_def (t))
        {
@@ -547,10 +546,13 @@ Parser<ManagedTokenSource>::parse_vis_item (AST::AttrVec outer_attrs)
          return parse_union (std::move (vis), std::move (outer_attrs));
          // or should item switch go straight to parsing union?
        }
-      else
+      else if (t->get_str () == Values::WeakKeywords::DEFAULT)
        {
-         break;
+         // parse normal functions with `default` qualifier they will be
+         // rejected in ASTValidation pass
+         return parse_function (std::move (vis), std::move (outer_attrs));
        }
+      break;
     case CONST:
       // lookahead to resolve syntactical production
       t = lexer.peek_token (1);