]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: fix ICE in TyVar constructor
authorlishin <lishin1008@gmail.com>
Sat, 27 Sep 2025 01:10:42 +0000 (02:10 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 30 Oct 2025 20:30:56 +0000 (21:30 +0100)
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 <lishin1008@gmail.com>
gcc/rust/typecheck/rust-tyty-util.cc
gcc/testsuite/rust/compile/issue-3556.rs [new file with mode: 0644]

index c6c740b2cba8526b9643e4305464f74118f55ef5..72761d9842b3c3948906b8c82dbc552ce248f98d 100644 (file)
@@ -30,7 +30,8 @@ TyVar::TyVar (HirId ref) : ref (ref)
   auto context = Resolver::TypeCheckContext::get ();
   BaseType *lookup = nullptr;
   bool ok = context->lookup_type (ref, &lookup);
-  rust_assert (ok);
+  if (!ok || lookup == nullptr || lookup->get_kind () == TypeKind::ERROR)
+    return;
 }
 
 BaseType *
@@ -39,7 +40,8 @@ TyVar::get_tyty () const
   auto context = Resolver::TypeCheckContext::get ();
   BaseType *lookup = nullptr;
   bool ok = context->lookup_type (ref, &lookup);
-  rust_assert (ok);
+  if (!ok || lookup == nullptr)
+    return nullptr;
   return lookup;
 }
 
@@ -95,7 +97,10 @@ TyVar::subst_covariant_var (TyTy::BaseType *orig, TyTy::BaseType *subst)
 TyVar
 TyVar::clone () const
 {
-  TyTy::BaseType *c = get_tyty ()->clone ();
+  TyTy::BaseType *base = get_tyty ();
+  if (base == nullptr || base->get_kind () == TypeKind::ERROR)
+    return TyVar::get_implicit_infer_var (UNKNOWN_LOCATION);
+  TyTy::BaseType *c = base->clone ();
   return TyVar (c->get_ref ());
 }
 
@@ -105,6 +110,10 @@ TyVar::monomorphized_clone () const
   auto &mappings = Analysis::Mappings::get ();
   auto context = Resolver::TypeCheckContext::get ();
 
+  TyTy::BaseType *base = get_tyty ();
+  if (base == nullptr || base->get_kind () == TypeKind::ERROR)
+    return TyVar::get_implicit_infer_var (UNKNOWN_LOCATION);
+
   // this needs a new hirid
   TyTy::BaseType *c = get_tyty ()->monomorphized_clone ();
   c->set_ref (mappings.get_next_hir_id ());
diff --git a/gcc/testsuite/rust/compile/issue-3556.rs b/gcc/testsuite/rust/compile/issue-3556.rs
new file mode 100644 (file)
index 0000000..be7d85a
--- /dev/null
@@ -0,0 +1,4 @@
+fn main() {
+    let ref mut a @ (ref mut b,);
+    // { dg-error "expected T\\?, found tuple" "" { target *-*-* } .-1 }
+}
\ No newline at end of file