From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 0EDB1385AE65; Wed, 15 Jun 2022 07:00:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0EDB1385AE65 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] parser: Error out if the order of lifetimes and generic types is incorrect X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: b9013bda082602ecdc6519009fe82c8f17d2cc4d X-Git-Newrev: 8e22742c9ad844d5e6dd348f6f7d8fa2ef064de4 Message-Id: <20220615070053.0EDB1385AE65@sourceware.org> Date: Wed, 15 Jun 2022 07:00:53 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Jun 2022 07:00:53 -0000 https://gcc.gnu.org/g:8e22742c9ad844d5e6dd348f6f7d8fa2ef064de4 commit 8e22742c9ad844d5e6dd348f6f7d8fa2ef064de4 Author: Arthur Cohen 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::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; // { dg-error "invalid order for generic parameters: lifetimes should always come before types" }