]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Optionally pull in the inherited arguments when required
authorPhilip Herron <herron.philip@googlemail.com>
Sat, 17 Jan 2026 20:47:29 +0000 (20:47 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Wed, 8 Jul 2026 15:22:43 +0000 (17:22 +0200)
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 <herron.philip@googlemail.com>
gcc/rust/typecheck/rust-tyty-subst.cc
gcc/testsuite/rust/compile/gat2.rs [new file with mode: 0644]

index f3873b8e6843ec3f013cc71b5ceb10f43045f889..6ee2a60dfe7b6314f5fa24a91c1e8f61534dafd7 100644 (file)
@@ -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 (file)
index 0000000..2aead48
--- /dev/null
@@ -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,
+    }
+}