]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Support compilation of IdentifierPattern's subpatterns
authorZhi Heng <yapzhhg@gmail.com>
Sat, 7 Jun 2025 14:24:03 +0000 (22:24 +0800)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:36:46 +0000 (16:36 +0200)
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 <yapzhhg@gmail.com>
gcc/rust/backend/rust-compile-pattern.cc
gcc/rust/backend/rust-compile-pattern.h
gcc/rust/typecheck/rust-hir-type-check-pattern.cc
gcc/testsuite/rust/compile/match-identifierpattern.rs [new file with mode: 0644]
gcc/testsuite/rust/execute/torture/match-identifierpattern.rs [new file with mode: 0644]

index 7b937c766e2aa9a2278775a3958d0716feb1bd48..f81e910b2b2d3b18d2839cc6286cfd0a7f9a9cce 100644 (file)
@@ -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
index 85c82b8458f840e83aeaef091f7ceae9b7827b77..4dd7d559e6eb581a894d6a1152ec403b5b305b2d 100644 (file)
@@ -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;
index 102083bd84c33e93f1d530413ac032c33d1b36fe..c1037ecd54e8064b6e2d08c4948a9e5cf67c9b8e 100644 (file)
@@ -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 (file)
index 0000000..6c558ac
--- /dev/null
@@ -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 (file)
index 0000000..b102ad1
--- /dev/null
@@ -0,0 +1,10 @@
+fn main() -> i32 {
+    let mut x = 2;
+
+    match x {
+        a @ 2 => { x = a + 1 },
+        _ => {}
+    }
+
+    x - 3
+}