Skip to content

Commit

Permalink
Require comma separators (and nothing else) between struct members.
Browse files Browse the repository at this point in the history
Fixes #28377, #28379
  • Loading branch information
mikebenfield committed Oct 8, 2024
1 parent 0d2eb02 commit 04407e1
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 28 deletions.
16 changes: 2 additions & 14 deletions compiler/parser/src/parser/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,11 @@ impl<N: Network> ParserContext<'_, N> {
fn parse_struct_members(&mut self) -> Result<(Vec<Member>, Span)> {
let mut members = Vec::new();

let (mut semi_colons, mut commas) = (false, false);

while !self.check(&Token::RightCurly) {
let variable = self.parse_member_variable_declaration()?;

if self.eat(&Token::Semicolon) {
if commas {
self.emit_err(ParserError::mixed_commas_and_semicolons(self.token.span));
}
semi_colons = true;
}

if self.eat(&Token::Comma) {
if semi_colons {
self.emit_err(ParserError::mixed_commas_and_semicolons(self.token.span));
}
commas = true;
if !self.check(&Token::RightCurly) && !self.eat(&Token::Comma) {
self.emit_err(ParserError::comma_expected_after_member(self.token.span));
}

members.push(variable);
Expand Down
9 changes: 9 additions & 0 deletions errors/src/errors/parser/parser_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ create_messages!(
}

/// For when the parser encountered a mix of commas and semi-colons in struct member variables.
// TODO unused
@formatted
mixed_commas_and_semicolons {
args: (),
Expand Down Expand Up @@ -341,4 +342,12 @@ create_messages!(
msg: format!("Cannot create an external record. Records can only be created in the program that they are defined in."),
help: None,
}

/// For when the parser encountered a member declaration not followed by a comma.
@formatted
comma_expected_after_member {
args: (),
msg: "Each member declaration in a struct or record must be followed by a comma (except the last).",
help: None,
}
);
5 changes: 5 additions & 0 deletions tests/expectations/parser/structs/comma_separators.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
namespace: Parse
expectation: Fail
outputs:
- "Error [EPAR0370041]: Each member declaration in a struct or record must be followed by a comma (except the last).\n --> test:5:9\n |\n 5 | y: u8\n | ^"
5 changes: 5 additions & 0 deletions tests/expectations/parser/structs/only_comma_separators.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
namespace: Parse
expectation: Fail
outputs:
- "Error [EPAR0370041]: Each member declaration in a struct or record must be followed by a comma (except the last).\n --> test:4:14\n |\n 4 | x: u8;\n | ^\nError [EPAR0370009]: unexpected string: expected 'identifier', found ';'\n --> test:4:14\n |\n 4 | x: u8;\n | ^"
2 changes: 1 addition & 1 deletion tests/tests/compiler/bugs/unknown_variable_fail.leo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ program test.aleo {
decimals: u8,
circulating_supply: u64,
total_supply: u64,
testers: u64
testers: u64,
admin: address,
}

Expand Down
2 changes: 1 addition & 1 deletion tests/tests/compiler/console/assert.leo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expectation: Pass

program test.aleo {
struct Foo {
a: u8;
a: u8,
}

record Token {
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/compiler/expression/cast_fail.leo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ program test.aleo {
mapping balances: field => field;

struct Foo {
data: field;
data: field,
}

async transition main(a: field) -> Future {
Expand Down
8 changes: 4 additions & 4 deletions tests/tests/compiler/finalize/mapping.leo
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ program test.aleo {
mapping balances: address => u128;

struct Token {
Owner: address;
balance: u128;
Owner: address,
balance: u128,
}

mapping tokens: address => Token;

struct Bar {
a: u128;
a: u128,
}

struct Baz {
a: u128;
a: u128,
}

mapping foo: Bar => Baz;
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/compiler/finalize/shadow_mapping_fail.leo
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ program test.aleo {
}

struct bar {
a: u64;
a: u64,
}
}
4 changes: 2 additions & 2 deletions tests/tests/compiler/mappings/read_external_mapping.leo
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ program relay.aleo {
mapping users: address => bool;

record message {
owner: address;
data: u8;
owner: address,
data: u8,
}

async transition send(addr: address, val: u8) -> (message, Future) {
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/compiler/structs/inline_fail.leo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expectation: Fail

program test.aleo {
struct Foo {
x: u32;
x: u32,
}

function main() {
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/compiler/structs/inline_member_fail.leo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expectation: Fail

program test.aleo {
struct Foo {
x: u8;
x: u8,
}

function main() {
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/compiler/structs/member_variable_fail.leo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expectation: Fail

program test.aleo {
struct Foo {
x: u32;
x: u32,
}

function main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expectation: Fail

program test.aleo {
struct Foo {
x: u8;
x: u8,
}

function Foo() {}
Expand Down
14 changes: 14 additions & 0 deletions tests/tests/parser/structs/comma_separators.leo
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
namespace: Parse
expectation: Fail
*/
program comma_separators.aleo {
struct none {
x: u8
y: u8
}

transition main(public a: u32) -> u32 {
return a;
}
}
19 changes: 19 additions & 0 deletions tests/tests/parser/structs/only_comma_separators.leo
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
namespace: Parse
expectation: Fail
*/
program only_comma_separators.aleo {
struct semicolons {
x: u8;
y: u8
}

struct semicolons_with_trailing {
x: u8;
y: u8;
}

transition main(public a: u32) -> u32 {
return a;
}
}

0 comments on commit 04407e1

Please sign in to comment.