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

Polymorphic Trait Inheritance Fails #6314

Closed
michaeljklein opened this issue Oct 22, 2024 · 0 comments · Fixed by #6381
Closed

Polymorphic Trait Inheritance Fails #6314

michaeljklein opened this issue Oct 22, 2024 · 0 comments · Fixed by #6381
Assignees
Labels
bug Something isn't working

Comments

@michaeljklein
Copy link
Contributor

Aim

Attempted to compile the following programs:

Single inheritance:

trait Foo {
    fn foo(self) -> Self;
}

trait Baz: Foo {}

impl<T> Baz for T where T: Foo {}

fn main() { }

Double inheritance:

trait Foo {
    fn foo(self) -> Self;
}

trait Bar {
    fn bar(self) -> Self;
}

trait Baz: Foo + Bar {}

impl<T> Baz for T where T: Foo + Bar {}

fn main() { }

Expected Behavior

Expected the programs to compile successfully

Bug

Compilation fails with:

Single inheritance:

error: The trait bound `T: Foo` is not satisfied
  ┌─ src/main.nr:5:12
  │
5 │ trait Baz: Foo {}
  │            --- required by this bound in `Baz
6 │ 
7 │ impl<T> Baz for T where T: Foo {}
  │                 - The trait `Foo` is not implemented for `T
  │

Aborting due to 1 previous error

Double inheritance:

error: The trait bound `T: Foo` is not satisfied
   ┌─ src/main.nr:9:12
   │
 9 │ trait Baz: Foo + Bar {}
   │            --- required by this bound in `Baz
10 │ 
11 │ impl<T> Baz for T where T: Foo + Bar {}
   │                 - The trait `Foo` is not implemented for `T
   │

error: The trait bound `T: Bar` is not satisfied
   ┌─ src/main.nr:9:18
   │
 9 │ trait Baz: Foo + Bar {}
   │                  --- required by this bound in `Baz
10 │ 
11 │ impl<T> Baz for T where T: Foo + Bar {}
   │                 - The trait `Bar` is not implemented for `T
   │

Aborting due to 2 previous errors

(Could be related to #6023, but this where clause is on an impl)

To Reproduce

Workaround

Yes

Workaround Description

Define each impl of the "trait alias" trait Baz monomorphically:

trait Foo {
    fn foo(self) -> Self;
}

trait Bar {
    fn bar(self) -> Self;
}

trait Baz: Foo + Bar {}

impl Foo for Field {
    fn foo(self) -> Self {
        self + 1
    }
}

impl Bar for Field {
    fn bar(self) -> Self {
        self + 2
    }
}

// impl<T> Baz for T where T: Foo + Bar {}
impl Baz for Field {}

fn main() {
    assert(0.foo().bar() == 3);
}

Additional Context

No response

Project Impact

Nice-to-have

Blocker Context

No response

Nargo Version

nargo version = 0.35.0 noirc version = 0.35.0+51ae1b324cd73fdb4fe3695b5d483a44b4aff4a9 (git version hash: 51ae1b3, is dirty: false)

NoirJS Version

No response

Proving Backend Tooling & 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 Oct 22, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Oct 22, 2024
@michaeljklein michaeljklein self-assigned this Oct 25, 2024
github-merge-queue bot pushed a commit that referenced this issue Oct 30, 2024
Co-authored-by: Tom French <[email protected]>
Co-authored-by: Tom French <[email protected]>
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Oct 30, 2024
TomAFrench added a commit that referenced this issue Oct 30, 2024
* master: (30 commits)
  chore: add regression tests for #4372 (#6401)
  chore: add regression tests for #6314 (#6381)
  chore: use array instead of Vec in keccak256 (#6395)
  fix: make keccak256 work with input lengths greater than 136 bytes (#6393)
  feat: support specifying generics on a struct when calling an associated function (#6306)
  fix: Display every bit in integer tokens (#6360)
  feat: better LSP hover for functions (#6376)
  feat: Add capacities to brillig vectors and use them in slice ops (#6332)
  feat: suggest removing `!` from macro call that doesn't return Quoted (#6384)
  fix: (formatter) correctly format quote delimiters (#6377)
  fix: allow globals in format strings (#6382)
  feat: do not increment reference counts on arrays through references (#6375)
  fix: (LSP) check visibility of module that re-exports item, if any (#6371)
  feat: let LSP suggest traits in trait bounds (#6370)
  fix: LSP auto-import would import public item inside private module (#6366)
  fix: remove assumed parent traits (#6365)
  fix: slightly better formatting of empty blocks with comments (#6367)
  fix: Fix panic in comptime code (#6361)
  feat: let the LSP import code action insert into existing use statements (#6358)
  chore: minor tweaks to comptime doc (#6357)
  ...
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