diff --git a/ets2panda/compiler/core/ETSemitter.cpp b/ets2panda/compiler/core/ETSemitter.cpp index 4da7d669bffc01517c9a77c6411698abc5df42e5..1151febda86663cf246872e802c7f7cee77ad1ef 100644 --- a/ets2panda/compiler/core/ETSemitter.cpp +++ b/ets2panda/compiler/core/ETSemitter.cpp @@ -1121,20 +1121,16 @@ ir::MethodDefinition *ETSEmitter::FindAsyncImpl(ir::ScriptFunction *asyncFunc) return nullptr; } - auto *checker = static_cast(Context()->GetChecker()); - checker::TypeRelation *typeRel = checker->Relation(); - checker::SavedTypeRelationFlagsContext savedFlagsCtx(typeRel, checker::TypeRelationFlag::NO_RETURN_TYPE_CHECK); - ES2PANDA_ASSERT(method->Function() != nullptr); - method->Function()->Signature()->IsSubtypeOf(typeRel, asyncFunc->Signature()); - if (typeRel->IsTrue()) { + if (asyncFunc->AsyncPairMethod() == method->Function()) { return method; } + for (auto overload : method->Overloads()) { - overload->Function()->Signature()->IsSubtypeOf(typeRel, asyncFunc->Signature()); - if (typeRel->IsTrue()) { + if (asyncFunc->AsyncPairMethod() == overload->Function()) { return overload; } } + return nullptr; } diff --git a/ets2panda/compiler/lowering/ets/asyncMethodLowering.cpp b/ets2panda/compiler/lowering/ets/asyncMethodLowering.cpp index be15ae71a728ab845bcecc16c3347c18d2a49559..a86ecfec9b85739dccd22137e91b85bec89229d0 100644 --- a/ets2panda/compiler/lowering/ets/asyncMethodLowering.cpp +++ b/ets2panda/compiler/lowering/ets/asyncMethodLowering.cpp @@ -170,6 +170,7 @@ void ComposeAsyncImplMethod(checker::ETSChecker *checker, ir::MethodDefinition * implMethod->Check(checker); node->SetAsyncPairMethod(implMethod); + node->Function()->SetAsyncPairMethod(implMethod->Function()); ES2PANDA_ASSERT(node->Function() != nullptr); if (node->Function()->IsOverload()) { diff --git a/ets2panda/compiler/lowering/ets/lambdaLowering.cpp b/ets2panda/compiler/lowering/ets/lambdaLowering.cpp index 753115eafd1faf593d72cb569aff0ef05ebb03d2..2e42d9bb9cfac8e1c311efe0e54889a25e899625 100644 --- a/ets2panda/compiler/lowering/ets/lambdaLowering.cpp +++ b/ets2panda/compiler/lowering/ets/lambdaLowering.cpp @@ -511,6 +511,9 @@ static ir::MethodDefinition *CreateCallee(public_lib::Context *ctx, ir::ArrowFun cmInfo.calleeName = calleeName; cmInfo.body = body; cmInfo.forcedReturnType = forcedReturnType; + if (lambda->Function()->IsAsyncFunc()) { + cmInfo.auxFunctionFlags = ir::ScriptFunctionFlags::ASYNC_IMPL; + } auto *method = CreateCalleeMethod(ctx, lambda, info, &cmInfo); if (lambda->Function()->IsAsyncFunc()) { @@ -521,6 +524,7 @@ static ir::MethodDefinition *CreateCallee(public_lib::Context *ctx, ir::ArrowFun cmInfoAsync.auxModifierFlags = ir::ModifierFlags::NATIVE; cmInfoAsync.auxFunctionFlags = ir::ScriptFunctionFlags::ASYNC; auto *asyncMethod = CreateCalleeMethod(ctx, lambda, info, &cmInfoAsync); + asyncMethod->Function()->SetAsyncPairMethod(method->Function()); return asyncMethod; } diff --git a/ets2panda/ir/base/scriptFunction.cpp b/ets2panda/ir/base/scriptFunction.cpp index 77ef01fa453dbffd84f9169cf64f98ba13eaa96a..f424f14f5dab028b27ff184e400473e847ace399 100644 --- a/ets2panda/ir/base/scriptFunction.cpp +++ b/ets2panda/ir/base/scriptFunction.cpp @@ -115,7 +115,8 @@ ScriptFunction::ScriptFunction(ArenaAllocator *allocator, ScriptFunctionData &&d body_(data.body), funcFlags_(data.funcFlags), lang_(data.lang), - returnStatements_(allocator->Adapter()) + returnStatements_(allocator->Adapter()), + asyncPairFunction_(nullptr) { for (auto *param : irSignature_.Params()) { param->SetParent(this); @@ -137,7 +138,8 @@ ScriptFunction::ScriptFunction(ArenaAllocator *allocator, ScriptFunctionData &&d body_(data.body), funcFlags_(data.funcFlags), lang_(data.lang), - returnStatements_(allocator->Adapter()) + returnStatements_(allocator->Adapter()), + asyncPairFunction_(nullptr) { for (auto *param : irSignature_.Params()) { param->SetParent(this); diff --git a/ets2panda/ir/base/scriptFunction.h b/ets2panda/ir/base/scriptFunction.h index e0d169804baad3b3400207557bb1fd314112f795..276eb1ad91c9a9a61a1aba79cc6921f29e4f8044 100644 --- a/ets2panda/ir/base/scriptFunction.h +++ b/ets2panda/ir/base/scriptFunction.h @@ -327,6 +327,21 @@ public: return GetHistoryNodeAs()->preferredReturnType_; } + void SetAsyncPairMethod(ScriptFunction *asyncPairFunction) + { + this->GetOrCreateHistoryNodeAs()->asyncPairFunction_ = asyncPairFunction; + } + + [[nodiscard]] const ScriptFunction *AsyncPairMethod() const noexcept + { + return GetHistoryNodeAs()->asyncPairFunction_; + } + + [[nodiscard]] ScriptFunction *AsyncPairMethod() noexcept + { + return GetHistoryNodeAs()->asyncPairFunction_; + } + [[nodiscard]] ScriptFunction *Clone(ArenaAllocator *allocator, AstNode *parent) override; void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; @@ -376,6 +391,7 @@ private: checker::Type *preferredReturnType_ {}; es2panda::Language lang_; ArenaVector returnStatements_; + ScriptFunction *asyncPairFunction_; }; } // namespace ark::es2panda::ir diff --git a/ets2panda/test/ast/compiler/ets/async_with_lambda.ets b/ets2panda/test/ast/compiler/ets/async_with_lambda.ets new file mode 100644 index 0000000000000000000000000000000000000000..f4dadb97d1d493aea22860f47b173b7202055a90 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/async_with_lambda.ets @@ -0,0 +1,26 @@ +/* + * 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: (arg: T) => Promise): Promise { + return (async (): Promise => { + const result = await Promise.resolve(undefined); + if (result !== undefined) { + await a(result); + } else { + await a(undefined as T); + } + return undefined; + })(); +} \ No newline at end of file diff --git a/ets2panda/test/unit/sizeof_node_test.cpp b/ets2panda/test/unit/sizeof_node_test.cpp index 5980cba512fa4d025bd7114ab44edff712304031..f7924975f6f7a07e337bd54c42916282b207d941 100644 --- a/ets2panda/test/unit/sizeof_node_test.cpp +++ b/ets2panda/test/unit/sizeof_node_test.cpp @@ -248,6 +248,7 @@ size_t SizeOfNodeTest::SizeOf() Align(sizeof(node->funcFlags_)) + sizeof(node->signature_) + sizeof(node->preferredReturnType_) + + sizeof(node->asyncPairFunction_) + Align(sizeof(node->lang_)) + sizeof(node->returnStatements_); // clang-format on