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

Requires async keyword before finalize statement. #2088

Merged
merged 6 commits into from
Sep 21, 2022
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
5 changes: 4 additions & 1 deletion compiler/parser/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ impl ParserContext<'_> {
pub(crate) fn parse_statement(&mut self) -> Result<Statement> {
match &self.token.token {
Token::Return => Ok(Statement::Return(self.parse_return_statement()?)),
Token::Finalize => Ok(Statement::Finalize(self.parse_finalize_statement()?)),
Token::Async => Ok(Statement::Finalize(self.parse_finalize_statement()?)),
// If a finalize token is found without a preceding async token, return an error.
Token::Finalize => Err(ParserError::finalize_without_async(self.token.span).into()),
Token::Increment => Ok(Statement::Increment(self.parse_increment_statement()?)),
Token::Decrement => Ok(Statement::Decrement(self.parse_decrement_statement()?)),
Token::If => Ok(Statement::Conditional(self.parse_conditional_statement()?)),
Expand Down Expand Up @@ -122,6 +124,7 @@ impl ParserContext<'_> {

/// Returns a [`FinalizeStatement`] AST node if the next tokens represent a finalize statement.
fn parse_finalize_statement(&mut self) -> Result<FinalizeStatement> {
self.expect(&Token::Async)?;
let start = self.expect(&Token::Finalize)?;
let (arguments, _, span) = self.parse_paren_comma_list(|p| p.parse_expression().map(Some))?;
self.expect(&Token::Semicolon)?;
Expand Down
1 change: 1 addition & 0 deletions compiler/parser/src/tokenizer/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ impl Token {
match &*identifier {
x if x.starts_with("aleo1") => Token::AddressLit(identifier),
"address" => Token::Address,
"async" => Token::Async,
"bool" => Token::Bool,
"circuit" => Token::Circuit,
"console" => Token::Console,
Expand Down
3 changes: 2 additions & 1 deletion compiler/parser/src/tokenizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ mod tests {
test_ident
12345
address
async
bool
const
else
Expand Down Expand Up @@ -157,7 +158,7 @@ mod tests {

assert_eq!(
output,
r#""test" "test{}test" "test{}" "{}test" "test{" "test}" "test{test" "test}test" "te{{}}" test_ident 12345 address bool const else false field finalize for function group i128 i64 i32 i16 i8 if in input let mut return scalar self string test true u128 u64 u32 u16 u8 console ! != && ( ) * ** + , - -> => _ . .. / : ; < <= = == > >= [ ] { { } } || ? @ // test
r#""test" "test{}test" "test{}" "{}test" "test{" "test}" "test{test" "test}test" "te{{}}" test_ident 12345 address async bool const else false field finalize for function group i128 i64 i32 i16 i8 if in input let mut return scalar self string test true u128 u64 u32 u16 u8 console ! != && ( ) * ** + , - -> => _ . .. / : ; < <= = == > >= [ ] { { } } || ? @ // test
/* test */ // "#
);
});
Expand Down
4 changes: 4 additions & 0 deletions compiler/parser/src/tokenizer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ pub enum Token {
Record,

// Regular Keywords
Async,
Circuit,
Console,
// Const variable and a const function.
Expand Down Expand Up @@ -143,6 +144,7 @@ pub enum Token {
/// because true and false are also boolean literals, which are different tokens from keywords
pub const KEYWORD_TOKENS: &[Token] = &[
Token::Address,
Token::Async,
Token::Bool,
Token::Circuit,
Token::Console,
Expand Down Expand Up @@ -192,6 +194,7 @@ impl Token {
pub fn keyword_to_symbol(&self) -> Option<Symbol> {
Some(match self {
Token::Address => sym::address,
Token::Async => sym::Async,
Token::Bool => sym::bool,
Token::Circuit => sym::circuit,
Token::Console => sym::console,
Expand Down Expand Up @@ -319,6 +322,7 @@ impl fmt::Display for Token {
U128 => write!(f, "u128"),
Record => write!(f, "record"),

Async => write!(f, "async"),
Circuit => write!(f, "circuit"),
Console => write!(f, "console"),
Const => write!(f, "const"),
Expand Down
1 change: 1 addition & 0 deletions compiler/span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ symbols! {
// general keywords
AlwaysConst,
assert,
Async: "async",
caller,
circuit,
Class: "class",
Expand Down
7 changes: 7 additions & 0 deletions errors/src/errors/parser/parser_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,11 @@ create_messages!(
msg: "Illegal spacing in the annotation declaration.",
help: Some("Remove whitespace between the `@` symbol and the identifier.".to_string()),
}

@formatted
finalize_without_async {
args: (),
msg: "A finalize statement must be preceded by the `async` keyword.",
help: Some("Add the `async` keyword before the `finalize` keyword.".to_string()),
}
);
4 changes: 2 additions & 2 deletions examples/broken_bank/src/main.leo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function deposit(token: Token, amount: u64) -> Token {

let hash: field = BHP256::hash(token.owner);

finalize(hash, amount);
async finalize(hash, amount);

return remaining;
}
Expand All @@ -50,7 +50,7 @@ function withdraw(recipient: address, amount: u64, rate: u64, periods: u64) -> T
amount: total,
};

finalize(hash, amount);
async finalize(hash, amount);

return token;
}
Expand Down
8 changes: 4 additions & 4 deletions examples/token/src/main.leo
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ record token {
@program
function mint_public(public receiver: address, public amount: u64) {
// Mint the tokens publicly by invoking the computation on-chain.
finalize(receiver, amount);
async finalize(receiver, amount);
}

finalize mint_public(public receiver: address, public amount: u64) {
Expand All @@ -41,7 +41,7 @@ function mint_private(receiver: address, amount: u64) -> token {
@program
function transfer_public(public receiver: address, public amount: u64) {
// Transfer the tokens publicly, by invoking the computation on-chain.
finalize(self.caller, receiver, amount);
async finalize(self.caller, receiver, amount);
}

finalize transfer_public(public sender: address, public receiver: address, public amount: u64) {
Expand Down Expand Up @@ -98,7 +98,7 @@ function transfer_private_to_public(sender: token, public receiver: address, pub
};

// Increment the token amount publicly for the token receiver.
finalize(receiver, amount);
async finalize(receiver, amount);

// Output the sender's change record.
return remaining;
Expand All @@ -123,7 +123,7 @@ function transfer_public_to_private(public receiver: address, public amount: u64
};

// Decrement the token amount of the caller publicly.
finalize(self.caller, amount);
async finalize(self.caller, amount);

// Output the receiver's record.
return transferred;
Expand Down
8 changes: 4 additions & 4 deletions examples/vote/src/main.leo
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function propose(public info: ProposalInfo) -> Proposal {
let id: field = BHP256::hash(info.title);

// Finalize the proposal id.
finalize(id);
async finalize(id);

// Return a new record for the proposal.
return Proposal {
Expand All @@ -64,7 +64,7 @@ function new_ticket(
public voter: address,
) -> Ticket {
// Finalize the proposal id for the ticket.
finalize(pid);
async finalize(pid);

return Ticket {
owner: voter,
Expand All @@ -80,7 +80,7 @@ finalize new_ticket(public pid: field) {
// Vote to agree with a proposal.
@program
function agree(ticket: Ticket) {// Privately cast the vote.
finalize(ticket.pid);
async finalize(ticket.pid);
}
finalize agree(public pid: field) {// Publicly increment the number of agree votes.
increment(agree_votes, pid, 1u64);
Expand All @@ -89,7 +89,7 @@ finalize agree(public pid: field) {// Publicly increment the number of agree vot
// Vote to disagree with a proposal.
@program
function disagree(ticket: Ticket) {// Privately cast the vote.
finalize(ticket.pid);
async finalize(ticket.pid);
}
finalize disagree(pid: field) {// Publicly increment the number of disagree votes.
increment(disagree_votes, pid, 1u64);
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/finalize/closure_with_finalize_fail.leo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ expectation: Fail
*/

function foo(a: u8, b: u8) -> u8 {
finalize(a, b);
async finalize(a, b);
return a + b;
}

Expand All @@ -18,7 +18,7 @@ finalize bar(a: u8, b: u8) -> u8 {


function mint_public(receiver: address, amount: u64) {
finalize(receiver, amount);
async finalize(receiver, amount);
}

finalize mint_public(receiver: address, amount: u64) {
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/finalize/decrement.leo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mapping amounts: address => u128;

@program
function decrease_self(amount: u128) {
finalize(self.caller, amount);
async finalize(self.caller, amount);
}

finalize decrease_self(addr: address, amount: u128) {
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/finalize/decrement_incorrect_type.leo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mapping tokens: address => Token;

@program
function decrease_self(amount: u128) {
finalize(self.caller, amount);
async finalize(self.caller, amount);
}

finalize decrease_self(addr: address, amount: u128) {
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/finalize/empty_finalize_fail.leo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ expectation: Fail

@program
function mint_public(public receiver: address, public amount: u64) {
finalize(receiver, amount);
async finalize(receiver, amount);
}

finalize mint_public (public receiver: address, public amount: u64) {}
6 changes: 3 additions & 3 deletions tests/compiler/finalize/finalize.leo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mapping values: u8 => u8;

@program
function mint_public(public receiver: address, public amount: u64) {
finalize(receiver, amount);
async finalize(receiver, amount);
}

finalize mint_public (public receiver: address, public amount: u64) {
Expand All @@ -17,14 +17,14 @@ finalize mint_public (public receiver: address, public amount: u64) {

@program
function public_adder(public a: u8, public b: u8) {
finalize(a, b);
async finalize(a, b);
} finalize public_adder(a: u8, b: u8) -> public u8 {
return a + b;
}

@program
function finalize_no_params() {
finalize();
async finalize();
}

finalize finalize_no_params() {
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/finalize/finalize_incorrect_modes_fail.leo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mapping account: address => u64;

@program
function mint_public(public receiver: address, public amount: u64) {
finalize(receiver, amount);
async finalize(receiver, amount);
}

finalize mint_public (public receiver: address, constant amount: u64) -> constant u64 {
Expand All @@ -16,7 +16,7 @@ finalize mint_public (public receiver: address, constant amount: u64) -> constan

@program
function mint_public2(public receiver: address, public amount: u64) {
finalize(receiver, amount);
async finalize(receiver, amount);
}

finalize mint_public2(public receiver: address, amount: u64) -> u64 {
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/finalize/finalize_incorrect_return_fail.leo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mapping account: address => u64;

@program
function mint_public(public receiver: address, public amount: u64) {
finalize(receiver, amount);
async finalize(receiver, amount);
}

finalize mint_public(public receiver: address, public amount: u64) -> u64 {
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/finalize/finalize_missing_return_fail.leo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mapping account: address => u64;

@program
function mint_public(public receiver: address, public amount: u64) {
finalize(receiver, amount);
async finalize(receiver, amount);
}

finalize mint_public (public receiver: address, public amount: u64) -> u64 {
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/finalize/finalize_name_mismatch_fail.leo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mapping values: u8 => u8;

@program
function mint_public(public receiver: address, public amount: u64) {
finalize(receiver, amount);
async finalize(receiver, amount);
}

finalize mint_private (public receiver: address, public amount: u64) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mapping account: address => u64;

@program
function mint_public(public receiver: address, public amount: u64) {
finalize(receiver, amount, amount);
async finalize(receiver, amount, amount);
}

finalize mint_public (public receiver: address, public amount: u64) {
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/finalize/increment.leo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mapping amounts: address => u128;

@program
function increase_self(amount: u128) {
finalize(self.caller, amount);
async finalize(self.caller, amount);
}

finalize increase_self(addr: address, amount: u128) {
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/finalize/increment_incorrect_type.leo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mapping tokens: address => Token;

@program
function increase_self(amount: u128) {
finalize(self.caller, amount);
async finalize(self.caller, amount);
}

finalize increase_self(addr: address, amount: u128) {
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/finalize/read_write_mapping_fail.leo
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ finalize read_in_finalize(public addr: address) -> public u128 {
}

function write_in_finalize(public addr: address, public amount: u128) {
finalize(addr, amount);
async finalize(addr, amount);
}

finalize write_in_finalize(public: addr: address, public amount: u128) {
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/function/self.leo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expectation: Pass

@program
function matches(addr: address) -> bool {
finalize(self.caller);
async finalize(self.caller);
return self.caller == addr;
} finalize matches(addr: address) -> bool {
return addr == self.caller;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
namespace: Compile
expectation: Fail
outputs:
- "Error [ETYC0372036]: Cannot use a `finalize` statement without a `finalize` block.\n --> compiler-test:4:5\n |\n 4 | finalize(a, b);\n | ^^^^^^^^^^^^^^\nError [ETYC0372044]: Function must contain a `finalize` statement on all execution paths.\n --> compiler-test:8:1\n |\n 8 | function bar(a: u8, b: u8) -> u8 {\n 9 | return a + b;\n 10 | }\n | ^\nError [ETYC0372032]: Only program functions can have a `finalize` block.\n --> compiler-test:12:1\n |\n 12 | finalize bar(a: u8, b: u8) -> u8 {\n 13 | return a + b;\n 14 | }\n | ^\n |\n = Remove the `finalize` block or add a `@program` annotation to the function.\nError [ETYC0372032]: Only program functions can have a `finalize` block.\n --> compiler-test:21:1\n |\n 21 | finalize mint_public(receiver: address, amount: u64) {\n 22 | increment(account, receiver, amount);\n 23 | }\n | ^\n |\n = Remove the `finalize` block or add a `@program` annotation to the function.\nError [ETYC0372005]: Unknown variable `account`\n --> compiler-test:22:15\n |\n 22 | increment(account, receiver, amount);\n | ^^^^^^^\nError [ETYC0372004]: Could not determine the type of `account`\n --> compiler-test:22:15\n |\n 22 | increment(account, receiver, amount);\n | ^^^^^^^\n"
- "Error [ETYC0372036]: Cannot use a `finalize` statement without a `finalize` block.\n --> compiler-test:4:11\n |\n 4 | async finalize(a, b);\n | ^^^^^^^^^^^^^^\nError [ETYC0372044]: Function must contain a `finalize` statement on all execution paths.\n --> compiler-test:8:1\n |\n 8 | function bar(a: u8, b: u8) -> u8 {\n 9 | return a + b;\n 10 | }\n | ^\nError [ETYC0372032]: Only program functions can have a `finalize` block.\n --> compiler-test:12:1\n |\n 12 | finalize bar(a: u8, b: u8) -> u8 {\n 13 | return a + b;\n 14 | }\n | ^\n |\n = Remove the `finalize` block or add a `@program` annotation to the function.\nError [ETYC0372032]: Only program functions can have a `finalize` block.\n --> compiler-test:21:1\n |\n 21 | finalize mint_public(receiver: address, amount: u64) {\n 22 | increment(account, receiver, amount);\n 23 | }\n | ^\n |\n = Remove the `finalize` block or add a `@program` annotation to the function.\nError [ETYC0372005]: Unknown variable `account`\n --> compiler-test:22:15\n |\n 22 | increment(account, receiver, amount);\n | ^^^^^^^\nError [ETYC0372004]: Could not determine the type of `account`\n --> compiler-test:22:15\n |\n 22 | increment(account, receiver, amount);\n | ^^^^^^^\n"
8 changes: 4 additions & 4 deletions tests/expectations/compiler/finalize/decrement.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ expectation: Pass
outputs:
- output:
- initial_input_ast: no input
initial_ast: 9498c048747706d4478783a2801179d84243099c3867cb437f4141bf873fc381
unrolled_ast: 9498c048747706d4478783a2801179d84243099c3867cb437f4141bf873fc381
ssa_ast: 9498c048747706d4478783a2801179d84243099c3867cb437f4141bf873fc381
flattened_ast: 30e7fb13033f0a61d999026dbeafff5fe3938e4b6ed2cb709fc0a38ce98c8c57
initial_ast: 7bbcb1e7bfda82030ab22d46c5c0ee4d2a54553aae4d581dc78b16663896a5f3
unrolled_ast: 7bbcb1e7bfda82030ab22d46c5c0ee4d2a54553aae4d581dc78b16663896a5f3
ssa_ast: 7bbcb1e7bfda82030ab22d46c5c0ee4d2a54553aae4d581dc78b16663896a5f3
flattened_ast: 71d3b3288fd82eab7ea755fe82ead9a5d3885cdb983488a88b5d677dd80fb1e8
8 changes: 4 additions & 4 deletions tests/expectations/compiler/finalize/finalize.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ expectation: Pass
outputs:
- output:
- initial_input_ast: no input
initial_ast: 4fcaf4a3cc781ca66c4ed30d4704bfa5bc31715a9cce6c12c3b45ba13deebb5c
unrolled_ast: 4fcaf4a3cc781ca66c4ed30d4704bfa5bc31715a9cce6c12c3b45ba13deebb5c
ssa_ast: d511fa6a72cca8b7370691502a92837faac71965312b63e55ce372271720612a
flattened_ast: a085ceac6974fb2a2cbf32de9775e0dbe3c1ca1e1ad184141a8794df6f7bef32
initial_ast: 96a5dcffcccc268103191159bbed06386425b092edbd7ffea20f97c80c078f2d
unrolled_ast: 96a5dcffcccc268103191159bbed06386425b092edbd7ffea20f97c80c078f2d
ssa_ast: 058ff955144948b6b26f3c529e18a4e20e74fcd328bd910f6d5f0771f35298d6
flattened_ast: f1b6acd125e8792ea90e5d8d43ce042795402542fa12e85e5644249cd8ae9f3b
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
namespace: Compile
expectation: Fail
outputs:
- "Error [ETYC0372042]: `finalize` expected `2` args, but got `3`\n --> compiler-test:8:5\n |\n 8 | finalize(receiver, amount, amount);\n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"
- "Error [ETYC0372042]: `finalize` expected `2` args, but got `3`\n --> compiler-test:8:11\n |\n 8 | async finalize(receiver, amount, amount);\n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"
8 changes: 4 additions & 4 deletions tests/expectations/compiler/finalize/increment.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ expectation: Pass
outputs:
- output:
- initial_input_ast: no input
initial_ast: 20577705e80224f486f237ba98fb33550fec7fb0a724eb9c339067a561d426e6
unrolled_ast: 20577705e80224f486f237ba98fb33550fec7fb0a724eb9c339067a561d426e6
ssa_ast: 20577705e80224f486f237ba98fb33550fec7fb0a724eb9c339067a561d426e6
flattened_ast: 6babfacc82c1283848fc1926ca778d6a3c7a46148cfe93fec13927ed31bd950a
initial_ast: 07adbfa271d270412b653f4c6d70028eae8bfa4b5228bc18d3059625a6b8b6ad
unrolled_ast: 07adbfa271d270412b653f4c6d70028eae8bfa4b5228bc18d3059625a6b8b6ad
ssa_ast: 07adbfa271d270412b653f4c6d70028eae8bfa4b5228bc18d3059625a6b8b6ad
flattened_ast: a8d61140c04832c275255f8c6cdeadbb99264b67e498a3bfb2ed0d452f21aaf2
Loading