https://gcc.gnu.org/g:0613241c5e841527753f09038956e38956320d35
commit r16-2845-g0613241c5e841527753f09038956e38956320d35 Author: Zhi Heng <yapz...@gmail.com> Date: Sat Jun 7 22:24:03 2025 +0800 gccrs: Support compilation of IdentifierPattern's subpatterns gcc/rust/ChangeLog: * backend/rust-compile-pattern.cc: Add CheckExpr compilation for IdentifierPattern with subpattern. * backend/rust-compile-pattern.h: Modify IdentifierPattern's visit func to support the above. * typecheck/rust-hir-type-check-pattern.cc: Add typechecking support for the changes above. Signed-off-by: Yap Zhi Heng <yapz...@gmail.com> Diff: --- gcc/rust/backend/rust-compile-pattern.cc | 11 +++++++++++ gcc/rust/backend/rust-compile-pattern.h | 5 +---- gcc/rust/typecheck/rust-hir-type-check-pattern.cc | 5 +++++ gcc/testsuite/rust/compile/match-identifierpattern.rs | 9 +++++++++ gcc/testsuite/rust/execute/torture/match-identifierpattern.rs | 10 ++++++++++ 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/gcc/rust/backend/rust-compile-pattern.cc b/gcc/rust/backend/rust-compile-pattern.cc index 7b937c766e2a..f81e910b2b2d 100644 --- a/gcc/rust/backend/rust-compile-pattern.cc +++ b/gcc/rust/backend/rust-compile-pattern.cc @@ -434,6 +434,17 @@ CompilePatternCheckExpr::visit (HIR::TuplePattern &pattern) } } +void CompilePatternCheckExpr::visit (HIR::IdentifierPattern &pattern) +{ + if (pattern.has_pattern_to_bind()) + { + check_expr = CompilePatternCheckExpr::Compile (pattern.get_to_bind(), match_scrutinee_expr, ctx); + } else + { + check_expr = boolean_true_node; + } +} + // setup the bindings void diff --git a/gcc/rust/backend/rust-compile-pattern.h b/gcc/rust/backend/rust-compile-pattern.h index 85c82b8458f8..4dd7d559e6eb 100644 --- a/gcc/rust/backend/rust-compile-pattern.h +++ b/gcc/rust/backend/rust-compile-pattern.h @@ -45,12 +45,9 @@ public: void visit (HIR::StructPattern &) override; void visit (HIR::TupleStructPattern &) override; void visit (HIR::TuplePattern &) override; + void visit (HIR::IdentifierPattern &) override; // Always succeeds - void visit (HIR::IdentifierPattern &) override - { - check_expr = boolean_true_node; - } void visit (HIR::WildcardPattern &) override { check_expr = boolean_true_node; diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc index 102083bd84c3..c1037ecd54e8 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc @@ -546,6 +546,11 @@ TypeCheckPattern::visit (HIR::RangePattern &pattern) void TypeCheckPattern::visit (HIR::IdentifierPattern &pattern) { + if (pattern.has_pattern_to_bind ()) + { + TypeCheckPattern::Resolve (pattern.get_to_bind (), parent); + } + if (!pattern.get_is_ref ()) { infered = parent; diff --git a/gcc/testsuite/rust/compile/match-identifierpattern.rs b/gcc/testsuite/rust/compile/match-identifierpattern.rs new file mode 100644 index 000000000000..6c558ac065b9 --- /dev/null +++ b/gcc/testsuite/rust/compile/match-identifierpattern.rs @@ -0,0 +1,9 @@ +fn main() { + let x = 1; + + match x { + 2 => {}, + a @ 3 => {}, + _ => {}, + } +} diff --git a/gcc/testsuite/rust/execute/torture/match-identifierpattern.rs b/gcc/testsuite/rust/execute/torture/match-identifierpattern.rs new file mode 100644 index 000000000000..b102ad1ec417 --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/match-identifierpattern.rs @@ -0,0 +1,10 @@ +fn main() -> i32 { + let mut x = 2; + + match x { + a @ 2 => { x = a + 1 }, + _ => {} + } + + x - 3 +}