From: lishin <[email protected]>

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]>
---
 gcc/rust/hir/tree/rust-hir-item.cc       |  7 +++++--
 gcc/testsuite/rust/compile/issue-3972.rs | 19 +++++++++++++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/issue-3972.rs

diff --git a/gcc/rust/hir/tree/rust-hir-item.cc 
b/gcc/rust/hir/tree/rust-hir-item.cc
index 5e5d9b7833a..309118cff43 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 00000000000..466efc36485
--- /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
+            },
+        >,
+    );
+}
-- 
2.50.1

Reply via email to