Skip to content

Commit 0427d48

Browse files
Lishin1215philberty
authored andcommitted
gccrs: fix ICE in TyVar constructor
gcc/rust/ChangeLog: * typecheck/rust-tyty-util.cc (TyVar::TyVar): Add null check to avoid ICE. (TyVar::get_tyty): Return nullptr when lookup fails. (TyVar::clone): Handle null base type safely. (TyVar::monomorphized_clone): Add fallback for error types. gcc/testsuite/ChangeLog: * rust/compile/issue-3556.rs: New test. Signed-off-by: lishin <[email protected]>
1 parent ab46a3b commit 0427d48

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

gcc/rust/typecheck/rust-tyty-util.cc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ TyVar::TyVar (HirId ref) : ref (ref)
3030
auto context = Resolver::TypeCheckContext::get ();
3131
BaseType *lookup = nullptr;
3232
bool ok = context->lookup_type (ref, &lookup);
33-
rust_assert (ok);
33+
if (!ok || lookup == nullptr || lookup->get_kind () == TypeKind::ERROR)
34+
return;
3435
}
3536

3637
BaseType *
@@ -39,7 +40,8 @@ TyVar::get_tyty () const
3940
auto context = Resolver::TypeCheckContext::get ();
4041
BaseType *lookup = nullptr;
4142
bool ok = context->lookup_type (ref, &lookup);
42-
rust_assert (ok);
43+
if (!ok || lookup == nullptr)
44+
return nullptr;
4345
return lookup;
4446
}
4547

@@ -95,7 +97,10 @@ TyVar::subst_covariant_var (TyTy::BaseType *orig, TyTy::BaseType *subst)
9597
TyVar
9698
TyVar::clone () const
9799
{
98-
TyTy::BaseType *c = get_tyty ()->clone ();
100+
TyTy::BaseType *base = get_tyty ();
101+
if (base == nullptr || base->get_kind () == TypeKind::ERROR)
102+
return TyVar::get_implicit_infer_var (UNKNOWN_LOCATION);
103+
TyTy::BaseType *c = base->clone ();
99104
return TyVar (c->get_ref ());
100105
}
101106

@@ -105,6 +110,10 @@ TyVar::monomorphized_clone () const
105110
auto &mappings = Analysis::Mappings::get ();
106111
auto context = Resolver::TypeCheckContext::get ();
107112

113+
TyTy::BaseType *base = get_tyty ();
114+
if (base == nullptr || base->get_kind () == TypeKind::ERROR)
115+
return TyVar::get_implicit_infer_var (UNKNOWN_LOCATION);
116+
108117
// this needs a new hirid
109118
TyTy::BaseType *c = get_tyty ()->monomorphized_clone ();
110119
c->set_ref (mappings.get_next_hir_id ());
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
let ref mut a @ (ref mut b,);
3+
// { dg-error "expected T\\?, found tuple" "" { target *-*-* } .-1 }
4+
}

0 commit comments

Comments
 (0)