public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] hir: Improve pattern bindings handling in closure parameters
@ 2023-03-20  7:22 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-03-20  7:22 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:fc0addd1ffae33478cfc838f93e6685c0cbfa08c

commit fc0addd1ffae33478cfc838f93e6685c0cbfa08c
Author: Mahmoud Mohamed <mahadelr19@gmail.com>
Date:   Wed Mar 15 01:06:46 2023 +0300

    hir: Improve pattern bindings handling in closure parameters
    
    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>

Diff:
---
 gcc/rust/backend/rust-compile-expr.cc              |  5 ++---
 gcc/testsuite/rust/execute/torture/closure4.rs     | 22 ++++++++++++++++++++++
 gcc/testsuite/rust/execute/torture/ref-pattern2.rs | 14 ++++++++++++++
 3 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc
index 200f3a2a07e..b2a8fab44f5 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -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
index 00000000000..07dca44308d
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/closure4.rs
@@ -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
index 00000000000..4c9e755ba84
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/ref-pattern2.rs
@@ -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
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-03-20  7:22 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-20  7:22 [gcc/devel/rust/master] hir: Improve pattern bindings handling in closure parameters Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).