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

Unmutable environment #4795

Open
aajj999 opened this issue Apr 12, 2024 · 2 comments · May be fixed by #5016
Open

Unmutable environment #4795

aajj999 opened this issue Apr 12, 2024 · 2 comments · May be fixed by #5016
Assignees
Labels
bug Something isn't working

Comments

@aajj999
Copy link

aajj999 commented Apr 12, 2024

Aim

Change mutable variables given in environment to lambda.
Mutable variable is created and then given as environment to lambda function. Lambda function changes this variable's value.

Expected Behavior

Mutable variables change their values outside of lambda function when they are in this function's environment.

Bug

Variables don't change their value.

To Reproduce

  1. Use repository: https://github.com/vlayer-xyz/repro-derived-constant
  2. Run nargo execute
  3. See the result

Project Impact

Nice-to-have

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

No response

Installation Method

Binary (noirup default)

Nargo Version

nargo version = 0.26.0 noirc version = 0.26.0+c46b164ce56e6a8f81255fb17eb6539bd040f336

NoirJS Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@aajj999 aajj999 added the bug Something isn't working label Apr 12, 2024
@jfecher
Copy link
Contributor

jfecher commented Apr 30, 2024

Repro is for an unrelated issue.

A repro for this issue is:

fn main() {
    let mut x = 3;

    let f = || {
        x += 2;
    };

    f();
    assert(x == 5);
}

The issue is that we allow x += 2; at all within the lambda since captures are meant to be immutable.
To capture mutable variables a mutable reference must be used:

fn main() {
    let x = &mut 3;

    let f = || {
        *x += 2;
    };

    f();
    assert(*x == 5);
}

@michaeljklein
Copy link
Contributor

Plan: LambdaContext appears to store these

@michaeljklein michaeljklein linked a pull request May 10, 2024 that will close this issue
5 tasks
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
Status: 📋 Backlog
Development

Successfully merging a pull request may close this issue.

3 participants