diff --git a/compiler/parser/src/parser/file.rs b/compiler/parser/src/parser/file.rs index 1390c114d1..74f863937d 100644 --- a/compiler/parser/src/parser/file.rs +++ b/compiler/parser/src/parser/file.rs @@ -349,7 +349,11 @@ impl ParserContext<'_, N> { let name = self.expect_identifier()?; // Parse parameters. - let (inputs, ..) = self.parse_paren_comma_list(|p| p.parse_input().map(Some))?; + let (inputs, _, span) = self.parse_paren_comma_list(|p| p.parse_input().map(Some))?; + + if inputs.is_empty() && matches!(variant, Variant::Function) { + return Err(ParserError::empty_function_arglist(span).into()); + } // Parse return type. let output = match self.eat(&Token::Arrow) { diff --git a/compiler/passes/src/code_generation/visit_program.rs b/compiler/passes/src/code_generation/visit_program.rs index b18da4899a..39c85ea1fe 100644 --- a/compiler/passes/src/code_generation/visit_program.rs +++ b/compiler/passes/src/code_generation/visit_program.rs @@ -247,6 +247,12 @@ impl<'a> CodeGenerator<'a> { // Construct and append the function body. let block_string = self.visit_block(&function.block); + if block_string.lines().all(|line| line.starts_with(" output ")) { + // There are no real instructions, which is invalid in Aleo, so + // add a dummy instruction. + function_string.push_str(" assert.eq true true;\n"); + } + function_string.push_str(&block_string); function_string diff --git a/errors/src/errors/parser/parser_errors.rs b/errors/src/errors/parser/parser_errors.rs index 48c6578932..28364ab270 100644 --- a/errors/src/errors/parser/parser_errors.rs +++ b/errors/src/errors/parser/parser_errors.rs @@ -365,4 +365,11 @@ create_messages!( msg: format!("Digit {digit} invalid in radix {radix} (token {token})."), help: None, } + + @formatted + empty_function_arglist { + args: (), + msg: format!("Cannot define a function with no parameters."), + help: None, + } ); diff --git a/tests/expectations/parser/functions/empty2.out b/tests/expectations/parser/functions/empty2.out index 701c2b45c6..71fe356203 100644 --- a/tests/expectations/parser/functions/empty2.out +++ b/tests/expectations/parser/functions/empty2.out @@ -1,22 +1,8 @@ namespace = "Parse" -expectation = "Pass" - -[[outputs]] - -[outputs.imports] - -[outputs.stubs] - -[outputs.program_scopes.test] -program_id = '{"name":"test","network":"\"{\\\"id\\\":\\\"1\\\",\\\"name\\\":\\\"aleo\\\",\\\"span\\\":\\\"{\\\\\\\"lo\\\\\\\":0,\\\\\\\"hi\\\\\\\":0}\\\"}\""}' -consts = [] -structs = [] -mappings = [] -functions = [[ - "x", - { annotations = [], variant = "Function", identifier = '{"id":"2","name":"x","span":"{\"lo\":39,\"hi\":40}"}', input = [], id = 5, output = [{ mode = "None", id = 3, type_ = { Integer = "U8" }, span = { lo = 46, hi = 48 } }], output_type = { Integer = "U8" }, block = { statements = [], id = 4, span = { lo = 49, hi = 51 } }, span = { lo = 30, hi = 51 } }, -]] - -[outputs.program_scopes.test.span] -lo = 2 -hi = 52 +expectation = "Fail" +outputs = [""" +Error [EPAR0370044]: Cannot define a function with no parameters. + --> test:4:15 + | + 4 | function x() -> u8 {} + | ^^"""] diff --git a/tests/tests/compiler/records/owner_wrong_ty.leo b/tests/tests/compiler/records/owner_wrong_ty.leo index 5a97bb1295..d6254b5998 100644 --- a/tests/tests/compiler/records/owner_wrong_ty.leo +++ b/tests/tests/compiler/records/owner_wrong_ty.leo @@ -9,7 +9,7 @@ program test.aleo { owner: bool, } - function main() -> bool { + function main(x: field) -> bool { return true; } } diff --git a/tests/tests/parser/functions/empty2.leo b/tests/tests/parser/functions/empty2.leo index 14ed61ff9b..0a84c2dec7 100644 --- a/tests/tests/parser/functions/empty2.leo +++ b/tests/tests/parser/functions/empty2.leo @@ -1,7 +1,8 @@ /* namespace = "Parse" -expectation = "Pass" +expectation = "Fail" */ program test.aleo { - function x() -> u8 {}} + function x() -> u8 {} +}