aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorOwen Avery <powerboat9.gamer@gmail.com>2024-05-02 20:58:49 -0400
committerArthur Cohen <arthur.cohen@embecosm.com>2025-03-17 16:35:23 +0100
commit7b9e2b5d8b901e90b43d15acc62f02efa3c866bd (patch)
tree3e2074c43253a4528dc95ade9c1754ebd4a2b07a /gcc
parent539fa6e83798b621eb84eb2ad192c8e65bb1817a (diff)
downloadgcc-7b9e2b5d8b901e90b43d15acc62f02efa3c866bd.zip
gcc-7b9e2b5d8b901e90b43d15acc62f02efa3c866bd.tar.gz
gcc-7b9e2b5d8b901e90b43d15acc62f02efa3c866bd.tar.bz2
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.gamer@gmail.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/backend/rust-compile-expr.cc2
-rw-r--r--gcc/rust/backend/rust-compile-pattern.cc11
-rw-r--r--gcc/testsuite/rust/compile/match-struct-path.rs7
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 ac85628..52318a9 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 8606320..ffa1fa7 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 0000000..acadcdb
--- /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 => ()
+ }
+}