]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix ICE when extra const arguments supplied
authorPhilip Herron <herron.philip@googlemail.com>
Thu, 31 Jul 2025 20:29:02 +0000 (21:29 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:37:01 +0000 (16:37 +0200)
The substitution code was not taking into account the const generic
arguments for checking the max bounds of total available parameters.

Fixes Rust-GCC#3546

gcc/rust/ChangeLog:

* typecheck/rust-tyty-subst.cc: fix check for total arguments

gcc/testsuite/ChangeLog:

* rust/compile/issue-3546.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/typecheck/rust-tyty-subst.cc
gcc/testsuite/rust/compile/issue-3546.rs [new file with mode: 0644]

index a47cde3b966414274c8d68c638f11fca30e8821b..45c8cd638eb4dad67c1b2926ff78ea79037889fc 100644 (file)
@@ -705,7 +705,9 @@ SubstitutionRef::get_mappings_from_generic_args (
 
   // for inherited arguments
   size_t offs = used_arguments.size ();
-  if (args.get_type_args ().size () + offs > substitutions.size ())
+  size_t total_arguments
+    = args.get_type_args ().size () + args.get_const_args ().size () + offs;
+  if (total_arguments > substitutions.size ())
     {
       rich_location r (line_table, args.get_locus ());
       if (!substitutions.empty ())
@@ -723,8 +725,6 @@ SubstitutionRef::get_mappings_from_generic_args (
       return SubstitutionArgumentMappings::error ();
     }
 
-  size_t total_arguments
-    = args.get_type_args ().size () + args.get_const_args ().size () + offs;
   if (total_arguments < min_required_substitutions ())
     {
       rich_location r (line_table, args.get_locus ());
diff --git a/gcc/testsuite/rust/compile/issue-3546.rs b/gcc/testsuite/rust/compile/issue-3546.rs
new file mode 100644 (file)
index 0000000..d4ec0bb
--- /dev/null
@@ -0,0 +1,16 @@
+const L: usize = 3;
+
+fn main() {
+    let p = Printer {};
+    p.print();
+}
+
+trait Print<const N: usize> {
+    fn print(&self) -> usize {
+        3
+    }
+}
+
+struct Printer {}
+impl Print<L> for Printer {}
+// { dg-error "generic item takes at most 1 type arguments but 1 were supplied" "" { target *-*-* } .-1 }