]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Improve matching on non-enum ADTs
authorOwen Avery <powerboat9.gamer@gmail.com>
Fri, 3 May 2024 00:58:49 +0000 (20:58 -0400)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 17 Mar 2025 15:35:23 +0000 (16:35 +0100)
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 ac85628d03c742599bb84274a364c4b08ff94540..52318a973aa37f012190c3800596e636acbca1ce 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 86063202bf8db5f4448853f520ee2bef6309e240..ffa1fa7f5dc14800e770877b199dbeae11e41575 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 => ()
+    }
+}