Forbit explicit inclusion of async function return type

This commit is contained in:
evan-schott 2024-04-08 11:27:42 -07:00
parent 5790124de4
commit b422657944
4 changed files with 40 additions and 2 deletions

View File

@ -629,7 +629,7 @@ impl<'a> ExpressionVisitor<'a> for TypeChecker<'a> {
// Async functions return a single future.
let mut ret = if func.variant == AsyncFunction {
if let Some(Type::Future(_)) = expected {
Type::Future(FutureType::new(Vec::new()))
Type::Future(FutureType::new(Vec::new(), Some(Location::new(input.program, ident.name))))
} else {
self.emit_err(TypeCheckerError::return_type_of_finalize_function_is_future(input.span));
Type::Unit

View File

@ -928,7 +928,7 @@ create_messages!(
finalize_function_cannot_return_value {
args: (),
msg: "An async function is not allowed to return a value.".to_string(),
help: Some("Remove an output type in the function signature, and remove the return statement from the function.".to_string()),
help: Some("Remove an output type in the function signature, and remove the return statement from the function. Note that the future returned by async functions is automatically inferred, and must not be explicitly written.".to_string()),
}
@formatted

View File

@ -0,0 +1,5 @@
---
namespace: Compile
expectation: Fail
outputs:
- "Error [ETYC0372118]: An async function is not allowed to return a value.\n --> compiler-test:10:5\n |\n 10 | async function finalize() -> Future {\n 11 | Mapping::set(foo, 1u32, 1u32);\n 12 | }\n | ^\n |\n = Remove an output type in the function signature, and remove the return statement from the function. Note that the future returned by async functions is automatically inferred, and must not be explicitly written.\nError [ETYC0372038]: Function must return a value.\n --> compiler-test:10:5\n |\n 10 | async function finalize() -> Future {\n 11 | Mapping::set(foo, 1u32, 1u32);\n 12 | }\n | ^\n"

View File

@ -0,0 +1,33 @@
/*
namespace: Compile
expectation: Fail
*/
program test.aleo {
mapping foo: u32 => u32;
async transition main_inner(public a: u32, b: u32) -> (u32, Future) {
let c: u32 = a + b;
let f: Future = finalize();
return (c, f);
}
async function finalize() -> Future {
Mapping::set(foo, 1u32, 1u32);
}
}
// --- Next Program --- //
import test.aleo;
program basic.aleo {
async transition main(public a: u32, b: u32) -> (u32, Future) {
let c: u32 = a + b;
let (d, f1): (u32, Future) = test.aleo/main_inner(1u32, 1u32);
let f:Future = finalize(c, f1);
return (c,f);
}
async function finalize(input: u32, f: Future) {
f.await();
assert_eq(input, 1u32);
}
}