https://gcc.gnu.org/g:7b9e2b5d8b901e90b43d15acc62f02efa3c866bd

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

    gccrs: 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 ac85628d03c7..52318a973aa3 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 86063202bf8d..ffa1fa7f5dc1 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