public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/rust/master] parser: Error out if the order of lifetimes and generic types is incorrect
Date: Wed, 15 Jun 2022 07:00:53 +0000 (GMT)	[thread overview]
Message-ID: <20220615070053.0EDB1385AE65@sourceware.org> (raw)

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" }


                 reply	other threads:[~2022-06-15  7:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220615070053.0EDB1385AE65@sourceware.org \
    --to=tschwinge@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).