Skip to content

Commit 5c4de7f

Browse files
Lishin1215CohenArthur
authored andcommitted
gccrs: Fix ICE cloning trait functions without return types
Fixes #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 #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 <lishin1008@gmail.com>
1 parent 399c14d commit 5c4de7f

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

gcc/rust/hir/tree/rust-hir-item.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,8 @@ TraitFunctionDecl::TraitFunctionDecl (
630630
TraitFunctionDecl::TraitFunctionDecl (TraitFunctionDecl const &other)
631631
: qualifiers (other.qualifiers), function_name (other.function_name),
632632
function_params (other.function_params),
633-
return_type (other.return_type->clone_type ()),
633+
return_type (other.return_type != nullptr ? other.return_type->clone_type ()
634+
: nullptr),
634635
where_clause (other.where_clause), self (other.self)
635636
{
636637
generic_params.reserve (other.generic_params.size ());
@@ -644,7 +645,9 @@ TraitFunctionDecl::operator= (TraitFunctionDecl const &other)
644645
function_name = other.function_name;
645646
qualifiers = other.qualifiers;
646647
function_params = other.function_params;
647-
return_type = other.return_type->clone_type ();
648+
return_type
649+
= other.return_type != nullptr ? other.return_type->clone_type () : nullptr;
650+
648651
where_clause = other.where_clause;
649652
self = other.self;
650653

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// { dg-options "-frust-compile-until=lowering" }
2+
#![feature(no_core)]
3+
#![no_core]
4+
5+
struct Expr<const N: u32>;
6+
7+
trait Trait0 {
8+
fn required(
9+
_: Expr<
10+
{
11+
trait Trait0 {
12+
fn required();
13+
}
14+
15+
0
16+
},
17+
>,
18+
);
19+
}

0 commit comments

Comments
 (0)