public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] parser: Error out if the order of lifetimes and generic types is incorrect
@ 2022-06-15 7:00 Thomas Schwinge
0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-15 7:00 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:8e22742c9ad844d5e6dd348f6f7d8fa2ef064de4
commit 8e22742c9ad844d5e6dd348f6f7d8fa2ef064de4
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date: Tue Jun 14 13:21:14 2022 +0200
parser: Error out if the order of lifetimes and generic types is
incorrect
Diff:
---
gcc/rust/parse/rust-parse-impl.h | 20 ++++++++++++++++++--
gcc/testsuite/rust/compile/generics13.rs | 1 +
2 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index 0fd381c7b72..ce68c16ac7c 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -2892,19 +2892,35 @@ Parser<ManagedTokenSource>::parse_generic_params (EndTokenPred is_end_token)
* parsed but it turns out to be type param */
AST::Attribute parsed_outer_attr = AST::Attribute::create_empty ();
+ // Did we parse a generic type param yet
+ auto type_seen = false;
+ // Did the user write a lifetime parameter after a type one
+ auto order_error = false;
+
// parse lifetime params
while (!is_end_token (lexer.peek_token ()->get_id ()))
{
auto param = parse_generic_param (is_end_token);
if (param)
{
+ // TODO: Handle `Const` here as well if necessary
+ if (param->get_kind () == AST::GenericParam::Kind::Type)
+ type_seen = true;
+ else if (param->get_kind () == AST::GenericParam::Kind::Lifetime
+ && type_seen)
+ order_error = true;
+
generic_params.emplace_back (std::move (param));
maybe_skip_token (COMMA);
}
-
- // error out if lifetime after type
}
+ // FIXME: Add reordering hint
+ if (order_error)
+ rust_error_at (generic_params.front ()->get_locus (),
+ "invalid order for generic parameters: lifetimes should "
+ "always come before types");
+
generic_params.shrink_to_fit ();
return generic_params;
}
diff --git a/gcc/testsuite/rust/compile/generics13.rs b/gcc/testsuite/rust/compile/generics13.rs
new file mode 100644
index 00000000000..05c75c5f63d
--- /dev/null
+++ b/gcc/testsuite/rust/compile/generics13.rs
@@ -0,0 +1 @@
+struct Foo<A, 'a>; // { dg-error "invalid order for generic parameters: lifetimes should always come before types" }
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2022-06-15 7:00 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-15 7:00 [gcc/devel/rust/master] parser: Error out if the order of lifetimes and generic types is incorrect Thomas Schwinge
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).