]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Implement let statement support for IdentifierPattern's subpatterns
authorYap Zhi Heng <yapzhhg@gmail.com>
Tue, 12 Aug 2025 13:58:18 +0000 (21:58 +0800)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 30 Oct 2025 19:58:40 +0000 (20:58 +0100)
Trimmed GIMPLE code gen for let-identifierpattern-subpattern.rs

...
      RUSTTMP.1.__0 = 0;
      RUSTTMP.1.__1 = 2;
      RUSTTMP.1.__2 = 3;
      bar = RUSTTMP.1.__0;
      RUSTTMP.2 = RUSTTMP.1.__1;
      RUSTTMP.3 = RUSTTMP.1.__2;
      foo.__0 = 0;
      foo.__1 = 2;
      foo.__2 = 3;
      ret = 1;
      RUSTTMP.5 = foo;
      _1 = RUSTTMP.5.__0;
      _2 = _1 == 0;
      _3 = RUSTTMP.5.__1;
      _4 = _3 == 2;
      _5 = _2 & _4;
      _6 = RUSTTMP.5.__2;
      _7 = _6 == 3;
      _8 = _5 & _7;
      if (_8 != 0) goto <D.143>; else goto <D.144>;
      <D.143>:
      {
        {
          ret = bar;
        }
        goto <D.137>;
      }
...

gcc/rust/ChangeLog:

* backend/rust-compile-pattern.cc(CompilePatternLet::visit(IdentifierPattern)):
Add support for subpatterns.
* backend/rust-compile-var-decl.h(CompileVarDecl::visit(IdentifierPattern)):
Implement compilation for subpatterns.

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

index 16f8b52679c111b26479e08727398f116d661f6f..577f8de9c93bd9018b1354c8768e773552f363d7 100644 (file)
@@ -992,6 +992,11 @@ CompilePatternLet::visit (HIR::IdentifierPattern &pattern)
     }
   else
     {
+      if (pattern.has_subpattern ())
+       {
+         CompilePatternLet::Compile (&pattern.get_subpattern (), init_expr, ty,
+                                     rval_locus, ctx);
+       }
       auto s = Backend::init_statement (fnctx.fndecl, var, init_expr);
       ctx->add_statement (s);
     }
index 4ab02a8b760d7e713ac7be820fc3e3dfc20d115c..15f3ebb3a6a726d8a3f7e481046db1a7cd3dd790 100644 (file)
@@ -64,6 +64,15 @@ public:
     ctx->insert_var_decl (stmt_id, var);
 
     vars.push_back (var);
+
+    if (pattern.has_subpattern ())
+      {
+       auto subpattern_vars
+         = CompileVarDecl::compile (fndecl, translated_type,
+                                    &pattern.get_subpattern (), ctx);
+       vars.insert (vars.end (), subpattern_vars.begin (),
+                    subpattern_vars.end ());
+      }
   }
 
   void visit (HIR::TuplePattern &pattern) override
diff --git a/gcc/testsuite/rust/execute/torture/let-identifierpattern-subpattern.rs b/gcc/testsuite/rust/execute/torture/let-identifierpattern-subpattern.rs
new file mode 100644 (file)
index 0000000..fa1f56e
--- /dev/null
@@ -0,0 +1,11 @@
+fn main() -> i32 {
+    let foo @ (bar, _, _) = (0, 2, 3);
+    let mut ret = 1;
+
+    match foo {
+        (0, 2, 3) => { ret = bar },
+        _ => {}
+    }
+
+    ret
+}
\ No newline at end of file