-
Notifications
You must be signed in to change notification settings - Fork 219
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
feat: Implement traits - first prototype #2094
feat: Implement traits - first prototype #2094
Conversation
Full list of changes: * The functions within trait definitions can now have a body * Parsing support for trait-associated constants (using the comptime keyword for now to avoid introducing a new keyword such as const). * Traits can have a where clause * The where clause is only allowed on generic definitions * The parsing of where clauses with multiple constraints has been fixed * More parsing tests for functions using traits and trait definitions.
1070de6
to
7d34e14
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you break this up into smaller PRs next time? This one is too large which makes it take longer to review (apologies for the delay) and contain more TODOs and stubs which prevent it from being merged.
We don't need to add name resolution, type checking, and monomorphization of traits in one PR. We can focus on e.g. one pass at a time and just add the minimum amount of features so that we have a clean base to build upon that can be expanded later.
crates/nargo_cli/tests/test_data_ssa_refactor/struct/Nargo.toml
Outdated
Show resolved
Hide resolved
if generics.len() > 0 || where_clause.len() > 0 { | ||
emit(ParserError::with_reason(ParserErrorReason::TraitsAreExperimental, span)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see why we wouldn't issue this error if the trait has no generics. Lets keep the traits are experimental error. We should probably change it to a warning now that we're iterating on traits so that we continue through proving.
.then_ignore(just(Token::Colon)) | ||
.then(parse_type()) | ||
.then(optional_default_value()) | ||
.validate(|((name, typ), default_value), _span, _emit| TraitItem::Constant { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.validate(|((name, typ), default_value), _span, _emit| TraitItem::Constant { | |
.map(|((name, typ), default_value)| TraitItem::Constant { |
@@ -124,6 +129,39 @@ pub struct StructType { | |||
pub span: Span, | |||
} | |||
|
|||
#[derive(Debug, PartialEq, Eq)] | |||
pub enum TraitItemType { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub enum TraitItemType { | |
pub enum TraitItem { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunetly, this conflicts with crates/noirc_frontend/src/ast/traits.rs
: pub enum TraitItem
I also like to submit small PRs. I was not sure what is the correct approach for implementing traits, so I tried to make it work end-to-end in order to validate the correctness. The result is this BIG PR. In the near future, I will aim at smaller changes/PRs |
Since larger PRs take quite a long time to review, I'd suggest that we break this PR down into smaller PRs and open an issue detailing the smaller chunks that are needed for implementing traits. If this PR is mostly done, then it would be just copying the relevant parts into separate PRs |
* Add several parsing traits test * Expand trait definition in AST to include body * Add diagnostic error when where clause is not appled on generic type
* feat: Implement traits - parser support #2094 * Add several parsing traits test * Expand trait definition in AST to include body * Add diagnostic error when where clause is not appled on generic type * Update crates/noirc_frontend/src/parser/parser.rs Co-authored-by: jfecher <jfecher11@gmail.com> * make parse_all to ignore warnings and use it everywhere --------- Co-authored-by: Yordan Madzhunkov <ymadzhunkov@gmail.com> Co-authored-by: jfecher <jfecher11@gmail.com>
* master: (80 commits) fix: properly capture lvalues in closure environments (#2120) (#2257) fix: Optimize contracts built by `nargo info` (#2259) chore: impl Display for DebugType (#2258) chore: update `noir_wasm` build process to match `acvm_js` (#2067) feat: Implement traits - parser support #2094 (#2230) chore: Refactor DefCollector duplicate errors (#2231) chore: Address clippy warnings (#2246) feat: Support `contract` package type in `nargo info` command (#2249) feat: Add slice append (#2241) chore: Bump `async-lsp` to v0.0.5 (#2186) chore: Move the remaining `nargo_cli` lib funcs into `nargo` crate (#2225) chore: Add test for eddsa (#2237) chore: Split `Nargo.toml` operations into separate package (#2224) fix(stdlib): correct `tecurve::contains` formula (#1821) feat: Remove `comptime` and warn upon usage (#2178) fix: Remove last vestige of array of structs to struct of arrays conversion (#2217) fix: Add foreign impl error (#2216) feat(nargo)!: Replace `--contracts` flag with `contract` package type (#2204) feat: Optimize `x < 0` for unsigned `x` to false (#2206) fix: Initialize numeric generics' type to a polymorphic integer when used in an expression (#2179) ...
* master: (29 commits) feat(nargo): Add support for contracts in `nargo check` (#2267) chore(ci): Name wasm job more clearly (#2269) chore(ci): Create cache key with consideration to target (#2273) chore(ci): Run publish workflow against PRs (#2268) chore: Merge in contents of `build-nargo` repository (#2211) fix(lsp): Improve dependency resolution in context of `Nargo.toml` (#2226) chore: Remove unnecessary duplication in how we test Noir compiler (#2248) fix: properly capture lvalues in closure environments (#2120) (#2257) fix: Optimize contracts built by `nargo info` (#2259) chore: impl Display for DebugType (#2258) chore: update `noir_wasm` build process to match `acvm_js` (#2067) feat: Implement traits - parser support #2094 (#2230) chore: Refactor DefCollector duplicate errors (#2231) chore: Address clippy warnings (#2246) feat: Support `contract` package type in `nargo info` command (#2249) feat: Add slice append (#2241) chore: Bump `async-lsp` to v0.0.5 (#2186) chore: Move the remaining `nargo_cli` lib funcs into `nargo` crate (#2225) chore: Add test for eddsa (#2237) chore: Split `Nargo.toml` operations into separate package (#2224) ...
* master: feat(nargo): Add support for contracts in `nargo check` (#2267) chore(ci): Name wasm job more clearly (#2269) chore(ci): Create cache key with consideration to target (#2273) chore(ci): Run publish workflow against PRs (#2268) chore: Merge in contents of `build-nargo` repository (#2211) fix(lsp): Improve dependency resolution in context of `Nargo.toml` (#2226) chore: Remove unnecessary duplication in how we test Noir compiler (#2248) fix: properly capture lvalues in closure environments (#2120) (#2257) fix: Optimize contracts built by `nargo info` (#2259) chore: impl Display for DebugType (#2258) chore: update `noir_wasm` build process to match `acvm_js` (#2067) feat: Implement traits - parser support #2094 (#2230) chore: Refactor DefCollector duplicate errors (#2231) chore: Address clippy warnings (#2246) feat: Support `contract` package type in `nargo info` command (#2249)
Description
Implement traits #527
Add initial prototype implementation of Traits in Noir.
This is very experimental work and needs more feedback, progress and testing before it can be enabled to the users
Problem*
Add trait functionality to Noir language analogous to how traits are implemented in rust
Resolves
Summary*
The goal is to make this simple program compile & execute
The noir compiler emits the following messages. I accept all kind of suggestion how to improve them:
Example 1:
Example 2:
Example 3:
Example 4
Example 5:
Example 6:
Example 7:
Example 8:
Documentation
This PR requires documentation updates when merged.
Additional Context
PR Checklist*
cargo fmt
on default settings.