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

commit r17-2223-gf4bf6e043814858363f4189fc15faeaeacd16d8a
Author: Philip Herron <[email protected]>
Date:   Sat Jan 17 20:47:29 2026 +0000

    gccrs: Optionally pull in the inherited arguments when required
    
    gcc/rust/ChangeLog:
    
            * typecheck/rust-tyty-subst.cc: we can optionally apply inherited 
arguments
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/gat2.rs: New test.
    
    Signed-off-by: Philip Herron <[email protected]>

Diff:
---
 gcc/rust/typecheck/rust-tyty-subst.cc | 12 +++++++++---
 gcc/testsuite/rust/compile/gat2.rs    | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/typecheck/rust-tyty-subst.cc 
b/gcc/rust/typecheck/rust-tyty-subst.cc
index f3873b8e6843..6ee2a60dfe7b 100644
--- a/gcc/rust/typecheck/rust-tyty-subst.cc
+++ b/gcc/rust/typecheck/rust-tyty-subst.cc
@@ -706,10 +706,16 @@ SubstitutionRef::get_mappings_from_generic_args (
        }
     }
 
-  // for inherited arguments
-  size_t offs = used_arguments.size ();
+  // check if we need to use inherited arguments or nothing
+  size_t offs = 0;
   size_t total_arguments
-    = args.get_type_args ().size () + args.get_const_args ().size () + offs;
+    = args.get_type_args ().size () + args.get_const_args ().size ();
+  if (total_arguments < substitutions.size ())
+    {
+      offs = used_arguments.size ();
+      total_arguments += offs;
+    }
+
   if (total_arguments > substitutions.size ())
     {
       rich_location r (line_table, args.get_locus ());
diff --git a/gcc/testsuite/rust/compile/gat2.rs 
b/gcc/testsuite/rust/compile/gat2.rs
new file mode 100644
index 000000000000..2aead485ce2d
--- /dev/null
+++ b/gcc/testsuite/rust/compile/gat2.rs
@@ -0,0 +1,33 @@
+#![feature(lang_items)]
+
+#[lang = "sized"]
+trait Sized {}
+
+pub enum Option<T> {
+    Some(T),
+    None,
+}
+
+trait Foo {
+    type Bar<T>;
+
+    fn foo<T>(self) -> Self::Bar<T>;
+}
+
+impl Foo for i32 {
+    type Bar<T> = Option<T>;
+
+    fn foo<T>(self) -> Self::Bar<T> {
+        Option::None
+    }
+}
+
+pub fn main() -> i32 {
+    let a = 15;
+    let res: Option<i8> = a.foo::<i8>();
+
+    match res {
+        Option::None => 0,
+        Option::Some(_) => 1,
+    }
+}

Reply via email to