From: Kushal Pal <[email protected]>
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 <[email protected]>
---
gcc/rust/checks/errors/rust-ast-validation.cc | 5 +++++
gcc/rust/parse/rust-parse-impl.hxx | 14 ++++++++------
2 files changed, 13 insertions(+), 6 deletions(-)
diff --git a/gcc/rust/checks/errors/rust-ast-validation.cc
b/gcc/rust/checks/errors/rust-ast-validation.cc
index 00a49b6dada..52831e26eb1 100644
--- a/gcc/rust/checks/errors/rust-ast-validation.cc
+++ b/gcc/rust/checks/errors/rust-ast-validation.cc
@@ -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%>");
diff --git a/gcc/rust/parse/rust-parse-impl.hxx
b/gcc/rust/parse/rust-parse-impl.hxx
index 8c60ac38245..8820e3555e0 100644
--- a/gcc/rust/parse/rust-parse-impl.hxx
+++ b/gcc/rust/parse/rust-parse-impl.hxx
@@ -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);
--
2.50.1