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

Turbofish for numeric generics in nested function call fails #5442

Closed
michaeljklein opened this issue Jul 8, 2024 · 0 comments · Fixed by #5448
Closed

Turbofish for numeric generics in nested function call fails #5442

michaeljklein opened this issue Jul 8, 2024 · 0 comments · Fixed by #5448
Assignees
Labels
bug Something isn't working

Comments

@michaeljklein
Copy link
Contributor

Aim

fn foo<let N: u32>() -> [u8; N] {
    [0; N]
}

fn bar<let N: u32>() -> [u8; N] {
    foo::<N>()
}

global M: u32 = 3;

fn main() {
    println(bar::<M>());
}

Expected Behavior

Program to compile and execute successfully.

Note in the workaround that using turbofish for bar in main works.

Bug

error: expected type, found numeric generic parameter N
  ┌─ /Users/michaelklein/Coding/noir/bug_from_regex/src/main.nr:6:11
  │
6 │     foo::<N>()
  │           - not a type

To Reproduce

Project Impact

Blocker

Impact Context

Medium-blocker: there's a lot of code that uses numeric generics and the workarounds are fairly heavy

Workaround

Yes

Workaround Description

Phantom data that's expected to compile to () can be passed around:

struct Phantom<let N: u32> { }

fn foo<let N: u32>(x: Phantom<N>) -> [u8; N] {
    [0; N]
}

fn bar<let N: u32>(x: Phantom<N>) -> [u8; N] {
    foo(x)
}

global M: u32 = 3;

fn main() {
    let phantom: Phantom<M> = Phantom {};
    println(bar::<M>(phantom));
}

Additional Context

No response

Installation Method

Compiled from source

Nargo Version

nargo version = 0.31.0 noirc version = 0.31.0+b58a034a36dab6d17e10f2fcceab93e9073f53ed (git version hash: b58a034, is dirty: false)

NoirJS 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 Jul 8, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Jul 8, 2024
@vezenovm vezenovm self-assigned this Jul 8, 2024
github-merge-queue bot pushed a commit that referenced this issue Jul 9, 2024
…5448)

# Description

## Problem\*

Resolves #5442 

## Summary\*

In #5155 we make sure that we make sure we resolve against the correct
type kind in various places such as when resolving trait bound generics
or trait impl generics. This was not done for function calls. Now when
resolving turbofish generics, rather than calling `resolve_type` in all
cases aside type expressions, we check whether the variable we are
elaborating is a function and fetch its already resolved generics and
pass that in as the expected kind.

To keep the resolution logic the simple (I just zipped together the
`direct_generics` from the function meta and the user specified
turbofish generics), as `zip` uses the shortest iterator, I also added
two checks for whether we have the correct turbofish count. We now have
multiple of these checks rather than in just `instantiate`, but I felt
it was the simplest logic and would lead to the least potential future
confusion.

I also have expanded the bug identified in the issue to also include
turbofish generics for method calls. I have also included this as part
of my test for this PR.

## Additional Context



## Documentation\*

Check one:
- [X] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** 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.

---------

Co-authored-by: jfecher <jake@aztecprotocol.com>
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Jul 9, 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
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants