]> git.ipfire.org Git - people/ms/gcc.git/commitdiff
hir: Improve pattern bindings handling in closure parameters
authorMahmoud Mohamed <mahadelr19@gmail.com>
Tue, 14 Mar 2023 22:06:46 +0000 (01:06 +0300)
committerPhilip Herron <philip.herron@embecosm.com>
Wed, 15 Mar 2023 11:04:50 +0000 (11:04 +0000)
gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::generate_closure_function):
handle closure parameters pattern bindings using CompilePatternBindings visitor

gcc/testsuite/ChangeLog:

* rust/execute/torture/closure4.rs: New test.
* rust/execute/torture/ref-pattern2.rs: New test.

Signed-off-by: Mahmoud Mohamed <mahadelr19@gmail.com>
gcc/rust/backend/rust-compile-expr.cc
gcc/testsuite/rust/execute/torture/closure4.rs [new file with mode: 0644]
gcc/testsuite/rust/execute/torture/ref-pattern2.rs [new file with mode: 0644]

index 200f3a2a07e4ecc9341e1356f1462e3a2b064122..b2a8fab44f5f832a57696519887f958cf556bad2 100644 (file)
@@ -2918,9 +2918,8 @@ CompileExpr::generate_closure_function (HIR::ClosureExpr &expr,
       tree compiled_param_var = ctx->get_backend ()->struct_field_expression (
        args_param_expr, i, closure_param.get_locus ());
 
-      const HIR::Pattern &param_pattern = *closure_param.get_pattern ();
-      ctx->insert_pattern_binding (
-       param_pattern.get_pattern_mappings ().get_hirid (), compiled_param_var);
+      CompilePatternBindings::Compile (closure_param.get_pattern ().get (),
+                                      compiled_param_var, ctx);
       i++;
     }
 
diff --git a/gcc/testsuite/rust/execute/torture/closure4.rs b/gcc/testsuite/rust/execute/torture/closure4.rs
new file mode 100644 (file)
index 0000000..07dca44
--- /dev/null
@@ -0,0 +1,22 @@
+#[lang = "fn_once"]
+pub trait FnOnce<Args> {
+    #[lang = "fn_once_output"]
+    type Output;
+
+    extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
+}
+
+struct Foo(i32, i32);
+
+fn main() -> i32 {
+  let foo = |&x: &i32, y: i32| -> i32 {
+    x + y
+  };
+  
+  let bar = |Foo(x, y): Foo| -> i32 {
+    x + y
+  };
+
+  let a = 4;
+  foo(&a, 2) + bar(Foo(100, 200)) - 306
+}
diff --git a/gcc/testsuite/rust/execute/torture/ref-pattern2.rs b/gcc/testsuite/rust/execute/torture/ref-pattern2.rs
new file mode 100644 (file)
index 0000000..4c9e755
--- /dev/null
@@ -0,0 +1,14 @@
+#[lang = "fn_once"]
+pub trait FnOnce<Args> {
+    #[lang = "fn_once_output"]
+    type Output;
+
+    extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
+}
+
+fn main() -> i32 {
+  let foo = |&&d: &&i32| -> i32 { d };
+
+  let x = &&5i32;
+  foo(x) - 5
+}