From aac34e483e7ba93cd551e7dc7278710dbb83de1d Mon Sep 17 00:00:00 2001 From: daizihan Date: Sun, 20 Jul 2025 19:12:40 +0800 Subject: [PATCH] Fix async overload crash Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICNICD?from=project-issue Signed-off-by: daizihan --- .../invariants/everyChildHasValidParent.cpp | 2 +- .../lowering/ets/asyncMethodLowering.cpp | 7 +++- ets2panda/test/runtime/ets/async_overload.ets | 32 +++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 ets2panda/test/runtime/ets/async_overload.ets diff --git a/ets2panda/ast_verifier/invariants/everyChildHasValidParent.cpp b/ets2panda/ast_verifier/invariants/everyChildHasValidParent.cpp index 7761dc5a5e..468ba99a4c 100644 --- a/ets2panda/ast_verifier/invariants/everyChildHasValidParent.cpp +++ b/ets2panda/ast_verifier/invariants/everyChildHasValidParent.cpp @@ -32,7 +32,7 @@ CheckResult EveryChildHasValidParent::operator()(const ir::AstNode *ast) auto overloads = maybeBaseOverloadMethod->Overloads(); auto res = std::find_if(overloads.begin(), overloads.end(), [node](ir::MethodDefinition *m) { return m == node; }); - return res != overloads.end(); + return res != overloads.end() || maybeBaseOverloadMethod->AsyncPairMethod() == node; } return false; }; diff --git a/ets2panda/compiler/lowering/ets/asyncMethodLowering.cpp b/ets2panda/compiler/lowering/ets/asyncMethodLowering.cpp index 01d054a717..d93cfa4310 100644 --- a/ets2panda/compiler/lowering/ets/asyncMethodLowering.cpp +++ b/ets2panda/compiler/lowering/ets/asyncMethodLowering.cpp @@ -173,11 +173,16 @@ void ComposeAsyncImplMethod(checker::ETSChecker *checker, ir::MethodDefinition * node->SetAsyncPairMethod(implMethod); ES2PANDA_ASSERT(node->Function() != nullptr); - if (node->Function()->IsOverload()) { + if (node->Function()->IsOverload() && node->BaseOverloadMethod()->AsyncPairMethod() != nullptr) { auto *baseOverloadImplMethod = node->BaseOverloadMethod()->AsyncPairMethod(); ES2PANDA_ASSERT(implMethod->Function() != nullptr && baseOverloadImplMethod->Function() != nullptr); implMethod->Function()->Id()->SetVariable(baseOverloadImplMethod->Function()->Id()->Variable()); baseOverloadImplMethod->AddOverload(implMethod); + } else if (node->Function()->IsOverload() && node->BaseOverloadMethod()->AsyncPairMethod() == nullptr) { + // If it's base overload function doesnot marked as async, + // then current AsyncImpl should be treated as AsyncPairMethod in base overload. + node->BaseOverloadMethod()->SetAsyncPairMethod(implMethod); + classDef->Body().push_back(implMethod); } else { classDef->Body().push_back(implMethod); } diff --git a/ets2panda/test/runtime/ets/async_overload.ets b/ets2panda/test/runtime/ets/async_overload.ets new file mode 100644 index 0000000000..f50ea63200 --- /dev/null +++ b/ets2panda/test/runtime/ets/async_overload.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 foo(a : string){ + return 1; +} + +async function foo() { + return 2 +} + +async function foo(a: int) { + return 3 +} + +function main() { + arktest.assertEQ(foo("test"), 1); + arktest.assertEQ(await foo(), 2); + arktest.assertEQ(await foo(1), 3); +} -- Gitee