From: Zhi Heng <yapz...@gmail.com> gcc/rust/ChangeLog:
* backend/rust-compile-pattern.cc: Add support for IdentifierPattern's subpattern under CompilePatternBindings. Signed-off-by: Yap Zhi Heng <yapz...@gmail.com> --- gcc/rust/backend/rust-compile-pattern.cc | 6 ++++++ .../rust/compile/match-identifierpattern-enum.rs | 12 ++++++++++++ .../rust/execute/match-identifierpattern-enum.rs | 15 +++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 gcc/testsuite/rust/compile/match-identifierpattern-enum.rs create mode 100644 gcc/testsuite/rust/execute/match-identifierpattern-enum.rs diff --git a/gcc/rust/backend/rust-compile-pattern.cc b/gcc/rust/backend/rust-compile-pattern.cc index e19aa678497..bd3aea01b53 100644 --- a/gcc/rust/backend/rust-compile-pattern.cc +++ b/gcc/rust/backend/rust-compile-pattern.cc @@ -666,6 +666,12 @@ CompilePatternBindings::visit (HIR::ReferencePattern &pattern) void CompilePatternBindings::visit (HIR::IdentifierPattern &pattern) { + if (pattern.has_subpattern ()) + { + CompilePatternBindings::Compile (pattern.get_subpattern (), + match_scrutinee_expr, ctx); + } + if (!pattern.get_is_ref ()) { ctx->insert_pattern_binding (pattern.get_mappings ().get_hirid (), diff --git a/gcc/testsuite/rust/compile/match-identifierpattern-enum.rs b/gcc/testsuite/rust/compile/match-identifierpattern-enum.rs new file mode 100644 index 00000000000..c712667e27a --- /dev/null +++ b/gcc/testsuite/rust/compile/match-identifierpattern-enum.rs @@ -0,0 +1,12 @@ +enum Foo { + I(i32), +} + +fn main() { + let x = Foo::I(1); + + match x { + a @ Foo::I(b) => {}, + _ => {}, + }; +} diff --git a/gcc/testsuite/rust/execute/match-identifierpattern-enum.rs b/gcc/testsuite/rust/execute/match-identifierpattern-enum.rs new file mode 100644 index 00000000000..c3a0f65fe71 --- /dev/null +++ b/gcc/testsuite/rust/execute/match-identifierpattern-enum.rs @@ -0,0 +1,15 @@ +enum Foo { + I(i32), +} + +fn main() -> i32 { + let x = Foo::I(0); + let ret = 1; + + match x { + _ @ Foo::I(b) => { ret = b }, + _ => {}, + }; + + ret +} -- 2.49.0