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

commit r17-1076-gfcbffd54fed2543a7fe1e6cb00a985290ce4b3e0
Author: lishin <[email protected]>
Date:   Mon Apr 20 19:39:04 2026 +0100

    gccrs: Fix ICE cloning trait functions without return types
    
    Fixes Rust-GCC/gccrs#3972.
    
    Trait functions without an explicit return type can have a null
    `return_type` in `TraitFunctionDecl`. When such declarations are copied,
    the copy constructor and assignment operator currently try to clone the
    return type unconditionally, and this can lead to an ICE.
    
    Handle this case by keeping `nullptr` when there is no return type to
    clone. Also add a regression test for the example from Rust-GCC/gccrs#3972.
    
    gcc/rust/ChangeLog:
    
            * hir/tree/rust-hir-item.cc (TraitFunctionDecl::TraitFunctionDecl):
            Handle null return types in copy constructor.
            (TraitFunctionDecl::operator=): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/issue-3972.rs: New test.
    
    Signed-off-by: lishin <[email protected]>

Diff:
---
 gcc/rust/hir/tree/rust-hir-item.cc       |  7 +++++--
 gcc/testsuite/rust/compile/issue-3972.rs | 19 +++++++++++++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/hir/tree/rust-hir-item.cc 
b/gcc/rust/hir/tree/rust-hir-item.cc
index 5e5d9b7833ac..309118cff434 100644
--- a/gcc/rust/hir/tree/rust-hir-item.cc
+++ b/gcc/rust/hir/tree/rust-hir-item.cc
@@ -630,7 +630,8 @@ TraitFunctionDecl::TraitFunctionDecl (
 TraitFunctionDecl::TraitFunctionDecl (TraitFunctionDecl const &other)
   : qualifiers (other.qualifiers), function_name (other.function_name),
     function_params (other.function_params),
-    return_type (other.return_type->clone_type ()),
+    return_type (other.return_type != nullptr ? other.return_type->clone_type 
()
+                                             : nullptr),
     where_clause (other.where_clause), self (other.self)
 {
   generic_params.reserve (other.generic_params.size ());
@@ -644,7 +645,9 @@ TraitFunctionDecl::operator= (TraitFunctionDecl const 
&other)
   function_name = other.function_name;
   qualifiers = other.qualifiers;
   function_params = other.function_params;
-  return_type = other.return_type->clone_type ();
+  return_type
+    = other.return_type != nullptr ? other.return_type->clone_type () : 
nullptr;
+
   where_clause = other.where_clause;
   self = other.self;
 
diff --git a/gcc/testsuite/rust/compile/issue-3972.rs 
b/gcc/testsuite/rust/compile/issue-3972.rs
new file mode 100644
index 000000000000..466efc364853
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-3972.rs
@@ -0,0 +1,19 @@
+// { dg-options "-frust-compile-until=lowering" }
+#![feature(no_core)]
+#![no_core]
+
+struct Expr<const N: u32>;
+
+trait Trait0 {
+    fn required(
+        _: Expr<
+            {
+                trait Trait0 {
+                    fn required();
+                }
+
+                0
+            },
+        >,
+    );
+}

Reply via email to