]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Implement compilation of IdentifierPattern's subpattern bindings
authorZhi Heng <yapzhhg@gmail.com>
Fri, 13 Jun 2025 14:45:23 +0000 (22:45 +0800)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:36:49 +0000 (16:36 +0200)
gcc/rust/ChangeLog:

* backend/rust-compile-pattern.cc: Add support for IdentifierPattern's
subpattern under CompilePatternBindings.

Signed-off-by: Yap Zhi Heng <yapzhhg@gmail.com>
gcc/rust/backend/rust-compile-pattern.cc
gcc/testsuite/rust/compile/match-identifierpattern-enum.rs [new file with mode: 0644]
gcc/testsuite/rust/execute/match-identifierpattern-enum.rs [new file with mode: 0644]

index e19aa678497d0ef51d470d5ea0f58ca9df53a4ae..bd3aea01b5379af3e1f4e87bac2c1a71b09d86e1 100644 (file)
@@ -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 (file)
index 0000000..c712667
--- /dev/null
@@ -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 (file)
index 0000000..c3a0f65
--- /dev/null
@@ -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
+}