From 488b0c11f774b15c07c2ac446feeec14bc27b3b9 Mon Sep 17 00:00:00 2001 From: xuxinjie4 Date: Wed, 16 Jul 2025 11:47:29 +0800 Subject: [PATCH] Fix lambda bug Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICMK8W?from=project-issue Signed-off-by: xuxinjie4 --- ets2panda/checker/ETSAnalyzer.cpp | 4 +--- .../compiler/lowering/ets/dynamicImport.cpp | 4 ++++ .../compiler/lowering/ets/lambdaLowering.cpp | 5 ++-- .../lowering/ets/restArgsLowering.cpp | 4 +--- .../dynamic_function_use_as_value.ets | 24 +++++++++++++++++++ 5 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 ets2panda/test/ast/parser/ets/dynamic_import_tests/dynamic_function_use_as_value.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index fb821fb4234..59bd40ad218 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -1796,9 +1796,7 @@ checker::Type *ETSAnalyzer::Check(ir::ConditionalExpression *expr) const static Type *TransformTypeForMethodReference(ETSChecker *checker, ir::Expression *const use, Type *type) { ES2PANDA_ASSERT(use->IsIdentifier() || use->IsMemberExpression()); - if (!type->IsETSMethodType() || - (use->IsMemberExpression() && use->AsMemberExpression()->PropVar() != nullptr && - use->AsMemberExpression()->PropVar()->HasFlag(varbinder::VariableFlags::DYNAMIC))) { + if (!type->IsETSMethodType()) { return type; } auto const getUseSite = [use]() { diff --git a/ets2panda/compiler/lowering/ets/dynamicImport.cpp b/ets2panda/compiler/lowering/ets/dynamicImport.cpp index 2801433e224..7701480b15a 100644 --- a/ets2panda/compiler/lowering/ets/dynamicImport.cpp +++ b/ets2panda/compiler/lowering/ets/dynamicImport.cpp @@ -223,6 +223,10 @@ static AstNodePtr TransformIdentifier(ir::Identifier *ident, public_lib::Context allocator, expr, newIdent, ir::MemberExpressionKind::PROPERTY_ACCESS, false, false); memberExpr->SetParent(parent); + // Ensure that it will not be incorrectly converted to ArrowType. + if (parent->IsCallExpression() && parent->AsCallExpression()->Callee() == ident) { + parent->AsCallExpression()->SetCallee(memberExpr); + } CheckLoweredNode(varBinder, checker, memberExpr); return memberExpr; diff --git a/ets2panda/compiler/lowering/ets/lambdaLowering.cpp b/ets2panda/compiler/lowering/ets/lambdaLowering.cpp index f621292487d..3533aac4ae8 100644 --- a/ets2panda/compiler/lowering/ets/lambdaLowering.cpp +++ b/ets2panda/compiler/lowering/ets/lambdaLowering.cpp @@ -1311,14 +1311,15 @@ static ir::AstNode *BuildLambdaClassWhenNeeded(public_lib::Context *ctx, ir::Ast // so it is correct to pass ETS extension here to isReference() if (id->IsReference(ScriptExtension::ETS) && id->TsType() != nullptr && id->TsType()->IsETSFunctionType() && !IsInCalleePosition(id) && !IsEnumFunctionCall(id) && IsValidFunctionDeclVar(var) && - !id->Variable()->HasFlag(varbinder::VariableFlags::DYNAMIC) && !IsOverloadedName(id)) { + !IsOverloadedName(id)) { return ConvertFunctionReference(ctx, id); } } if (node->IsMemberExpression()) { auto *mexpr = node->AsMemberExpression(); if (mexpr->Kind() == ir::MemberExpressionKind::PROPERTY_ACCESS && mexpr->TsType() != nullptr && - mexpr->TsType()->IsETSFunctionType() && mexpr->Object()->TsType()->IsETSObjectType()) { + mexpr->TsType()->IsETSFunctionType() && mexpr->Object()->TsType()->IsETSObjectType() && + mexpr->PropVar() != nullptr && !mexpr->PropVar()->HasFlag(varbinder::VariableFlags::DYNAMIC)) { ES2PANDA_ASSERT(mexpr->Property()->IsIdentifier()); auto *var = mexpr->Object()->TsType()->AsETSObjectType()->GetProperty( mexpr->Property()->AsIdentifier()->Name(), diff --git a/ets2panda/compiler/lowering/ets/restArgsLowering.cpp b/ets2panda/compiler/lowering/ets/restArgsLowering.cpp index 952da6e17e7..7251ef30842 100644 --- a/ets2panda/compiler/lowering/ets/restArgsLowering.cpp +++ b/ets2panda/compiler/lowering/ets/restArgsLowering.cpp @@ -203,9 +203,7 @@ ir::CallExpression *RestArgsLowering::TransformCallExpressionWithRestArgs(ir::Ca public_lib::Context *context) { checker::Type *calleeType = callExpr->Callee()->TsType(); - if (calleeType == nullptr || calleeType->IsETSArrowType() || - (callExpr->Callee()->IsMemberExpression() && callExpr->Callee()->AsMemberExpression()->PropVar() != nullptr && - callExpr->Callee()->AsMemberExpression()->PropVar()->HasFlag(varbinder::VariableFlags::DYNAMIC))) { + if (calleeType == nullptr || calleeType->IsETSArrowType() || callExpr->Signature()->Function()->IsDynamic()) { return callExpr; } diff --git a/ets2panda/test/ast/parser/ets/dynamic_import_tests/dynamic_function_use_as_value.ets b/ets2panda/test/ast/parser/ets/dynamic_import_tests/dynamic_function_use_as_value.ets new file mode 100644 index 00000000000..a82806dcf2a --- /dev/null +++ b/ets2panda/test/ast/parser/ets/dynamic_import_tests/dynamic_function_use_as_value.ets @@ -0,0 +1,24 @@ +/* + * 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. + */ + +/*--- +flags: [dynamic-ast] +---*/ + +import { foo, A } from "dynamic_import_tests/modules/module" + +function bar(f:(p:A)=>void){} + +bar(foo) // should not crash \ No newline at end of file -- Gitee