https://gcc.gnu.org/g:1c52754ee19991bc07c4023cad777a6a894bb64f

commit 1c52754ee19991bc07c4023cad777a6a894bb64f
Author: Owen Avery <powerboat9.ga...@gmail.com>
Date:   Thu May 2 20:58:49 2024 -0400

    Improve matching on non-enum ADTs
    
    gcc/rust/ChangeLog:
    
            * backend/rust-compile-expr.cc
            (check_match_scrutinee): Add assertion.
            * backend/rust-compile-pattern.cc
            (CompilePatternCheckExpr::visit):
            Handle HIR::PathInExpression matching a non-enum.
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/match-struct-path.rs: New test.
    
    Signed-off-by: Owen Avery <powerboat9.ga...@gmail.com>

Diff:
---
 gcc/rust/backend/rust-compile-expr.cc           |  2 ++
 gcc/rust/backend/rust-compile-pattern.cc        | 11 ++++++++---
 gcc/testsuite/rust/compile/match-struct-path.rs |  7 +++++++
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-expr.cc 
b/gcc/rust/backend/rust-compile-expr.cc
index 6f37dd8a23ef..e53da939a773 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -956,6 +956,8 @@ check_match_scrutinee (HIR::MatchExpr &expr, Context *ctx)
       TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (scrutinee_expr_tyty);
       if (adt->is_enum ())
        rust_assert (adt->number_of_variants () > 0);
+      else
+       rust_assert (adt->number_of_variants () == 1);
     }
   else if (scrutinee_kind == TyTy::TypeKind::FLOAT)
     {
diff --git a/gcc/rust/backend/rust-compile-pattern.cc 
b/gcc/rust/backend/rust-compile-pattern.cc
index 74adc3f8c177..c462a6d7a025 100644
--- a/gcc/rust/backend/rust-compile-pattern.cc
+++ b/gcc/rust/backend/rust-compile-pattern.cc
@@ -35,11 +35,16 @@ CompilePatternCheckExpr::visit (HIR::PathInExpression 
&pattern)
                                      &lookup);
   rust_assert (ok);
 
-  // this must be an enum
-  // TODO: might not be
+  // must be an ADT (?)
   rust_assert (lookup->get_kind () == TyTy::TypeKind::ADT);
   TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
-  rust_assert (adt->is_enum ());
+
+  // if this isn't an enum, always succeed
+  if (!adt->is_enum ())
+    {
+      check_expr = boolean_true_node;
+      return;
+    }
 
   // lookup the variant
   HirId variant_id;
diff --git a/gcc/testsuite/rust/compile/match-struct-path.rs 
b/gcc/testsuite/rust/compile/match-struct-path.rs
new file mode 100644
index 000000000000..acadcdb87ad4
--- /dev/null
+++ b/gcc/testsuite/rust/compile/match-struct-path.rs
@@ -0,0 +1,7 @@
+pub struct S;
+
+pub fn foo(v: S) {
+    match v {
+        S => ()
+    }
+}

Reply via email to