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

Epic: Implement Traits #2568

Open
9 of 11 tasks
Tracked by #2629 ...
nickysn opened this issue Sep 5, 2023 · 4 comments
Open
9 of 11 tasks
Tracked by #2629 ...

Epic: Implement Traits #2568

nickysn opened this issue Sep 5, 2023 · 4 comments
Milestone

Comments

@nickysn
Copy link
Contributor

nickysn commented Sep 5, 2023

This is a tracking issue for the implementation of traits.

Noir Traits Roadmap

Note: Traits are a huge feature, and this list is by no means final. Expect more steps to be added along the way.

Parser

Parser (optional)

Def collector

    trait Trait1 {
        fn tralala();
    }
    trait Trait2 {
        fn tralala();
    }
    struct MyStruct {}
    impl Trait1 for MyStruct {
        fn tralala() {
        }
    }
    impl Trait2 for MyStruct {
        fn tralala() {
        }
    }

Name resolution/Type checking

    trait MyTrait {
        type MyTraitType;
        let MyConst: MyTraitType;
        fn MyFunc(para: MyTraitType);
    }
  • using the type outside the trait
    trait MyTrait {
        type MyTraitType;
    }
     
    fn main()
    {
        let a: MyTrait::MyTraitType = ...
    }

Monomorphization

Support generic traits

  1. enhancement
    jfecher

Implement builtin traits

  • Operator overloading: Rust std::ops equivalents (Add, Sub, ...)
  • Operator overloading: Rust std::cmp equivalentes (Eq, PartialEq)
  • Function traits (Fn)

Support combining of traits

  1. enhancement

Testing

@Savio-Sou Savio-Sou changed the title Noir Traits Implementation Progress Epic: Implement Traits Sep 6, 2023
This was referenced Oct 17, 2023
@kevaundray
Copy link
Collaborator

Adding #2629 for when this Epic is nearing completion

@Savio-Sou Savio-Sou added this to the 1.0 milestone Oct 16, 2023
github-merge-queue bot pushed a commit that referenced this issue Dec 13, 2023
# Description

## Problem\*

Resolves #3773
Part of #2568 

## Summary\*

This PR implements the ability to be able to call trait methods such as
`Default::default()` where previously we required
`StructName::default()`. The specific impl is selected via type
inference. Previously, this resulted in a compiler panic.

## Additional Context

When a trait method's type isn't constrained enough or is otherwise
still ambiguous after type inference, the compiler currently just
selects the first trait implementation that matches. E.g. `let _ =
Default::default();`. This is a separate issue, so I'll create a new
issue for this.

## Documentation\*

Check one:
- [ ] No documentation needed. 
- [ ] Documentation included in this PR.
- [x] **[Exceptional Case]** Documentation to be submitted in a separate
PR.
- I think traits are finally stable enough to start writing
documentation for. I'm going to submit another PR to remove some of the
experimental warnings the compiler has for traits, and with it add
documentation for traits as well. This wouldn't stabilize all trait
features (e.g. associated types are still unimplemented), but users will
now be able to use basic traits with functions without warnings. Any
unimplemented trait items (associated types again) will not be included
in the documentation.

# 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: kevaundray <kevtheappdev@gmail.com>
github-merge-queue bot pushed a commit that referenced this issue Jan 3, 2024
# Description

## Problem\*

Working towards #2568

## Summary\*

This PR implements operator overloading through traits. This works for
all operators in noir, including bitwise operators. The comparison
operators `<`, `<=`, `>`, and `>=`, are notable in that their
corresponding function `cmp` returns an `Ordering` object rather than a
boolean as the operators themselves do.

## Additional Context

There is a bug when using static trait function syntax
`Default::default` or operator overloading when the trait has generic
methods. I'm considering this a separate bug, although for the moment it
prevents us from being able to use e.g. the `Eq` trait for arrays. There
is a boolean in `type_check/expr.rs` that determines if we use a trait
impl or primitive implementation of each operator and because of this
bug I've kept the primitive implementations for string and array
equality.

## Documentation\*

Check one:
- [ ] No documentation needed.
- [x] Documentation included in this PR.
- [ ] **[Exceptional Case]** 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: Tom French <tom@tomfren.ch>
Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
@Savio-Sou
Copy link
Collaborator

Added a "Bugs" section to the original Issue with latest ones @jfecher gathered.

@Savio-Sou
Copy link
Collaborator

Added #3471 and #3949. Useful features for Aztec.nr development.

@jfecher
Copy link
Contributor

jfecher commented Mar 12, 2024

I've made issues for all the remaining items. They are:

Of these, only #4538 is a major item. The rest are mostly small syntactic shorthands which would be nice to have. #4539 is to some degree necessary, but only once we have #4538.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 📋 Backlog
Development

No branches or pull requests

4 participants