From: Zhi Heng <[email protected]>
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 <[email protected]>
---
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 +++++++++
.../rust/execute/torture/match-identifierpattern.rs | 10 ++++++++++
5 files changed, 36 insertions(+), 4 deletions(-)
create mode 100644 gcc/testsuite/rust/compile/match-identifierpattern.rs
create mode 100644
gcc/testsuite/rust/execute/torture/match-identifierpattern.rs
diff --git a/gcc/rust/backend/rust-compile-pattern.cc
b/gcc/rust/backend/rust-compile-pattern.cc
index 7b937c766e2..f81e910b2b2 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 85c82b8458f..4dd7d559e6e 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 102083bd84c..c1037ecd54e 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 00000000000..6c558ac065b
--- /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 00000000000..b102ad1ec41
--- /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
+}
--
2.49.0