From a8d607b62e7e6c077702432e8f48fe7650eb9e80 Mon Sep 17 00:00:00 2001 From: zhaoshuting Date: Thu, 4 Sep 2025 09:36:14 +0800 Subject: [PATCH] Cherry-pick !8215 from 0702 to 0728 Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICWPWU Signed-off-by: zhaoshuting --- ets2panda/checker/ETSAnalyzer.cpp | 3 +- .../compiler/lowering/ets/lambdaLowering.cpp | 8 +++++ ets2panda/ir/astNodeFlags.h | 1 + .../ast/compiler/ets/return_type_test.ets | 28 ++++++++++++++++ .../test/runtime/ets/return_type_test.ets | 32 +++++++++++++++++++ 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/compiler/ets/return_type_test.ets create mode 100644 ets2panda/test/runtime/ets/return_type_test.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 18f30b2681..8ce6f3a28f 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 b215a47e80..232cdb4004 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 2f8281d0a0..2d26cce87b 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 0000000000..763211a6dc --- /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 0000000000..5761170031 --- /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); + }); +} -- Gitee