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

Incorrect type inference for bit_not on unsigned integer #2548

Closed
gladiatorcasino opened this issue Sep 4, 2023 · 4 comments · Fixed by #5589
Closed

Incorrect type inference for bit_not on unsigned integer #2548

gladiatorcasino opened this issue Sep 4, 2023 · 4 comments · Fixed by #5589
Assignees
Labels
bug Something isn't working compiler frontend `noirc_frontend` crate

Comments

@gladiatorcasino
Copy link

Aim

The return type of the bit_not operator is not correctly inferred when used on an unsigned integer.
Given the statement:

let a: u32 = 3 & !1;

The type of 3 is inferred to be Field, and the type of !1 is inferred to be bool. Both of these are incorrect. The compiler throws an error message complaining that the & operator is operating on two imcompatible types: Field and bool.

Without the !1 part, i.e., let a: u32 = 3 & 1;, the compiler correctly infers all the data types and the compilation succeeds.

Expected Behavior

The compiler should correctly handle the bitwise not operator and infer the type as unsigned integer.

Bug

Run nargo new bit_not and replace the contents of src/main.rs with the following:

fn main() {
    let a: u32 = 3 & !1;
}

Run nargo check and observe the following error message:

warning: unused variable a
  ┌─ /home/gladiatorcasino/bit_not/src/main.nr:2:9
  │
2 │     let a: u32 = 3 & !1;
  │         - unused variable
  │

error: Expected type bool, found type Field
  ┌─ /home/gladiatorcasino/bit_not/src/main.nr:2:23
  │
2 │     let a: u32 = 3 & !1;
  │                       -
  │

error: Types in a binary operation should match, but found Field and bool
  ┌─ /home/gladiatorcasino/bit_not/src/main.nr:2:18
  │
2 │     let a: u32 = 3 & !1;
  │                  ------
  │

Error: Aborting due to 2 previous errors

Location:
    crates/nargo_cli/src/cli/mod.rs:79:5

To Reproduce

  1. Run nargo new bit_not and replace the contents of src/main.rs with the code above.
  2. Run nargo check.

Installation Method

Binary

Nargo Version

nargo 0.10.5 (git version hash: 9fe4cfd, is dirty: false)

Additional Context

No response

Would you like to submit a PR for this Issue?

No

Support Needs

No response

@gladiatorcasino gladiatorcasino added the bug Something isn't working label Sep 4, 2023
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Sep 4, 2023
@jfecher jfecher added compiler frontend `noirc_frontend` crate P-MEDIUM labels Sep 5, 2023
@TomAFrench
Copy link
Member

Confirming this issue still exists in master.

@TomAFrench
Copy link
Member

TomAFrench commented Feb 19, 2024

There's a good chance this is fixed by @michaeljklein's recent work. Will check today.

Edit: it wasn't

@asterite
Copy link
Collaborator

This seems to be working fine now. I think it's because we now let ! dispatch to a trait and so !1 will make 1 be any integer type that supports the ! operator.

To see the deduced types, with inlay hints:

image

@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Jul 23, 2024
@asterite
Copy link
Collaborator

Reopening to add a regression test.

@asterite asterite reopened this Jul 23, 2024
github-merge-queue bot pushed a commit that referenced this issue Jul 23, 2024
# Description

## Problem

Resolves #2548

## Summary

Just adds a regression test.

## Additional Context

None.

## 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.
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 frontend `noirc_frontend` crate
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

5 participants