https://gcc.gnu.org/g:ad8393db6b492358028367d27b4f3b918dd7a7c0

commit r16-2991-gad8393db6b492358028367d27b4f3b918dd7a7c0
Author: Philip Herron <herron.phi...@googlemail.com>
Date:   Thu Jul 31 21:29:02 2025 +0100

    gccrs: Fix ICE when extra const arguments supplied
    
    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.phi...@googlemail.com>

Diff:
---
 gcc/rust/typecheck/rust-tyty-subst.cc    |  6 +++---
 gcc/testsuite/rust/compile/issue-3546.rs | 16 ++++++++++++++++
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/typecheck/rust-tyty-subst.cc 
b/gcc/rust/typecheck/rust-tyty-subst.cc
index a47cde3b9664..45c8cd638eb4 100644
--- a/gcc/rust/typecheck/rust-tyty-subst.cc
+++ b/gcc/rust/typecheck/rust-tyty-subst.cc
@@ -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
index 000000000000..d4ec0bbd4350
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-3546.rs
@@ -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 }

Reply via email to