From b71b15d3305e20d013433f9499717a62fa0c3fbd Mon Sep 17 00:00:00 2001 From: kek kek kek Date: Mon, 9 Oct 2023 09:36:57 -0700 Subject: [PATCH] chore: parse fieldless structures (#3021) Co-authored-by: kevaundray --- compiler/noirc_frontend/src/parser/parser.rs | 13 ++++++++----- .../tests/execution_success/struct/src/main.nr | 4 ++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/compiler/noirc_frontend/src/parser/parser.rs b/compiler/noirc_frontend/src/parser/parser.rs index 7216dd9a924..702f056adcd 100644 --- a/compiler/noirc_frontend/src/parser/parser.rs +++ b/compiler/noirc_frontend/src/parser/parser.rs @@ -228,14 +228,15 @@ fn struct_definition() -> impl NoirParser { use self::Keyword::Struct; use Token::*; - let fields = struct_fields().delimited_by(just(LeftBrace), just(RightBrace)).recover_with( - nested_delimiters( + let fields = struct_fields() + .delimited_by(just(LeftBrace), just(RightBrace)) + .recover_with(nested_delimiters( LeftBrace, RightBrace, [(LeftParen, RightParen), (LeftBracket, RightBracket)], |_| vec![], - ), - ); + )) + .or(just(Semicolon).map(|_| Vec::new())); attributes() .or_not() @@ -2281,6 +2282,7 @@ mod test { #[test] fn parse_structs() { let cases = vec![ + "struct Foo;", "struct Foo { }", "struct Bar { ident: Field, }", "struct Baz { ident: Field, other: Field }", @@ -2315,12 +2317,13 @@ mod test { #[test] fn parse_constructor() { let cases = vec![ + "Baz", "Bar { ident: 32 }", "Baz { other: 2 + 42, ident: foo() + 1 }", "Baz { other, ident: foo() + 1, foo }", ]; - parse_all(expression(), cases); + parse_all(expression(), cases); parse_with(expression(), "Foo { a + b }").unwrap_err(); } diff --git a/tooling/nargo_cli/tests/execution_success/struct/src/main.nr b/tooling/nargo_cli/tests/execution_success/struct/src/main.nr index 5e3530e8364..2bd5a1aec28 100644 --- a/tooling/nargo_cli/tests/execution_success/struct/src/main.nr +++ b/tooling/nargo_cli/tests/execution_success/struct/src/main.nr @@ -52,7 +52,11 @@ fn get_dog() -> Animal { dog } +struct Unit; + fn main(x: Field, y: Field) { + let unit = Unit {}; + let first = Foo::default(x,y); let p = Pair { first, second: 1 };