Skip to content

Commit

Permalink
Error for future parameters except on async functions.
Browse files Browse the repository at this point in the history
Also, fix the span for function inputs.
  • Loading branch information
mikebenfield committed Dec 16, 2024
1 parent 2f49d3b commit a81b97d
Show file tree
Hide file tree
Showing 30 changed files with 110 additions and 56 deletions.
5 changes: 3 additions & 2 deletions compiler/parser/src/parser/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,10 @@ impl<N: Network> ParserContext<'_, N> {
let name = self.expect_identifier()?;
self.expect(&Token::Colon)?;

let type_ = self.parse_type()?.0;
let (type_, type_span) = self.parse_type()?;
let span = name.span() + type_span;

Ok(functions::Input { identifier: name, mode, type_, span: name.span, id: self.node_builder.next_id() })
Ok(functions::Input { identifier: name, mode, type_, span, id: self.node_builder.next_id() })
}

/// Returns an [`Output`] AST node if the next tokens represent a function output.
Expand Down
9 changes: 7 additions & 2 deletions compiler/passes/src/type_checking/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,8 +1220,13 @@ impl<'a, N: Network> TypeChecker<'a, N> {
}
}

// Add function inputs to the symbol table. Futures have already been added.
if !matches!(&input_var.type_(), &Type::Future(_)) {
if matches!(&input_var.type_(), Type::Future(_)) {
// Future parameters may only appear in async functions.
if !matches!(self.scope_state.variant, Some(Variant::AsyncFunction)) {
self.emit_err(TypeCheckerError::no_future_parameters(input_var.span()));
}
} else {
// Add function inputs to the symbol table. Futures have already been added.
if let Err(err) = self.symbol_table.borrow_mut().insert_variable(
Location::new(None, input_var.identifier().name),
self.scope_state.program_name,
Expand Down
7 changes: 7 additions & 0 deletions errors/src/errors/type_checker/type_checker_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,4 +912,11 @@ create_messages!(
msg: format!("An array cannot have a future as an element type."),
help: None,
}

@formatted
no_future_parameters {
args: (),
msg: format!("Futures may only appear as parameters to async functions."),
help: None,
}
);
2 changes: 1 addition & 1 deletion tests/expectations/compiler/array/array_of_records.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ Error [ETYC0372079]: An array cannot have a record as an element type
--> compiler-test:9:20
|
9 | transition foo(a: [bar; 8]) -> u8 {
| ^
| ^^^^^^^^^^^
"""]
2 changes: 1 addition & 1 deletion tests/expectations/compiler/array/array_too_large_fail.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ Error [ETYC0372077]: An array cannot have more than 32 elements, found one with
--> compiler-test:4:20
|
4 | transition foo(a: [bool; 33]) -> bool {
| ^
| ^^^^^^^^^^^^^
"""]
2 changes: 1 addition & 1 deletion tests/expectations/compiler/array/array_too_small_fail.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ Error [ETYC0372076]: An array cannot be empty
--> compiler-test:4:20
|
4 | transition foo(a: [bool; 0]) -> bool {
| ^
| ^^^^^^^^^^^^
"""]
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Error [ETYC0372032]: An input to an async function must be public.
--> compiler-test:10:76
|
10 | async function finalize_mint_public(public receiver: address, constant amount: u64) -> constant u64 {
| ^^^^^^
| ^^^^^^^^^^^
|
= Use a `public` modifier to the input variable declaration or remove the visibility modifier entirely.
Error [ETYC0372038]: A returned value cannot be a constant.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ Error [ETYC0372057]: Only `transition` functions can have a record as input or o
--> compiler-test:9:18
|
9 | function foo(board: Board, data: u8) -> Board {
| ^^^^^
| ^^^^^^^^^^^^
Error [ETYC0372057]: Only `transition` functions can have a record as input or output.
--> compiler-test:9:18
|
9 | function foo(board: Board, data: u8) -> Board {
| ^^^^^
| ^^^^^^^^^^^^
Error [ETYC0372057]: Only `transition` functions can have a record as input or output.
--> compiler-test:9:45
|
Expand All @@ -25,10 +25,10 @@ Error [ETYC0372057]: Only `transition` functions can have a record as input or o
--> compiler-test:16:18
|
16 | function bar(board: Board) {
| ^^^^^
| ^^^^^^^^^^^^
Error [ETYC0372057]: Only `transition` functions can have a record as input or output.
--> compiler-test:16:18
|
16 | function bar(board: Board) {
| ^^^^^
| ^^^^^^^^^^^^
"""]
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ Error [ETYC0372057]: Only `transition` functions can have a record as input or o
--> compiler-test:9:18
|
9 | function foo(a: credits) -> u8 {
| ^
| ^^^^^^^^^^
Error [ETYC0372057]: Only `transition` functions can have a record as input or output.
--> compiler-test:9:18
|
9 | function foo(a: credits) -> u8 {
| ^
| ^^^^^^^^^^
Error [ETYC0372057]: Only `transition` functions can have a record as input or output.
--> compiler-test:13:41
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ Error [ETYC0372017]: The type `Board` is not found in the current scope.
--> compiler-test:4:35
|
4 | function aria192check_for_win(b: Board, p: u8) -> u128bool {
| ^
| ^^^^^^^^
|
= If you are using an external type, make sure to preface with the program name. Ex: `credits.aleo/credits` instead of `credits`
Error [ETYC0372017]: The type `Board` is not found in the current scope.
--> compiler-test:4:35
|
4 | function aria192check_for_win(b: Board, p: u8) -> u128bool {
| ^
| ^^^^^^^^
|
= If you are using an external type, make sure to preface with the program name. Ex: `credits.aleo/credits` instead of `credits`
Error [ETYC0372017]: The type `u128bool` is not found in the current scope.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Error [ETYC0372017]: The type `Foo` is not found in the current scope.
--> compiler-test:4:28
|
4 | transition main(a: u8, foo: Foo) -> u8 {
| ^^^
| ^^^^^^^^
|
= If you are using an external type, make sure to preface with the program name. Ex: `credits.aleo/credits` instead of `credits`
Error [ETYC0372017]: The type `Foo` is not found in the current scope.
Expand Down
19 changes: 19 additions & 0 deletions tests/expectations/compiler/futures/future_parameter_fail.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace = "Compile"
expectation = "Fail"
outputs = ["""
Error [ETYC0372116]: Futures may only appear as parameters to async functions.
--> compiler-test:4:28
|
4 | async transition first(x: Future) -> Future {
| ^^^^^^^^^
Error [ETYC0372116]: Futures may only appear as parameters to async functions.
--> compiler-test:8:23
|
8 | transition second(x: Future) -> u8 {
| ^^^^^^^^^
Error [ETYC0372116]: Futures may only appear as parameters to async functions.
--> compiler-test:12:20
|
12 | function third(x: Future) -> u8 {
| ^^^^^^^^^
"""]
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ Error [ETYC0372051]: A function cannot take in a tuple as input.
--> compiler-test:4:20
|
4 | transition foo(a: (u8, u16)) -> (u8, u16) {
| ^
| ^^^^^^^^^^^^
"""]
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Error [ETYC0372051]: A function cannot take in a tuple as input.
--> compiler-test:8:18
|
8 | function foo(a: (u8, u16)) -> (u8, u16) {
| ^
| ^^^^^^^^^^^^
Error [ETYC0372049]: A tuple type cannot contain a tuple.
--> compiler-test:12:36
|
Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/parser/finalize/finalize.out
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ functions = [
[
"main",
{ annotations = [], variant = "AsyncFunction", identifier = '{"id":"13","name":"main","span":"{\"lo\":193,\"hi\":197}"}', id = 23, input = [
{ identifier = '{"id":"14","name":"a","span":"{\"lo\":198,\"hi\":199}"}', mode = "None", id = 16, type_ = { Composite = { id = '{"id":"15","name":"foo","span":"{\"lo\":201,\"hi\":204}"}', program = "test" } }, span = { lo = 198, hi = 199 } },
{ identifier = '{"id":"17","name":"b","span":"{\"lo\":206,\"hi\":207}"}', mode = "None", id = 19, type_ = { Composite = { id = '{"id":"18","name":"bar","span":"{\"lo\":209,\"hi\":212}"}', program = "test" } }, span = { lo = 206, hi = 207 } },
{ identifier = '{"id":"14","name":"a","span":"{\"lo\":198,\"hi\":199}"}', mode = "None", id = 16, type_ = { Composite = { id = '{"id":"15","name":"foo","span":"{\"lo\":201,\"hi\":204}"}', program = "test" } }, span = { lo = 198, hi = 204 } },
{ identifier = '{"id":"17","name":"b","span":"{\"lo\":206,\"hi\":207}"}', mode = "None", id = 19, type_ = { Composite = { id = '{"id":"18","name":"bar","span":"{\"lo\":209,\"hi\":212}"}', program = "test" } }, span = { lo = 206, hi = 212 } },
], output = [{ mode = "None", id = 21, type_ = { Composite = { id = '{"id":"20","name":"baz","span":"{\"lo\":217,\"hi\":220}"}', program = "test" } }, span = { lo = 217, hi = 220 } }], output_type = { Composite = { id = '{"id":"20","name":"baz","span":"{\"lo\":217,\"hi\":220}"}', program = "test" } }, block = { statements = [], id = 22, span = { lo = 221, hi = 233 } }, span = { lo = 178, hi = 233 } },
],
]
Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/parser/functions/bounded_recursion.out
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mappings = []
functions = [
[
"x",
{ annotations = [], variant = "Function", identifier = '{"id":"2","name":"x","span":"{\"lo\":39,\"hi\":40}"}', id = 18, input = [{ identifier = '{"id":"3","name":"y","span":"{\"lo\":50,\"hi\":51}"}', mode = "Constant", id = 4, type_ = { Integer = "U32" }, span = { lo = 50, hi = 51 } }], output = [{ mode = "None", id = 5, type_ = { Integer = "U8" }, span = { lo = 61, hi = 63 } }], output_type = { Integer = "U8" }, block = { id = 17, statements = [{ Conditional = { id = 16, condition = { Binary = { op = "Lt", id = 8, left = { Identifier = '{"id":"6","name":"y","span":"{\"lo\":77,\"hi\":78}"}' }, right = { Literal = { Integer = [
{ annotations = [], variant = "Function", identifier = '{"id":"2","name":"x","span":"{\"lo\":39,\"hi\":40}"}', id = 18, input = [{ identifier = '{"id":"3","name":"y","span":"{\"lo\":50,\"hi\":51}"}', mode = "Constant", id = 4, type_ = { Integer = "U32" }, span = { lo = 50, hi = 56 } }], output = [{ mode = "None", id = 5, type_ = { Integer = "U8" }, span = { lo = 61, hi = 63 } }], output_type = { Integer = "U8" }, block = { id = 17, statements = [{ Conditional = { id = 16, condition = { Binary = { op = "Lt", id = 8, left = { Identifier = '{"id":"6","name":"y","span":"{\"lo\":77,\"hi\":78}"}' }, right = { Literal = { Integer = [
"U32",
"5",
{ span = { lo = 81, hi = 85 } },
Expand All @@ -29,7 +29,7 @@ functions = [
],
[
"main",
{ annotations = [], variant = "Function", identifier = '{"id":"19","name":"main","span":"{\"lo\":145,\"hi\":149}"}', output_type = "Boolean", id = 30, input = [{ identifier = '{"id":"20","name":"y","span":"{\"lo\":150,\"hi\":151}"}', mode = "None", type_ = "Boolean", id = 21, span = { lo = 150, hi = 151 } }], output = [{ mode = "None", type_ = "Boolean", id = 22, span = { lo = 162, hi = 166 } }], block = { id = 29, statements = [
{ annotations = [], variant = "Function", identifier = '{"id":"19","name":"main","span":"{\"lo\":145,\"hi\":149}"}', output_type = "Boolean", id = 30, input = [{ identifier = '{"id":"20","name":"y","span":"{\"lo\":150,\"hi\":151}"}', mode = "None", type_ = "Boolean", id = 21, span = { lo = 150, hi = 157 } }], output = [{ mode = "None", type_ = "Boolean", id = 22, span = { lo = 162, hi = 166 } }], block = { id = 29, statements = [
{ Expression = { id = 26, expression = { Call = { program = "test", id = 25, arguments = [{ Literal = { Integer = [
"U32",
"1",
Expand Down
8 changes: 4 additions & 4 deletions tests/expectations/parser/functions/const_param.out
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ functions = [
[
"x",
{ annotations = [], variant = "Function", identifier = '{"id":"2","name":"x","span":"{\"lo\":39,\"hi\":40}"}', id = 11, input = [
{ identifier = '{"id":"3","name":"x","span":"{\"lo\":41,\"hi\":42}"}', mode = "None", id = 4, type_ = { Integer = "U32" }, span = { lo = 41, hi = 42 } },
{ identifier = '{"id":"5","name":"y","span":"{\"lo\":58,\"hi\":59}"}', mode = "Constant", id = 6, type_ = { Integer = "I32" }, span = { lo = 58, hi = 59 } },
{ identifier = '{"id":"3","name":"x","span":"{\"lo\":41,\"hi\":42}"}', mode = "None", id = 4, type_ = { Integer = "U32" }, span = { lo = 41, hi = 47 } },
{ identifier = '{"id":"5","name":"y","span":"{\"lo\":58,\"hi\":59}"}', mode = "Constant", id = 6, type_ = { Integer = "I32" }, span = { lo = 58, hi = 64 } },
], output = [{ mode = "None", id = 7, type_ = { Integer = "U8" }, span = { lo = 69, hi = 71 } }], output_type = { Integer = "U8" }, block = { id = 10, statements = [{ Return = { id = 9, expression = { Literal = { Integer = [
"U8",
"0",
Expand All @@ -28,8 +28,8 @@ functions = [
[
"x",
{ annotations = [], variant = "Function", identifier = '{"id":"12","name":"x","span":"{\"lo\":118,\"hi\":119}"}', id = 21, input = [
{ identifier = '{"id":"13","name":"x","span":"{\"lo\":129,\"hi\":130}"}', mode = "Constant", id = 14, type_ = { Integer = "U32" }, span = { lo = 129, hi = 130 } },
{ identifier = '{"id":"15","name":"y","span":"{\"lo\":137,\"hi\":138}"}', mode = "None", id = 16, type_ = { Integer = "I32" }, span = { lo = 137, hi = 138 } },
{ identifier = '{"id":"13","name":"x","span":"{\"lo\":129,\"hi\":130}"}', mode = "Constant", id = 14, type_ = { Integer = "U32" }, span = { lo = 129, hi = 135 } },
{ identifier = '{"id":"15","name":"y","span":"{\"lo\":137,\"hi\":138}"}', mode = "None", id = 16, type_ = { Integer = "I32" }, span = { lo = 137, hi = 143 } },
], output = [{ mode = "None", id = 17, type_ = { Integer = "U8" }, span = { lo = 148, hi = 150 } }], output_type = { Integer = "U8" }, block = { id = 20, statements = [{ Return = { id = 19, expression = { Literal = { Integer = [
"U8",
"0",
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/parser/functions/constant_input.out
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ structs = []
mappings = []
functions = [[
"x",
{ annotations = [], variant = "Function", identifier = '{"id":"2","name":"x","span":"{\"lo\":39,\"hi\":40}"}', id = 7, input = [{ identifier = '{"id":"3","name":"x","span":"{\"lo\":50,\"hi\":51}"}', mode = "Constant", id = 4, type_ = { Integer = "U8" }, span = { lo = 50, hi = 51 } }], output = [{ mode = "None", id = 5, type_ = { Integer = "U8" }, span = { lo = 60, hi = 62 } }], output_type = { Integer = "U8" }, block = { statements = [], id = 6, span = { lo = 63, hi = 65 } }, span = { lo = 30, hi = 65 } },
{ annotations = [], variant = "Function", identifier = '{"id":"2","name":"x","span":"{\"lo\":39,\"hi\":40}"}', id = 7, input = [{ identifier = '{"id":"3","name":"x","span":"{\"lo\":50,\"hi\":51}"}', mode = "Constant", id = 4, type_ = { Integer = "U8" }, span = { lo = 50, hi = 55 } }], output = [{ mode = "None", id = 5, type_ = { Integer = "U8" }, span = { lo = 60, hi = 62 } }], output_type = { Integer = "U8" }, block = { statements = [], id = 6, span = { lo = 63, hi = 65 } }, span = { lo = 30, hi = 65 } },
]]

[outputs.program_scopes.test.span]
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/parser/functions/infinite_recursion.out
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ functions = [
],
[
"main",
{ annotations = [], variant = "Function", identifier = '{"id":"9","name":"main","span":"{\"lo\":92,\"hi\":96}"}', output_type = "Boolean", id = 19, input = [{ identifier = '{"id":"10","name":"y","span":"{\"lo\":97,\"hi\":98}"}', mode = "None", type_ = "Boolean", id = 11, span = { lo = 97, hi = 98 } }], output = [{ mode = "None", type_ = "Boolean", id = 12, span = { lo = 109, hi = 113 } }], block = { id = 18, statements = [
{ annotations = [], variant = "Function", identifier = '{"id":"9","name":"main","span":"{\"lo\":92,\"hi\":96}"}', output_type = "Boolean", id = 19, input = [{ identifier = '{"id":"10","name":"y","span":"{\"lo\":97,\"hi\":98}"}', mode = "None", type_ = "Boolean", id = 11, span = { lo = 97, hi = 104 } }], output = [{ mode = "None", type_ = "Boolean", id = 12, span = { lo = 109, hi = 113 } }], block = { id = 18, statements = [
{ Expression = { id = 15, expression = { Call = { arguments = [], program = "test", id = 14, function = { Identifier = '{"id":"13","name":"inf","span":"{\"lo\":124,\"hi\":127}"}' }, span = { lo = 124, hi = 129 } } }, span = { lo = 124, hi = 130 } } },
{ Return = { id = 17, expression = { Identifier = '{"id":"16","name":"y","span":"{\"lo\":146,\"hi\":147}"}' }, span = { lo = 139, hi = 148 } } },
], span = { lo = 114, hi = 154 } }, span = { lo = 83, hi = 154 } },
Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/parser/functions/inline_function.out
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ mappings = []
functions = [[
"foo",
{ annotations = [], variant = "Inline", identifier = '{"id":"2","name":"foo","span":"{\"lo\":33,\"hi\":36}"}', id = 11, input = [
{ identifier = '{"id":"3","name":"x","span":"{\"lo\":37,\"hi\":38}"}', mode = "None", id = 4, type_ = { Integer = "U32" }, span = { lo = 37, hi = 38 } },
{ identifier = '{"id":"5","name":"y","span":"{\"lo\":45,\"hi\":46}"}', mode = "None", id = 6, type_ = { Integer = "I32" }, span = { lo = 45, hi = 46 } },
{ identifier = '{"id":"3","name":"x","span":"{\"lo\":37,\"hi\":38}"}', mode = "None", id = 4, type_ = { Integer = "U32" }, span = { lo = 37, hi = 43 } },
{ identifier = '{"id":"5","name":"y","span":"{\"lo\":45,\"hi\":46}"}', mode = "None", id = 6, type_ = { Integer = "I32" }, span = { lo = 45, hi = 51 } },
], output = [{ mode = "None", id = 7, type_ = { Integer = "U32" }, span = { lo = 56, hi = 59 } }], output_type = { Integer = "U32" }, block = { id = 10, statements = [{ Return = { id = 9, expression = { Literal = { Integer = [
"U32",
"0",
Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/parser/functions/params.out
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ mappings = []
functions = [[
"x",
{ annotations = [], variant = "Function", identifier = '{"id":"2","name":"x","span":"{\"lo\":39,\"hi\":40}"}', id = 11, input = [
{ identifier = '{"id":"3","name":"x","span":"{\"lo\":41,\"hi\":42}"}', mode = "None", id = 4, type_ = { Integer = "U32" }, span = { lo = 41, hi = 42 } },
{ identifier = '{"id":"5","name":"y","span":"{\"lo\":49,\"hi\":50}"}', mode = "None", id = 6, type_ = { Integer = "I32" }, span = { lo = 49, hi = 50 } },
{ identifier = '{"id":"3","name":"x","span":"{\"lo\":41,\"hi\":42}"}', mode = "None", id = 4, type_ = { Integer = "U32" }, span = { lo = 41, hi = 47 } },
{ identifier = '{"id":"5","name":"y","span":"{\"lo\":49,\"hi\":50}"}', mode = "None", id = 6, type_ = { Integer = "I32" }, span = { lo = 49, hi = 55 } },
], output = [{ mode = "None", id = 7, type_ = { Integer = "U8" }, span = { lo = 60, hi = 62 } }], output_type = { Integer = "U8" }, block = { id = 10, statements = [{ Return = { id = 9, expression = { Literal = { Integer = [
"U8",
"0",
Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/parser/functions/params_return.out
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ mappings = []
functions = [[
"x",
{ annotations = [], variant = "Function", identifier = '{"id":"2","name":"x","span":"{\"lo\":39,\"hi\":40}"}', id = 11, input = [
{ identifier = '{"id":"3","name":"x","span":"{\"lo\":41,\"hi\":42}"}', mode = "None", id = 4, type_ = { Integer = "U32" }, span = { lo = 41, hi = 42 } },
{ identifier = '{"id":"5","name":"y","span":"{\"lo\":49,\"hi\":50}"}', mode = "None", id = 6, type_ = { Integer = "I32" }, span = { lo = 49, hi = 50 } },
{ identifier = '{"id":"3","name":"x","span":"{\"lo\":41,\"hi\":42}"}', mode = "None", id = 4, type_ = { Integer = "U32" }, span = { lo = 41, hi = 47 } },
{ identifier = '{"id":"5","name":"y","span":"{\"lo\":49,\"hi\":50}"}', mode = "None", id = 6, type_ = { Integer = "I32" }, span = { lo = 49, hi = 55 } },
], output = [{ mode = "None", id = 7, type_ = { Integer = "U32" }, span = { lo = 60, hi = 63 } }], output_type = { Integer = "U32" }, block = { id = 10, statements = [{ Return = { id = 9, expression = { Literal = { Integer = [
"U8",
"0",
Expand Down
Loading

0 comments on commit a81b97d

Please sign in to comment.