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

Passing a mut reference into an unconstrained function crashes the compiler #4245

Closed
zac-williamson opened this issue Feb 2, 2024 · 1 comment · Fixed by #4281
Closed
Assignees
Labels
bug Something isn't working compiler

Comments

@zac-williamson
Copy link

Aim

Trying to work around #4244

Code:

use dep::std::field::bn254::assert_gt;

struct ListItem {
    key: Field,
    previous: Field,
    next: Field,
}

impl ListItem {
    fn default() -> ListItem {
        ListItem {
            key: 0,
            previous: 0,
            next: 0,
        }
    }
}

struct Map<Size> {
    entries: [ListItem; Size],
    size: Field,
    is_empty: bool,
    first_index: Field,
    last_index: Field,
}

unconstrained fn find_previous_key_location<Size>(map: Map<Size>, key: Field) -> (Field) {
    let mut found_index: Field = 0;
    found_index
}

unconstrained fn find_previous_key_location_nocopy<Size>(key: Field) -> (Field) {
    let mut found_index: Field = 0;
    found_index
}

unconstrained fn find_previous_key_location_crash<Size>(map: &mut Map<Size>, key: Field) -> (Field) {
    let mut found_index: Field = 0;
    found_index
}

impl<Size> Map<Size> {
    fn default() -> Map<Size> {
        Map{
            entries: [ListItem::default(); Size], // todo fix
            size: 0,
            is_empty: true,
            first_index: 0,
            last_index: 0,
        }
    }


    fn insert_crashy(&mut self, key: Field) {
        let previous_indexx = find_previous_key_location_crash(self, key);
        self.entries[previous_indexx].key = key;
    }

}

fn main(x: Field, y: pub Field) {
    let mut test_list: Map<50> = Map::default();
    test_list.insert_crashy(x);
    test_list.insert_crashy(x + y);
    test_list.insert_crashy(x + y * y);
}

Expected Behavior

no crash. Sad face.

Bug

compiler crashes

To Reproduce

  1. nargo info
  2. observe crash

Installation Method

None

Nargo Version

0.23.0

Additional Context

No response

Would you like to submit a PR for this Issue?

No

Support Needs

No response

@zac-williamson zac-williamson added the bug Something isn't working label Feb 2, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Feb 2, 2024
@jfecher
Copy link
Contributor

jfecher commented Feb 2, 2024

It isn't possible to pass a mutable reference from a constrained function into an unconstrained one since any mutations performed on it wouldn't be reflected back in the constrained function. For this reason, passing a mutable reference through this boundary is banned, although we should issue a compiler error rather than panic here.

@vezenovm vezenovm self-assigned this Feb 6, 2024
github-merge-queue bot pushed a commit that referenced this issue Feb 7, 2024
# Description

## Problem\*

Resolves #4245 

## Summary\*

This adds a type check for call expressions to check the arguments of a
call to an unconstrained function from a constrained function. As we
have two different runtime environments this makes type checking a bit
more complicated. As a followup to this PR we could try and catch the
error added here (#4280) sooner.
However, if the unconstrained function uses generics we must still fall
back to the "type check" in SSA/ACIR gen from PR #4280.

This PR now errors for the snippet in issue with the below using `nargo
check`:

<img width="900" alt="Screenshot 2024-02-06 at 3 36 27 PM"
src="https://github.com/noir-lang/noir/assets/43554004/6c037458-3182-4ea4-8470-050a1734b875">


## Additional Context



## Documentation\*

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

# PR Checklist\*

- [ ] I have tested the changes locally.
- [ ] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants