From 3e79fc8dc406735bcbbf54f7b0068f898c38f6e3 Mon Sep 17 00:00:00 2001 From: Nobel Singh Date: Mon, 22 Apr 2024 00:26:12 +0545 Subject: [PATCH 1/2] Handle structs as scrutinee for match expressions gcc/rust/ChangeLog: * backend/rust-compile-expr.cc (check_match_scrutinee): Handle structs Signed-off-by: Nobel Singh --- gcc/rust/backend/rust-compile-expr.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 6a9bb73ffe0f..1bed4525bbba 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -954,8 +954,8 @@ check_match_scrutinee (HIR::MatchExpr &expr, Context *ctx) // this will need to change but for now the first pass implementation, // lets assert this is the case TyTy::ADTType *adt = static_cast (scrutinee_expr_tyty); - rust_assert (adt->is_enum ()); - rust_assert (adt->number_of_variants () > 0); + if (adt->is_enum ()) + rust_assert (adt->number_of_variants () > 0); } else if (scrutinee_kind == TyTy::TypeKind::FLOAT) { From 1906607fbbd5a1b35519fc21f079ba7276d93d89 Mon Sep 17 00:00:00 2001 From: Nobel Singh Date: Wed, 24 Apr 2024 02:07:52 +0545 Subject: [PATCH 2/2] Add testcases for handling struct as scrutinee for match expr gcc/testsuite/ChangeLog: * rust/compile/issue-2906.rs: New test. * rust/execute/torture/issue-2906.rs: New test. Signed-off-by: Nobel Singh --- gcc/testsuite/rust/compile/issue-2906.rs | 10 ++++++ .../rust/execute/torture/issue-2906.rs | 34 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 gcc/testsuite/rust/compile/issue-2906.rs create mode 100644 gcc/testsuite/rust/execute/torture/issue-2906.rs diff --git a/gcc/testsuite/rust/compile/issue-2906.rs b/gcc/testsuite/rust/compile/issue-2906.rs new file mode 100644 index 000000000000..20abcb095b8f --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-2906.rs @@ -0,0 +1,10 @@ +// { dg-warning "field is never read: .a." "" { target *-*-* } .-1 } +struct Foo { a: i32 } + +fn main() { + let a = Foo { a: 15 }; + + match a { + b => { } + } +} \ No newline at end of file diff --git a/gcc/testsuite/rust/execute/torture/issue-2906.rs b/gcc/testsuite/rust/execute/torture/issue-2906.rs new file mode 100644 index 000000000000..d3ca8ae1c5d4 --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/issue-2906.rs @@ -0,0 +1,34 @@ +// { dg-warning "field is never read: .x." "" { target *-*-* } .-1 } +// { dg-warning "field is never read: .y." "" { target *-*-* } .-2 } +struct Point { + x: u32, + y: u32, +} + +fn is_origin(p: Point) -> bool { + match p { + Point { x, y } => { + if x == 0 && y == 0 { + return true; + } + false + } + _ => false, + } +} + +fn main() -> i32 { + let p = Point { x: 0, y: 0 }; + let q = Point { x: 0, y: 1 }; + let mut retval = 2; + + if is_origin(p) { + retval -= 1; + } + + if !is_origin(q) { + retval -= 1; + } + + retval +}