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

Expression type is ambiguous #4994

Closed
LogvinovLeon opened this issue May 8, 2024 · 1 comment · Fixed by #5635
Closed

Expression type is ambiguous #4994

LogvinovLeon opened this issue May 8, 2024 · 1 comment · Fixed by #5635
Assignees
Labels
bug Something isn't working

Comments

@LogvinovLeon
Copy link
Contributor

LogvinovLeon commented May 8, 2024

Aim

Getting this code to compile:

fn main() {
    let a: [u8; 10] = [1; 10];
    let b: [Field; 10] = a.map(|x| x as Field);
}

Expected Behavior

Compiles

Bug

Does not compile:

error: Expression type is ambiguous
  ┌─ /Users/leonidlogvinov/Dev/ZK/plain-repro/src/main.nr:3:36
  │
3 │     let b: [Field; 10] = a.map(|x| x as Field);
  │                                    ---------- Type must be known at this point
  │

Aborting due to 1 previous error

To Reproduce

  1. Try to compile

Project Impact

Nice-to-have

Impact Context

No response

Workaround

Yes

Workaround Description

I can write a loop instead, but it's uglier & more verbose. Ideally - would like to be able to cast whole arrays

Additional Context

No response

Installation Method

Binary (noirup default)

Nargo Version

nargo version = 0.28.0

NoirJS Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@TomAFrench
Copy link
Member

A type hint fixes this for now.

fn main() {
    let a: [u8; 10] = [1; 10];
    let b: [Field; 10] = a.map(|x: u8| x as Field);
}

github-merge-queue bot pushed a commit that referenced this issue Jul 31, 2024
# Description

## Problem

Resolves #4994

## Summary

Before this PR, if you had something like:

```rust
x as Field
```

and x's type was an unbound type variable, the compiler would
immediately produce an error.

In this PR that check is delayed. In order to do that, the cast can only
succeed if `x` is an integer, a field... or a bool. We don't have a
polymorphic type that's one of those, so this PR introduces that.

(note: I think what I'm doing here is correct, but let me know if it's
not!)

Then, because we now have three different polymorphic types, I put them
all into an enum, mainly because some of this polymorphic handling was
sometimes duplicated, or almost the same with slight changes.

## Additional Context

I'm not sure I covered all the scenarios where `IntegerOrFieldOrBool`
should be handled. I'm not even sure I handled correctly the cases I
needed to handle.

This code now works fine:

```rust
fn main() {
    let a: [u8; 10] = [1; 10];
    let b: [Field; 10] = a.map(|x| x as Field);
}
```

Also this one:

```rust
fn main() {
    let a: [u8; 10] = [true; 10];
    let b: [Field; 10] = a.map(|x| x as Field);
}
```

However, this code correctly fails to compile, but the error is a bit
strange:

```rust
fn main() {
    let a = ["a"; 10];
    let b: [Field; 10] = a.map(|x| x as Field);
}
```

The error is:

```
error: Expected type fn(str<1>) -> _ with env _, found type fn(Integer | Field | bool) -> Field
  ┌─ src/main.nr:3:32
  │
3 │     let b: [Field; 10] = a.map(|x| x as Field);
  │                                --------------
```

but maybe that's expected?

(I also don't know what type we could show here instead of `Integer |
Field | bool`)

## 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 31, 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.

3 participants