Skip to content

Commit

Permalink
feat: Remove experimental feature warning for traits (#3783)
Browse files Browse the repository at this point in the history
# Description

## Problem\*


## Summary\*

After #3774 is merged we can merge
this PR to remove the experimental warning when using most trait
features. This PR also provides documentation for traits.

## Additional Context

Since there are still some unimplemented trait features I've added
experimental warnings for:
- Placing generics on the trait itself
- Associated types on traits
- Associated constants on traits

## 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>
  • Loading branch information
3 people committed Dec 13, 2023
1 parent cfa34d4 commit cb52242
Show file tree
Hide file tree
Showing 3 changed files with 377 additions and 14 deletions.
41 changes: 27 additions & 14 deletions compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,12 @@ fn trait_definition() -> impl NoirParser<TopLevelStatement> {
.then(trait_body())
.then_ignore(just(Token::RightBrace))
.validate(|(((name, generics), where_clause), items), span, emit| {
emit(ParserError::with_reason(ParserErrorReason::ExperimentalFeature("Traits"), span));
if !generics.is_empty() {
emit(ParserError::with_reason(
ParserErrorReason::ExperimentalFeature("Generic traits"),
span,
));
}
TopLevelStatement::Trait(NoirTrait { name, generics, where_clause, span, items })
})
}
Expand All @@ -437,7 +442,13 @@ fn trait_constant_declaration() -> impl NoirParser<TraitItem> {
.then(parse_type())
.then(optional_default_value())
.then_ignore(just(Token::Semicolon))
.map(|((name, typ), default_value)| TraitItem::Constant { name, typ, default_value })
.validate(|((name, typ), default_value), span, emit| {
emit(ParserError::with_reason(
ParserErrorReason::ExperimentalFeature("Associated constants"),
span,
));
TraitItem::Constant { name, typ, default_value }
})
}

/// trait_function_declaration: 'fn' ident generics '(' declaration_parameters ')' function_return_type
Expand Down Expand Up @@ -544,10 +555,15 @@ fn function_declaration_parameters() -> impl NoirParser<Vec<(Ident, UnresolvedTy

/// trait_type_declaration: 'type' ident generics
fn trait_type_declaration() -> impl NoirParser<TraitItem> {
keyword(Keyword::Type)
.ignore_then(ident())
.then_ignore(just(Token::Semicolon))
.map(|name| TraitItem::Type { name })
keyword(Keyword::Type).ignore_then(ident()).then_ignore(just(Token::Semicolon)).validate(
|name, span, emit| {
emit(ParserError::with_reason(
ParserErrorReason::ExperimentalFeature("Associated types"),
span,
));
TraitItem::Type { name }
},
)
}

/// Parses a non-trait implementation, adding a set of methods to a type.
Expand Down Expand Up @@ -581,11 +597,10 @@ fn trait_implementation() -> impl NoirParser<TopLevelStatement> {
.then_ignore(just(Token::LeftBrace))
.then(trait_implementation_body())
.then_ignore(just(Token::RightBrace))
.validate(|args, span, emit| {
.map(|args| {
let ((other_args, where_clause), items) = args;
let (((impl_generics, trait_name), trait_generics), object_type) = other_args;

emit(ParserError::with_reason(ParserErrorReason::ExperimentalFeature("Traits"), span));
TopLevelStatement::TraitImpl(NoirTraitImpl {
impl_generics,
trait_name,
Expand Down Expand Up @@ -616,12 +631,10 @@ fn where_clause() -> impl NoirParser<Vec<UnresolvedTraitConstraint>> {
trait_bounds: Vec<TraitBound>,
}

let constraints = parse_type().then_ignore(just(Token::Colon)).then(trait_bounds()).validate(
|(typ, trait_bounds), span, emit| {
emit(ParserError::with_reason(ParserErrorReason::ExperimentalFeature("Traits"), span));
MultiTraitConstraint { typ, trait_bounds }
},
);
let constraints = parse_type()
.then_ignore(just(Token::Colon))
.then(trait_bounds())
.map(|(typ, trait_bounds)| MultiTraitConstraint { typ, trait_bounds });

keyword(Keyword::Where)
.ignore_then(constraints.separated_by(just(Token::Comma)))
Expand Down
2 changes: 2 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"monomorphizes",
"nand",
"nargo",
"newtype",
"nixpkgs",
"noirc",
"noirup",
Expand All @@ -89,6 +90,7 @@
"pprof",
"preprocess",
"prettytable",
"println",
"printstd",
"pseudocode",
"quantile",
Expand Down

0 comments on commit cb52242

Please sign in to comment.