public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] gccrs: fix ICE with bad match arm type
@ 2023-04-06 21:33 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-04-06 21:33 UTC (permalink / raw)
  To: gcc-cvs

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

commit bb035c384a08ef25dceadc33b11d2fb990209cb0
Author: Philip Herron <herron.philip@googlemail.com>
Date:   Wed Mar 29 15:57:56 2023 +0100

    gccrs: fix ICE with bad match arm type
    
    We expect to get some kind of ADT or Tuple type when computing this kind of
    match arms this adds a new diagnostic to check for this case instead of
    an assertion.
    
    Fixes #2029
    
    gcc/rust/ChangeLog:
    
            * typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): return early on bad type
            * typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::TypeCheckPattern):
            remove assertion in favor of check
            (TypeCheckPattern::visit): likewise
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/issue-2029.rs: New test.
    
    Signed-off-by: Philip Herron <herron.philip@googlemail.com>

Diff:
---
 gcc/rust/typecheck/rust-hir-type-check-expr.cc    |  2 ++
 gcc/rust/typecheck/rust-hir-type-check-pattern.cc | 28 +++++++++++++++--------
 gcc/testsuite/rust/compile/issue-2029.rs          | 13 +++++++++++
 3 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
index 117682b8a6b..912e0740f7f 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
@@ -1381,6 +1381,8 @@ TypeCheckExpr::visit (HIR::MatchExpr &expr)
 	{
 	  TyTy::BaseType *kase_arm_ty
 	    = TypeCheckPattern::Resolve (pattern.get (), scrutinee_tyty);
+	  if (kase_arm_ty->get_kind () == TyTy ::TypeKind::ERROR)
+	    return;
 
 	  TyTy::BaseType *checked_kase = unify_site (
 	    expr.get_mappings ().get_hirid (),
diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
index 44678610366..5c8fd9a38af 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
@@ -24,7 +24,7 @@ namespace Rust {
 namespace Resolver {
 
 TypeCheckPattern::TypeCheckPattern (TyTy::BaseType *parent)
-  : TypeCheckBase (), parent (parent), infered (nullptr)
+  : TypeCheckBase (), parent (parent), infered (new TyTy::ErrorType (0))
 {}
 
 TyTy::BaseType *
@@ -50,11 +50,16 @@ TypeCheckPattern::visit (HIR::PathInExpression &pattern)
 void
 TypeCheckPattern::visit (HIR::TupleStructPattern &pattern)
 {
-  infered = TypeCheckExpr::Resolve (&pattern.get_path ());
-  if (infered->get_kind () == TyTy::TypeKind::ERROR)
-    return;
+  TyTy::BaseType *pattern_ty = TypeCheckExpr::Resolve (&pattern.get_path ());
+  if (pattern_ty->get_kind () != TyTy::TypeKind::ADT)
+    {
+      rust_error_at (pattern.get_locus (),
+		     "expected tuple struct/variant, found: %s",
+		     pattern_ty->get_name ().c_str ());
+      return;
+    }
 
-  rust_assert (infered->get_kind () == TyTy::TypeKind::ADT);
+  infered = pattern_ty;
   TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (infered);
   rust_assert (adt->number_of_variants () > 0);
 
@@ -137,11 +142,16 @@ TypeCheckPattern::visit (HIR::TupleStructPattern &pattern)
 void
 TypeCheckPattern::visit (HIR::StructPattern &pattern)
 {
-  infered = TypeCheckExpr::Resolve (&pattern.get_path ());
-  if (infered->get_kind () == TyTy::TypeKind::ERROR)
-    return;
+  TyTy::BaseType *pattern_ty = TypeCheckExpr::Resolve (&pattern.get_path ());
+  if (pattern_ty->get_kind () != TyTy::TypeKind::ADT)
+    {
+      rust_error_at (pattern.get_locus (),
+		     "expected tuple struct/variant, found: %s",
+		     pattern_ty->get_name ().c_str ());
+      return;
+    }
 
-  rust_assert (infered->get_kind () == TyTy::TypeKind::ADT);
+  infered = pattern_ty;
   TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (infered);
   rust_assert (adt->number_of_variants () > 0);
 
diff --git a/gcc/testsuite/rust/compile/issue-2029.rs b/gcc/testsuite/rust/compile/issue-2029.rs
new file mode 100644
index 00000000000..80af2289256
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-2029.rs
@@ -0,0 +1,13 @@
+struct Foo(bool);
+fn foo(_: usize) -> Foo {
+    Foo(false)
+}
+
+fn main() {
+    match Foo(true) {
+        // { dg-error "failed to type resolve expression" "" { target *-*-* } .-1 }
+        foo(x)
+        // { dg-error "expected tuple struct/variant, found" "" { target *-*-* } .-1 }
+        => ()
+    }
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-04-06 21:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-06 21:33 [gcc/devel/rust/master] gccrs: fix ICE with bad match arm type Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).