From 388f73ed37badf90d3362621a9ac568c99d3143c Mon Sep 17 00:00:00 2001 From: turgutbababalim Date: Wed, 9 Jul 2025 15:55:10 +0300 Subject: [PATCH] Added a check for async implementation to emitter Issue: ICL88O Description: Lowered async lambdas now has an additional check in the emitter to avoid crashes. Signed-off-by: turgutbababalim --- ets2panda/compiler/core/ETSemitter.cpp | 12 +++------ .../lowering/ets/asyncMethodLowering.cpp | 1 + .../compiler/lowering/ets/lambdaLowering.cpp | 4 +++ ets2panda/ir/base/scriptFunction.cpp | 6 +++-- ets2panda/ir/base/scriptFunction.h | 16 ++++++++++++ .../ast/compiler/ets/async_with_lambda.ets | 26 +++++++++++++++++++ ets2panda/test/unit/sizeof_node_test.cpp | 1 + 7 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/async_with_lambda.ets diff --git a/ets2panda/compiler/core/ETSemitter.cpp b/ets2panda/compiler/core/ETSemitter.cpp index 4da7d669bf..1151febda8 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 be15ae71a7..a86ecfec9b 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 753115eafd..2e42d9bb9c 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 77ef01fa45..f424f14f5d 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 e0d169804b..276eb1ad91 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 0000000000..f4dadb97d1 --- /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 5980cba512..f7924975f6 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 -- Gitee