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

fix(traits)!: trait functions with a default implementation must not be followed by a semicolon #2987

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,10 @@
}

fn self_parameter() -> impl NoirParser<(Pattern, UnresolvedType, Visibility)> {
let refmut_pattern = just(Token::Ampersand).then_ignore(keyword(Keyword::Mut));

Check warning on line 346 in compiler/noirc_frontend/src/parser/parser.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (refmut)
let mut_pattern = keyword(Keyword::Mut);

refmut_pattern

Check warning on line 349 in compiler/noirc_frontend/src/parser/parser.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (refmut)
.or(mut_pattern)
.map_with_span(|token, span| (token, span))
.or_not()
Expand Down Expand Up @@ -394,8 +394,7 @@
trait_function_declaration()
.or(trait_type_declaration())
.or(trait_constant_declaration())
.separated_by(just(Token::Semicolon))
.allow_trailing()
.repeated()
}

fn optional_default_value() -> impl NoirParser<Option<Expression>> {
Expand All @@ -408,18 +407,22 @@
.then_ignore(just(Token::Colon))
.then(parse_type())
.then(optional_default_value())
.then_ignore(just(Token::Semicolon))
.map(|((name, typ), default_value)| TraitItem::Constant { name, typ, default_value })
}

/// trait_function_declaration: 'fn' ident generics '(' declaration_parameters ')' function_return_type
fn trait_function_declaration() -> impl NoirParser<TraitItem> {
let trait_function_body_or_semicolon =
block(expression()).map(Option::from).or(just(Token::Semicolon).map(|_| Option::None));
jfecher marked this conversation as resolved.
Show resolved Hide resolved

keyword(Keyword::Fn)
.ignore_then(ident())
.then(generics())
.then(parenthesized(function_declaration_parameters()))
.then(function_return_type().map(|(_, typ)| typ))
.then(where_clause())
.then(block(expression()).or_not())
.then(trait_function_body_or_semicolon)
.validate(
|(((((name, generics), parameters), return_type), where_clause), body), span, emit| {
validate_where_clause(&generics, &where_clause, span, emit);
Expand Down Expand Up @@ -526,7 +529,10 @@

/// trait_type_declaration: 'type' ident generics
fn trait_type_declaration() -> impl NoirParser<TraitItem> {
keyword(Keyword::Type).ignore_then(ident()).map(|name| TraitItem::Type { name })
keyword(Keyword::Type)
.ignore_then(ident())
.then_ignore(just(Token::Semicolon))
.map(|name| TraitItem::Type { name })
}

/// Parses a non-trait implementation, adding a set of methods to a type.
Expand Down Expand Up @@ -916,7 +922,7 @@
let shorthand_operators = right_shift_operator().or(one_of(shorthand_operators));
let shorthand_syntax = shorthand_operators.then_ignore(just(Token::Assign));

// Since >> is lexed as two separate greater-thans, >>= is lexed as > >=, so

Check warning on line 925 in compiler/noirc_frontend/src/parser/parser.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (thans)
// we need to account for that case here as well.
let right_shift_fix =
just(Token::Greater).then(just(Token::GreaterEqual)).map(|_| Token::ShiftRight);
Expand Down Expand Up @@ -944,7 +950,7 @@

let dereferences = just(Token::Star).repeated();

let lvalues =

Check warning on line 953 in compiler/noirc_frontend/src/parser/parser.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (lvalues)
l_ident.then(l_member_rhs.or(l_index).repeated()).foldl(|lvalue, rhs| match rhs {
LValueRhs::MemberAccess(field_name) => {
LValue::MemberAccess { object: Box::new(lvalue), field_name }
Expand All @@ -952,7 +958,7 @@
LValueRhs::Index(index) => LValue::Index { array: Box::new(lvalue), index },
});

dereferences.then(lvalues).foldr(|_, lvalue| LValue::Dereference(Box::new(lvalue)))

Check warning on line 961 in compiler/noirc_frontend/src/parser/parser.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (lvalues)

Check warning on line 961 in compiler/noirc_frontend/src/parser/parser.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (foldr)
}

fn parse_type<'a>() -> impl NoirParser<UnresolvedType> + 'a {
Expand Down Expand Up @@ -2042,7 +2048,7 @@
// for a particular operation. Also known as `tag` or `marker` traits:
// https://stackoverflow.com/questions/71895489/what-is-the-purpose-of-defining-empty-impl-in-rust
"trait Empty {}",
"trait TraitWithDefaultBody { fn foo(self) {}; }",
"trait TraitWithDefaultBody { fn foo(self) {} }",
"trait TraitAcceptingMutableRef { fn foo(&mut self); }",
"trait TraitWithTypeBoundOperation { fn identity() -> Self; }",
"trait TraitWithAssociatedType { type Element; fn item(self, index: Field) -> Self::Element; }",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
trait Trait1a {
fn trait_method1(self) -> Field {
self.trait_method2() * 7892 - self.vl
};
}
fn trait_method2(self) -> Field {
43278
}
Expand All @@ -42,7 +42,7 @@ impl Trait1a for Struct1a { }
trait Trait1b {
fn trait_method1(self) -> Field {
self.trait_method2() * 2832 - self.vl
};
}
fn trait_method2(self) -> Field {
9323
}
Expand All @@ -58,7 +58,7 @@ impl Trait1b for Struct1b {
trait Trait1c {
fn trait_method1(self) -> Field {
self.trait_method2() * 7635 - self.vl
};
}
fn trait_method2(self) -> Field;
}
struct Struct1c { vl: Field }
Expand All @@ -72,7 +72,7 @@ impl Trait1c for Struct1c {
trait Trait1d {
fn trait_method1(self) -> Field {
self.trait_method2() * 2825 - self.vl
};
}
fn trait_method2(self) -> Field {
29341
}
Expand All @@ -88,7 +88,7 @@ impl Trait1d for Struct1d {
trait Trait1e {
fn trait_method1(self) -> Field {
self.trait_method2() * 85465 - self.vl
};
}
fn trait_method2(self) -> Field {
2381
}
Expand All @@ -107,7 +107,7 @@ impl Trait1e for Struct1e {
trait Trait1f {
fn trait_method1(self) -> Field {
self.trait_method2() * 43257 - self.vl
};
}
fn trait_method2(self) -> Field;
}
struct Struct1f { vl: Field }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ impl Default for Foo {

trait F {
fn f1(self) -> Field;
fn f2(self) -> Field { 2 };
fn f3(self) -> Field { 3 };
fn f4(self) -> Field { 4 };
fn f5(self) -> Field { 5 };
fn f2(self) -> Field { 2 }
fn f3(self) -> Field { 3 }
fn f4(self) -> Field { 4 }
fn f5(self) -> Field { 5 }
}

struct Bar {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ trait ATrait {

fn static_method() -> Field {
Self::static_method_2()
};
}

fn static_method_2() -> Field {
100
};
}
}

struct Foo {
Expand Down
Loading