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

Mutable enums can't be immutably passed to unconstrained functions #7636

Open
michaeljklein opened this issue Mar 7, 2025 · 2 comments
Open
Labels
bug Something isn't working

Comments

@michaeljklein
Copy link
Contributor

Aim

Attempted to pass a mutable enum to an unconstrained function that accepts an immutable instance of the type

pub enum Foo {
    Bar,
}

pub unconstrained fn baz(_x: Field) { }
pub unconstrained fn qux(_x: Foo) { }

fn main() {
    let mut x: Field = 0;
    let mut y: Foo = Foo::Bar;

    // Safety: this passes
    unsafe {
        baz(x);
    }

    // Safety: this has a "mutable reference" error
    unsafe {
        qux(y);
    }
}

Expected Behavior

Expected the mutable reference to be dereferenced when being passed to fn qux(_x: Foo).

Bug

Compilation fails for the enum case:

❯ clr; ~/.nargo/bin/nargo compile -Zenums

error: Cannot pass a mutable reference from a constrained runtime to an unconstrained runtime
   ┌─ src/main.nr:19:13
   │
19 │         qux(y);
   │             -
   │

Aborting due to 1 previous error

It appears that mutable references to enums aren't being handled correctly. E.g. I expected that I might be able to make an explicitly non-mutable copy of y, i.e.

    let z: Foo = y;
    qux(z);

But it fails with the same error.

To Reproduce

Workaround

Yes

Workaround Description

Using an unconstrained main function

Additional Context

No response

Project Impact

Nice-to-have

Blocker Context

No response

Nargo Version

nargo version = 1.0.0-beta.3 noirc version = 1.0.0-beta.3+37be49fd081f33dc7256d23cee8ccc0719c50a82 (git version hash: 37be49f, is dirty: false)

NoirJS Version

No response

Proving Backend Tooling & Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@michaeljklein michaeljklein added the bug Something isn't working label Mar 7, 2025
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Mar 7, 2025
@michaeljklein
Copy link
Contributor Author

michaeljklein commented Mar 7, 2025

Duplicate of #7558

@michaeljklein michaeljklein marked this as a duplicate of #7558 Mar 7, 2025
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Mar 7, 2025
@michaeljklein
Copy link
Contributor Author

This isn't a duplicate of #7558 because it fails with the Cannot pass a mutable reference from a constrained runtime to an unconstrained runtime error when Foo isn't a mutable reference:

pub enum foo {
    bar,
}

pub fn to_unmut(x: &mut foo) -> foo {
    let y: foo = *x;
    y
}

pub unconstrained fn unconstrained_fn(_x: foo) { }

fn main() {
    let mut baz = foo::bar;
    let qux: foo = to_unmut(&mut baz);
    // safety: test
    unsafe {
        unconstrained_fn(qux);
    }
}

@michaeljklein michaeljklein reopened this Mar 11, 2025
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

No branches or pull requests

1 participant