From 217cb7f14aade88b57eef9c59bf5b148b7f820b5 Mon Sep 17 00:00:00 2001 From: Alex Vitkov Date: Mon, 28 Aug 2023 08:57:48 +0300 Subject: [PATCH 1/2] fix: make closure env ast::Ident mutable --- crates/noirc_frontend/src/monomorphization/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/noirc_frontend/src/monomorphization/mod.rs b/crates/noirc_frontend/src/monomorphization/mod.rs index 86a0ca1f20c..70798b9cae6 100644 --- a/crates/noirc_frontend/src/monomorphization/mod.rs +++ b/crates/noirc_frontend/src/monomorphization/mod.rs @@ -1126,7 +1126,7 @@ impl<'interner> Monomorphizer<'interner> { }); let location = None; // TODO: This should match the location of the lambda expression - let mutable = false; + let mutable = true; let definition = Definition::Local(env_local_id); let env_ident = ast::Ident { From 8cbcb19c4d18a6d61b38332e2766e4fed06bb296 Mon Sep 17 00:00:00 2001 From: Alex Vitkov Date: Mon, 28 Aug 2023 09:03:03 +0300 Subject: [PATCH 2/2] test: add test for lvalue capture --- .../execution_success/closures_mut_ref/src/main.nr | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/nargo_cli/tests/execution_success/closures_mut_ref/src/main.nr b/crates/nargo_cli/tests/execution_success/closures_mut_ref/src/main.nr index 1f439a3b43d..2888745a96e 100644 --- a/crates/nargo_cli/tests/execution_success/closures_mut_ref/src/main.nr +++ b/crates/nargo_cli/tests/execution_success/closures_mut_ref/src/main.nr @@ -15,4 +15,17 @@ fn main(mut x: Field) { add2(&mut x); assert(x == 3); + issue_2120(); } + +// https://github.com/noir-lang/noir/issues/2120 +fn issue_2120() { + let x1 = &mut 42; + let set_x1 = |y| { *x1 = y; }; + + assert(*x1 == 42); + set_x1(44); + assert(*x1 == 44); + set_x1(*x1); + assert(*x1 == 44); +} \ No newline at end of file