]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Improve matching on non-enum ADTs
authorOwen Avery <powerboat9.gamer@gmail.com>
Fri, 3 May 2024 00:58:49 +0000 (20:58 -0400)
committerP-E-P <32375388+P-E-P@users.noreply.github.com>
Thu, 16 May 2024 09:10:48 +0000 (09:10 +0000)
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.gamer@gmail.com>
gcc/rust/backend/rust-compile-expr.cc
gcc/rust/backend/rust-compile-pattern.cc
gcc/testsuite/rust/compile/match-struct-path.rs [new file with mode: 0644]

index 6f37dd8a23ef9c82ec3386cd7a8b97414f4d746c..e53da939a7734b1511e1ea33539b7fc1fd14b919 100644 (file)
@@ -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)
     {
index 74adc3f8c177720ee3d9cbb800ca21069be40a85..c462a6d7a0257c31c00f3a3c954d5c7e8d842899 100644 (file)
@@ -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 (file)
index 0000000..acadcdb
--- /dev/null
@@ -0,0 +1,7 @@
+pub struct S;
+
+pub fn foo(v: S) {
+    match v {
+        S => ()
+    }
+}