Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closure environments aren't added to a closure's parameters #2120

Closed
alehander92 opened this issue Aug 2, 2023 · 3 comments · Fixed by #4212
Closed

Closure environments aren't added to a closure's parameters #2120

alehander92 opened this issue Aug 2, 2023 · 3 comments · Fixed by #4212
Assignees
Labels
bug Something isn't working

Comments

@alehander92
Copy link
Contributor

alehander92 commented Aug 2, 2023

Aim

Try to assign to a value using a captured mutable reference to it in a closure:

(example by @jfecher ):

fn main() {
    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);
}

run nargo execute in this project

Expected Behavior

assertions pass correctly, no error

Bug

A lookup ssa ICE:

The application panicked (crashed).
Message:  lookup: variable not defined
Location: crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs:418

This is a bug. We may have already fixed this in newer versions of Nargo so try searching for similar issues at https://github.com/noir-lang/noir/issues/.
If there isn't an open issue for this bug, consider opening one at https://github.com/noir-lang/noir/issues/new?labels=bug&template=bug_report.yml

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⋮ 8 frames hidden ⋮
9: core::panicking::panic_display::h29316b4d8276b58d
at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/panicking.rs:139
10: core::panicking::panic_str::hdb14547b30ece385
at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/panicking.rs:123
11: core::option::expect_failed::h1c9d589f6e6349ed
at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/option.rs:1879
12: core::option::Option::expect::h4358a8adecd3fa90
at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/option.rs:741
13: noirc_evaluator::ssa_refactor::ssa_gen::context::FunctionContext::lookup::h9248c1e85392b3f2
at /home/al/noir/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs:418
14: noirc_evaluator::ssa_refactor::ssa_gen::context::FunctionContext::ident_lvalue::h139e67ba738052c6
at /home/al/noir/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs:520
15: noirc_evaluator::ssa_refactor::ssa_gen::context::FunctionContext::extract_current_value_recursive::hd12835bcfdf47006
at /home/al/noir/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs:543
16: noirc_evaluator::ssa_refactor::ssa_gen::context::FunctionContext::extract_current_value::h98d5aedb18d85c15
at /home/al/noir/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs:503
17: noirc_evaluator::ssa_refactor::ssa_gen::::codegen_assign::h0ae74c0605bce610
at /home/al/noir/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/mod.rs:433
18: noirc_evaluator::ssa_refactor::ssa_gen::::codegen_expression::hbed02fe13302f1d6
at /home/al/noir/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/mod.rs:81
19: noirc_evaluator::ssa_refactor::ssa_gen::::codegen_block::h1f4442e155ad6e66
at /home/al/noir/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/mod.rs:178
20: noirc_evaluator::ssa_refactor::ssa_gen::::codegen_expression::hbed02fe13302f1d6
at /home/al/noir/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/mod.rs:65
21: noirc_evaluator::ssa_refactor::ssa_gen::::codegen_function_body::h3a30d7fadb672b15
at /home/al/noir/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/mod.rs:56
22: noirc_evaluator::ssa_refactor::ssa_gen::generate_ssa::heca22e4cc66763e8
at /home/al/noir/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/mod.rs:46
23: noirc_evaluator::ssa_refactor::optimize_into_acir::hc5b0e6998a74d50a
at /home/al/noir/crates/noirc_evaluator/src/ssa_refactor.rs:43
24: noirc_evaluator::ssa_refactor::create_circuit::ha30ce1b0ce7adb30
at /home/al/noir/crates/noirc_evaluator/src/ssa_refactor.rs:87
25: noirc_driver::compile_no_check::h6c12ded194d20b7f
at /home/al/noir/crates/noirc_driver/src/lib.rs:277
26: noirc_driver::compile_main::he8205448c3365dc5
at /home/al/noir/crates/noirc_driver/src/lib.rs:166
27: nargo_cli::cli::compile_cmd::compile_circuit::hfe072f713cdd406f
at /home/al/noir/crates/nargo_cli/src/cli/compile_cmd.rs:136
28: nargo_cli::cli::execute_cmd::execute_package::hfc05356ee1f8318e
at /home/al/noir/crates/nargo_cli/src/cli/execute_cmd.rs:73
29: nargo_cli::cli::execute_cmd::run::h4995f87b92c6170a
at /home/al/noir/crates/nargo_cli/src/cli/execute_cmd.rs:51
30: nargo_cli::cli::start_cli::hed1ec5e662c4cab2
at /home/al/noir/crates/nargo_cli/src/cli/mod.rs:79
31: nargo::main::hd4a820dc8ea83d4b
at /home/al/noir/crates/nargo_cli/src/main.rs:14
32: core::ops::function::FnOnce::call_once::h341ee90d78f8ccb6
at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/ops/function.rs:251
⋮ 16 frames hidden ⋮

To Reproduce

No response

Installation Method

Compiled from source

Nargo Version

nargo 0.9.0 (git version hash: 39610af, is dirty: true)

Additional Context

originated here #1959 (comment)

Would you like to submit a PR for this Issue?

No

Support Needs

No response

@alehander92 alehander92 added the bug Something isn't working label Aug 2, 2023
@alehander92 alehander92 changed the title A problem with assigning using a captured mutable reference in closures An ICE when trying to assign using a captured mutable reference in closures Aug 2, 2023
@jfecher jfecher self-assigned this Aug 2, 2023
@jfecher
Copy link
Contributor

jfecher commented Aug 3, 2023

Looking at the lambda from this example in the monomorphized IR:

fn lambda$f1(y$l1: Field) -> () {
    *x1$l0 = y$l1
}

We can see that the "variable not found" panic is correct since x1 isn't defined within the lambda at all. It is part of the closure environment but was never added as a parameter to the lambda. I'll keep this issue open to track adding environment parameters to closures, but it is not an SSA or references related issue.

@jfecher jfecher assigned alehander92 and unassigned jfecher Aug 8, 2023
@jfecher jfecher changed the title An ICE when trying to assign using a captured mutable reference in closures Closure environments aren't added to a closure's parameters Aug 8, 2023
@jfecher
Copy link
Contributor

jfecher commented Aug 8, 2023

Also, this may help with debugging:

To get the above output I added a println of the monomorphized IR after the call to monomorphize in crates/noirc_driver/src/lib.rs

@alexvitkov
Copy link
Contributor

@jfecher I've submitted a PR that should fix this this: #2257
Here's the monomorphized AST for the lambda with the fix:

fn lambda$f1(mut env$l2: (&mut Field), y$l1: Field) -> () {
    *env$l2.0 = y$l1
}

However another unrelated codegen issue popped up, that still prevents this example from working properly: #2255

TomAFrench added a commit that referenced this issue Aug 10, 2023
* master: (80 commits)
  fix: properly capture lvalues in closure environments (#2120) (#2257)
  fix: Optimize contracts built by `nargo info` (#2259)
  chore: impl Display for DebugType (#2258)
  chore: update `noir_wasm` build process to match `acvm_js` (#2067)
  feat: Implement traits - parser support #2094 (#2230)
  chore: Refactor DefCollector duplicate errors (#2231)
  chore: Address clippy warnings (#2246)
  feat: Support `contract` package type in `nargo info` command (#2249)
  feat: Add slice append (#2241)
  chore: Bump `async-lsp` to v0.0.5 (#2186)
  chore: Move the remaining `nargo_cli` lib funcs into `nargo` crate (#2225)
  chore: Add test for eddsa (#2237)
  chore: Split `Nargo.toml` operations into separate package (#2224)
  fix(stdlib): correct `tecurve::contains` formula (#1821)
  feat: Remove `comptime` and warn upon usage (#2178)
  fix: Remove last vestige of array of structs to struct of arrays conversion (#2217)
  fix: Add foreign impl error (#2216)
  feat(nargo)!: Replace `--contracts` flag with `contract` package type (#2204)
  feat: Optimize `x < 0` for unsigned `x` to false (#2206)
  fix: Initialize numeric generics' type to a polymorphic integer when used in an expression (#2179)
  ...
TomAFrench added a commit that referenced this issue Aug 11, 2023
* master: (29 commits)
  feat(nargo): Add support for contracts in `nargo check` (#2267)
  chore(ci): Name wasm job more clearly (#2269)
  chore(ci): Create cache key with consideration to target (#2273)
  chore(ci): Run publish workflow against PRs (#2268)
  chore: Merge in contents of `build-nargo` repository (#2211)
  fix(lsp): Improve dependency resolution in context of `Nargo.toml` (#2226)
  chore: Remove unnecessary duplication in how we test Noir compiler (#2248)
  fix: properly capture lvalues in closure environments (#2120) (#2257)
  fix: Optimize contracts built by `nargo info` (#2259)
  chore: impl Display for DebugType (#2258)
  chore: update `noir_wasm` build process to match `acvm_js` (#2067)
  feat: Implement traits - parser support #2094 (#2230)
  chore: Refactor DefCollector duplicate errors (#2231)
  chore: Address clippy warnings (#2246)
  feat: Support `contract` package type in `nargo info` command (#2249)
  feat: Add slice append (#2241)
  chore: Bump `async-lsp` to v0.0.5 (#2186)
  chore: Move the remaining `nargo_cli` lib funcs into `nargo` crate (#2225)
  chore: Add test for eddsa (#2237)
  chore: Split `Nargo.toml` operations into separate package (#2224)
  ...
TomAFrench added a commit that referenced this issue Aug 11, 2023
* master:
  feat(nargo): Add support for contracts in `nargo check` (#2267)
  chore(ci): Name wasm job more clearly (#2269)
  chore(ci): Create cache key with consideration to target (#2273)
  chore(ci): Run publish workflow against PRs (#2268)
  chore: Merge in contents of `build-nargo` repository (#2211)
  fix(lsp): Improve dependency resolution in context of `Nargo.toml` (#2226)
  chore: Remove unnecessary duplication in how we test Noir compiler (#2248)
  fix: properly capture lvalues in closure environments (#2120) (#2257)
  fix: Optimize contracts built by `nargo info` (#2259)
  chore: impl Display for DebugType (#2258)
  chore: update `noir_wasm` build process to match `acvm_js` (#2067)
  feat: Implement traits - parser support #2094 (#2230)
  chore: Refactor DefCollector duplicate errors (#2231)
  chore: Address clippy warnings (#2246)
  feat: Support `contract` package type in `nargo info` command (#2249)
github-merge-queue bot pushed a commit that referenced this issue Feb 1, 2024
# Description

This issue has been resolved, presumably by
github.com//pull/2457, but this test no longer appears to
be in the repo so re-adding it to prevent regressions.

## Problem\*

Resolves #2120 

## Summary\*

This just adds a test.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
TomAFrench added a commit that referenced this issue Feb 2, 2024
* master: (22 commits)
  feat: remove predicate from `sort` intrinsic function (#4228)
  chore: add test for missing lambda closure environment (#2120) (#4212)
  chore(docs): Updates following `is_recursive` flag removal (#4199)
  fix: from field with constant values (#4226)
  feat: Option expect method (#4219)
  feat: Evaluation of dynamic assert messages (#4101)
  chore(docs): XOR typo in docs (#4223)
  fix: apply range constraints to return values from unconstrained functions (#4217)
  fix(lsp): replace panics with errors (#4209)
  feat: Improve Error Handling for Cargo in Bootstrap Script (#4211)
  fix: prevent declarations of blackbox functions outside of the stdlib (#4177)
  feat: disable unused variable checks on low-level and oracle functions (#4179)
  chore: Rename acir_docs.md to README.md (#4208)
  feat: remove replacement of boolean range opcodes with `AssertZero` opcodes (#4107)
  chore(docs): updating docs to match new recursion interfacee (#4187)
  feat!: Sync commits from `aztec-packages` (#4144)
  feat: multiply first to allow more ACIR gen optimizations (#4201)
  feat: Move bounded_vec into the noir stdlib (#4197)
  chore: simplify marking black box function outputs as solvable (#4194)
  chore(doc): Add docs for `assert_max_bit_size` (#4196)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants