From: Owen Avery <[email protected]>
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 <[email protected]>
---
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(-)
create mode 100644 gcc/testsuite/rust/compile/match-struct-path.rs
diff --git a/gcc/rust/backend/rust-compile-expr.cc
b/gcc/rust/backend/rust-compile-expr.cc
index ac85628d03c..52318a973aa 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 86063202bf8..ffa1fa7f5dc 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 00000000000..acadcdb87ad
--- /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 => ()
+ }
+}
--
2.45.2