From cf214173689b90ce6081c0cd36ebc4e80e418e2c Mon Sep 17 00:00:00 2001 From: zhaoshuting Date: Tue, 2 Sep 2025 17:22:33 +0800 Subject: [PATCH] Fix unexpected CTE in async lambda Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICW0W6 Signed-off-by: zhaoshuting --- ets2panda/checker/ETSAnalyzer.cpp | 20 +++++++++++-- .../ets/async_lambda_return_type_test.ets | 30 +++++++++++++++++++ .../declgen-ets2ts-runtime-ignored.txt | 1 + 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 ets2panda/test/runtime/ets/async_lambda_return_type_test.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index decda894ce..91fbc4ac29 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -1123,6 +1123,21 @@ void TryInferPreferredType(ir::ArrowFunctionExpression *expr, checker::Type *pre } } +static bool IsUnionTypeContainingPromise(checker::Type *type, ETSChecker *checker) +{ + if (!type->IsETSUnionType()) { + return false; + } + for (auto subtype : type->AsETSUnionType()->ConstituentTypes()) { + if (subtype->IsETSObjectType() && + subtype->AsETSObjectType()->GetOriginalBaseType() == checker->GlobalBuiltinPromiseType()) { + return true; + } + } + + return false; +} + checker::Type *ETSAnalyzer::Check(ir::ArrowFunctionExpression *expr) const { ETSChecker *checker = GetETSChecker(); @@ -1184,8 +1199,9 @@ checker::Type *ETSAnalyzer::Check(ir::ArrowFunctionExpression *expr) const if (expr->Function()->ReturnTypeAnnotation() == nullptr) { if (expr->Function()->IsAsyncFunc()) { auto *retType = signature->ReturnType(); - if (!retType->IsETSObjectType() || - retType->AsETSObjectType()->GetOriginalBaseType() != checker->GlobalBuiltinPromiseType()) { + if (!IsUnionTypeContainingPromise(retType, checker) && + (!retType->IsETSObjectType() || + retType->AsETSObjectType()->GetOriginalBaseType() != checker->GlobalBuiltinPromiseType())) { auto returnType = checker->CreateETSAsyncFuncReturnTypeFromBaseType(signature->ReturnType()); ES2PANDA_ASSERT(returnType != nullptr); expr->Function()->Signature()->SetReturnType(returnType->PromiseType()); diff --git a/ets2panda/test/runtime/ets/async_lambda_return_type_test.ets b/ets2panda/test/runtime/ets/async_lambda_return_type_test.ets new file mode 100644 index 0000000000..71a3cec3a5 --- /dev/null +++ b/ets2panda/test/runtime/ets/async_lambda_return_type_test.ets @@ -0,0 +1,30 @@ +/* + * 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. + */ + +let money_box: number[] = Array.from([10.0, 5.0, 20.0, 50.0, 100.0, 200.0, 500.0]) +let lock: AsyncLock = AsyncLock.request("lock_1") +let len: number = 0 + +async function initialize() { + len = await lock.lockAsync(async () => { + return money_box.length + }) +} + +function main() { + initialize().then(() => { + arktest.assertEQ(len, 7) + }) +} diff --git a/ets2panda/test/test-lists/declgenets2ts/ets-runtime/declgen-ets2ts-runtime-ignored.txt b/ets2panda/test/test-lists/declgenets2ts/ets-runtime/declgen-ets2ts-runtime-ignored.txt index ab78fc6dce..307994e6fe 100644 --- a/ets2panda/test/test-lists/declgenets2ts/ets-runtime/declgen-ets2ts-runtime-ignored.txt +++ b/ets2panda/test/test-lists/declgenets2ts/ets-runtime/declgen-ets2ts-runtime-ignored.txt @@ -30,3 +30,4 @@ typeAlias_2.ets Multiline_string.ets Multiline_string_escape_char.ets run_ok_without_semicolon.ets +async_lambda_return_type_test.ets -- Gitee