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

Trait impl with generics count mismatch doesn't produce an error #5583

Closed
asterite opened this issue Jul 23, 2024 · 0 comments · Fixed by #5623
Closed

Trait impl with generics count mismatch doesn't produce an error #5583

asterite opened this issue Jul 23, 2024 · 0 comments · Fixed by #5623
Assignees
Labels
bug Something isn't working

Comments

@asterite
Copy link
Collaborator

asterite commented Jul 23, 2024

Aim

Compile this program:

struct Foo {}

impl Foo<T> {}

fn main() { }

Expected Behavior

It should error because Foo isn't generic. A similar program in Rust gives two erros:

  1. cannot find T in this scope
  2. struct takes 0 generic arguments but 1 generic argument was supplied

This program also doesn't produce any errors:

struct Foo {}

impl <T> Foo<T> {}

fn main() { }

In Rust:

  1. struct takes 0 generic arguments but 1 generic argument was supplied

Bug

The above programs should produce an error.

To Reproduce

No response

Project Impact

None

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

Related to #5542

Installation Method

None

Nargo Version

No response

NoirJS Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@asterite asterite added the bug Something isn't working label Jul 23, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Jul 23, 2024
@asterite asterite self-assigned this Jul 29, 2024
github-merge-queue bot pushed a commit that referenced this issue Jul 29, 2024
# Description

## Problem

Resolves #5583

## Summary

There's a call, `verify_generics_count`, that's supposed to do this
check. The problem is that the `args` given to it are the results of
zipping the struct's generics with the given generics. That will always
produce an `args` the size of the smallest of the two. So, if a struct
doesn't have generics, `args` will end up empty and no error is
produced. However, if a struct has more generics than given, an error
was correctly produced. The solution is to get the actual and expected
numbers before shadowing `args`.

## Additional Context

In Rust this program gives two errors:

```rust
struct Foo {}

impl Foo<T> {}

fn main() { }
```

1. cannot find T in this scope
2. struct takes 0 generic arguments but 1 generic argument was supplied

With this PR, in Noir we'll be just giving one error (the second one).
The reason is that only generics that match the struct generics count
are checked. I thought about changing the code to produce the same
number of errors as Rust... but I didn't know if it was worth it. Here
you'll get the "incorrect generics count" error, so you'll have to fix
that by either removing the generic (solved) or by adding a generic to
`struct Foo` (likely not what you are going to do), at which point
you'll get the other error... so I thought that with just one error it's
good enough.

## 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.
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Jul 29, 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.

1 participant