diff options
author | Mahmoud Mohamed <mahadelr19@gmail.com> | 2023-03-15 01:06:46 +0300 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2023-03-15 11:04:50 +0000 |
commit | fc0addd1ffae33478cfc838f93e6685c0cbfa08c (patch) | |
tree | 8f32808c41bbfefe9f090fd202469d1727298591 | |
parent | 7cd4f74e3d485202a1433d487adbce43d271ebe5 (diff) | |
download | gcc-fc0addd1ffae33478cfc838f93e6685c0cbfa08c.zip gcc-fc0addd1ffae33478cfc838f93e6685c0cbfa08c.tar.gz gcc-fc0addd1ffae33478cfc838f93e6685c0cbfa08c.tar.bz2 |
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>
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.cc | 5 | ||||
-rw-r--r-- | gcc/testsuite/rust/execute/torture/closure4.rs | 22 | ||||
-rw-r--r-- | 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 200f3a2..b2a8fab 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 ¶m_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 0000000..07dca44 --- /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 0000000..4c9e755 --- /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 +} |