]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix ICE with non-trailing const defaults
authorvishruth-thimmaiah <vishruththimmaiah@gmail.com>
Wed, 29 Oct 2025 17:20:49 +0000 (22:50 +0530)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 17 Nov 2025 14:58:17 +0000 (15:58 +0100)
When a const generic with a default value is not trailing, emit an
error.

gcc/rust/ChangeLog:

* parse/rust-parse-impl.h (Parser::parse_generic_params): Emit
an error when const generics with a default value is not
trailing.

gcc/testsuite/ChangeLog:

* rust/compile/const_generics_17.rs: New test.
* rust/compile/generics14.rs: New test.

Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
gcc/rust/parse/rust-parse-impl.h
gcc/testsuite/rust/compile/const_generics_17.rs [new file with mode: 0644]
gcc/testsuite/rust/compile/generics14.rs [new file with mode: 0644]

index e79727704553d35feb5b64372a1b781c43140c4f..62831ebbf10ef6193d6ff10f86751c602d0b90e6 100644 (file)
@@ -3262,8 +3262,12 @@ Parser<ManagedTokenSource>::parse_generic_params (EndTokenPred is_end_token)
 
   // Did we parse a generic type param yet
   auto type_seen = false;
+  // Did we parse a const param with a default value yet
+  auto const_with_default_seen = false;
   // Did the user write a lifetime parameter after a type one
   auto order_error = false;
+  // Did the user write a const param with a default value after a type one
+  auto const_with_default_order_error = false;
 
   // parse lifetime params
   while (!is_end_token (lexer.peek_token ()->get_id ()))
@@ -3271,12 +3275,29 @@ Parser<ManagedTokenSource>::parse_generic_params (EndTokenPred is_end_token)
       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;
+           {
+             type_seen = true;
+             if (const_with_default_seen)
+               const_with_default_order_error = true;
+           }
          else if (param->get_kind () == AST::GenericParam::Kind::Lifetime
                   && type_seen)
-           order_error = true;
+           {
+             order_error = true;
+             if (const_with_default_seen)
+               const_with_default_order_error = true;
+           }
+         else if (param->get_kind () == AST::GenericParam::Kind::Const)
+           {
+             type_seen = true;
+             AST::ConstGenericParam *const_param
+               = static_cast<AST::ConstGenericParam *> (param.get ());
+             if (const_param->has_default_value ())
+               const_with_default_seen = true;
+             else if (const_with_default_seen)
+               const_with_default_order_error = true;
+           }
 
          generic_params.emplace_back (std::move (param));
          maybe_skip_token (COMMA);
@@ -3293,6 +3314,13 @@ Parser<ManagedTokenSource>::parse_generic_params (EndTokenPred is_end_token)
                   "must be declared prior to type and const parameters");
       add_error (std::move (error));
     }
+  if (const_with_default_order_error)
+    {
+      Error error (generic_params.front ()->get_locus (),
+                  "invalid order for generic parameters: generic parameters "
+                  "with a default must be trailing");
+      add_error (std::move (error));
+    }
 
   generic_params.shrink_to_fit ();
   return generic_params;
diff --git a/gcc/testsuite/rust/compile/const_generics_17.rs b/gcc/testsuite/rust/compile/const_generics_17.rs
new file mode 100644 (file)
index 0000000..f65ca22
--- /dev/null
@@ -0,0 +1,3 @@
+struct Foo<const N: u32 = 1, const O: bool>; // { dg-error "invalid order for generic parameters: generic parameters with a default must be trailing" }
+
+impl<const N: u32> Foo<N> {}
diff --git a/gcc/testsuite/rust/compile/generics14.rs b/gcc/testsuite/rust/compile/generics14.rs
new file mode 100644 (file)
index 0000000..d97fd35
--- /dev/null
@@ -0,0 +1 @@
+struct Foo<const N: i32, 'a>; // { dg-error "invalid order for generic parameters: lifetime parameters must be declared prior to type and const parameters" }