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

add turbofish-like syntax #4710

Closed
2 tasks done
Savio-Sou opened this issue Apr 3, 2024 Discussed in #3413 · 0 comments · Fixed by #3542
Closed
2 tasks done

add turbofish-like syntax #4710

Savio-Sou opened this issue Apr 3, 2024 Discussed in #3413 · 0 comments · Fixed by #3542
Assignees
Labels
enhancement New feature or request

Comments

@Savio-Sou
Copy link
Collaborator

Savio-Sou commented Apr 3, 2024

Discussed in https://github.com/orgs/noir-lang/discussions/3413

Originally posted by f01dab1e August 28, 2023

Problem

In Rust, the "turbofish" syntax (::<>) allows users to explicitly specify generic type parameters in function calls. This feature has proven to be useful for disambiguating type inference in certain scenarios.

Happy Case

fn main() {
    let mut slice = [];
    slice = slice.push_back(1);
    slice = slice.push_back(2);
    let slice = slice.as_array::<2>();
}

Alternatives Considered

No response

Additional Context

No response

Would you like to submit a PR for this Issue?

No

Support Needs

No response

Issues which can be worked around / fixed with turbofish

Preview Give feedback
  1. bug
  2. enhancement ssa
@Savio-Sou Savio-Sou added the enhancement New feature or request label Apr 3, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Apr 3, 2024
@TomAFrench TomAFrench linked a pull request Apr 26, 2024 that will close this issue
6 tasks
@TomAFrench TomAFrench assigned vezenovm and unassigned michaeljklein May 17, 2024
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir May 21, 2024
github-merge-queue bot pushed a commit that referenced this issue May 21, 2024
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

Working towards #4514 and fully
resolving #4710

## Summary\*

We need to accurately monomorphize functions with turbofish operators
where no function parameters or the return type using the specified
generic.

Without this change the following would pass:
```
fn main(x: Field, y: pub Field) {
    let mut hasher = PoseidonHasher::default();
    hasher.write(x);
    hasher.write(y);
    let poseidon_expected_hash = hasher.finish();
    assert(hash_simple_array::<PoseidonHasher>([x, y]) == poseidon_expected_hash);

    let mut hasher = Poseidon2Hasher::default();
    hasher.write(x);
    hasher.write(y);
    let poseidon2_expected_hash = hasher.finish();
    assert(hash_simple_array::<Poseidon2Hasher>([x, y]) == poseidon_expected_hash);
    assert(hash_simple_array::<Poseidon2Hasher>([x, y]) != poseidon2_expected_hash);
}

fn hash_simple_array<H>(input: [Field; 2]) -> Field where H: Hasher + Default {
    let mut hasher: H = H::default();
    hasher.write(input[0]);
    hasher.write(input[1]);
    hasher.finish()
}
```

Essentially any future invocations of `hash_simple_array` would use the
`PoseidonHasher`. We now correctly monomorphize the functions to use the
correct type.

## Additional Context



## Documentation\*

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

---------

Co-authored-by: Jake Fecher <[email protected]>
Co-authored-by: Jake Fecher <[email protected]>
Co-authored-by: Tom French <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants