From a721cef09ec7fbbeb31689242ed3b9b228c55164 Mon Sep 17 00:00:00 2001 From: zhaoshuting Date: Thu, 4 Sep 2025 09:36:14 +0800 Subject: [PATCH] Fix CTE when ret statement not have an expression Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICW9Q1 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 | 29 +++++++++++++++++ .../test/runtime/ets/return_type_test.ets | 32 +++++++++++++++++++ 5 files changed, 72 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 ebc28058a2..53f045d8b8 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -3633,7 +3633,8 @@ bool ETSAnalyzer::CheckInferredFunctionReturnType(ir::ReturnStatement *st, ir::S if (st->argument_ == nullptr) { ES2PANDA_ASSERT(funcReturnType != nullptr); if (!funcReturnType->IsETSVoidType() && funcReturnType != checker->GlobalVoidType() && - !funcReturnType->IsETSAsyncFuncReturnType()) { + !funcReturnType->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 e42b1e6dbf..b173028694 100644 --- a/ets2panda/compiler/lowering/ets/lambdaLowering.cpp +++ b/ets2panda/compiler/lowering/ets/lambdaLowering.cpp @@ -534,6 +534,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..9171a58bbc --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/return_type_test.ets @@ -0,0 +1,29 @@ +/* + * 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(): Promise { + let pro = new Promise((resolve) => { + resolve(21); + }); + + const thenResult = pro.then(async (): Promise => { + return; + }); + + return thenResult; +} + +/* @@? 23: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..6a35e24cf9 --- /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(): Promise { + 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