diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 18f30b2681dcedcc830305bab68e08c59fce5ebd..8ce6f3a28f679247b36a405d63b6298a6504fe08 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -3622,7 +3622,8 @@ bool ETSAnalyzer::CheckInferredFunctionReturnType(ir::ReturnStatement *st, ir::S ES2PANDA_ASSERT(funcReturnType != nullptr); if (!funcReturnType->MaybeBaseTypeOfGradualType()->IsETSVoidType() && funcReturnType != checker->GlobalVoidType() && - !funcReturnType->MaybeBaseTypeOfGradualType()->IsETSAsyncFuncReturnType()) { + !funcReturnType->MaybeBaseTypeOfGradualType()->IsETSAsyncFuncReturnType() && + (containingFunc->Flags() & ir::ScriptFunctionFlags::RETURN_PROMISEVOID) == 0) { checker->LogError(diagnostic::RETURN_WITHOUT_VALUE, {}, st->Start()); return false; } diff --git a/ets2panda/compiler/lowering/ets/lambdaLowering.cpp b/ets2panda/compiler/lowering/ets/lambdaLowering.cpp index b215a47e80c574e68c599f9d7163f878bb44299e..232cdb4004ac9128be78f6c9dfce9023414d3a03 100644 --- a/ets2panda/compiler/lowering/ets/lambdaLowering.cpp +++ b/ets2panda/compiler/lowering/ets/lambdaLowering.cpp @@ -513,6 +513,14 @@ static ir::MethodDefinition *CreateCallee(public_lib::Context *ctx, ir::ArrowFun cmInfo.forcedReturnType = forcedReturnType; if (lambda->Function()->IsAsyncFunc()) { cmInfo.auxFunctionFlags = ir::ScriptFunctionFlags::ASYNC_IMPL; + auto retTypeAnnotation = lambda->Function()->ReturnTypeAnnotation(); + if (retTypeAnnotation != nullptr && retTypeAnnotation->TsType()->IsETSObjectType()) { + auto retType = retTypeAnnotation->TsType()->AsETSObjectType(); + if (retType->GetOriginalBaseType() == checker->GlobalBuiltinPromiseType() && + retType->TypeArguments().front() == checker->GlobalVoidType()) { + cmInfo.auxFunctionFlags = ir::ScriptFunctionFlags::RETURN_PROMISEVOID | cmInfo.auxFunctionFlags; + } + } } auto *method = CreateCalleeMethod(ctx, lambda, info, &cmInfo); diff --git a/ets2panda/ir/astNodeFlags.h b/ets2panda/ir/astNodeFlags.h index 2f8281d0a0cc3815930402f3a90e7c7ff3563c24..2d26cce87b2c72eb18c004d3abefbb41fab060e4 100644 --- a/ets2panda/ir/astNodeFlags.h +++ b/ets2panda/ir/astNodeFlags.h @@ -122,6 +122,7 @@ enum class ScriptFunctionFlags : uint32_t { IN_RECORD = 1U << 22U, TRAILING_LAMBDA = 1U << 23U, SYNTHETIC = 1U << 24U, + RETURN_PROMISEVOID = 1U << 25U, }; enum class TSOperatorType { READONLY, KEYOF, UNIQUE }; diff --git a/ets2panda/test/ast/compiler/ets/return_type_test.ets b/ets2panda/test/ast/compiler/ets/return_type_test.ets new file mode 100644 index 0000000000000000000000000000000000000000..763211a6dc4416615d067f8d7d8daec57d19fb46 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/return_type_test.ets @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function test() { + let pro = new Promise((resolve) => { + resolve(21); + }); + + const thenResult = pro.then(async (): Promise => { + return; + }); + + return thenResult; +} + +/* @@? 22:9 Error TypeError: Missing return value. */ diff --git a/ets2panda/test/runtime/ets/return_type_test.ets b/ets2panda/test/runtime/ets/return_type_test.ets new file mode 100644 index 0000000000000000000000000000000000000000..5761170031b90ce5e9ff56c25274234cb5362fb9 --- /dev/null +++ b/ets2panda/test/runtime/ets/return_type_test.ets @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function test() { + let pro = new Promise((resolve) => { + resolve(undefined); + }); + + const thenResult = pro.then(async (): Promise => { + return; + }); + + return thenResult; +} + +function main(): void { + test().then(result => { + arktest.assertTrue(result === undefined); + }); +}