From: Owen Avery Date: Fri, 3 May 2024 00:58:49 +0000 (-0400) Subject: gccrs: Improve matching on non-enum ADTs X-Git-Tag: basepoints/gcc-16~1448 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7b9e2b5d8b901e90b43d15acc62f02efa3c866bd;p=thirdparty%2Fgcc.git 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 --- 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 (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 (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 => () + } +}