From 80f136118fd243942c8823692be956a2574d5d4c Mon Sep 17 00:00:00 2001 From: yp9522 Date: Thu, 31 Jul 2025 11:47:08 +0800 Subject: [PATCH] BugFix function name conflict with typealias Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICQ0UJ Signed-off-by: yp9522 --- ets2panda/checker/ETSchecker.h | 7 +++-- ets2panda/checker/ets/helpers.cpp | 20 ++++++------ .../ets/typealias_function_name_conflict.ets | 31 +++++++++++++++++++ .../test-lists/recheck/recheck-ignored.txt | 1 + 4 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 ets2panda/test/runtime/ets/typealias_function_name_conflict.ets diff --git a/ets2panda/checker/ETSchecker.h b/ets2panda/checker/ETSchecker.h index 0f21caa25b..47fc4abbb2 100644 --- a/ets2panda/checker/ETSchecker.h +++ b/ets2panda/checker/ETSchecker.h @@ -700,10 +700,13 @@ public: bool CompareIdentifiersValuesAreDifferent(ir::Expression *compareValue, const std::string &caseValue); void CheckIdentifierSwitchCase(ir::Expression *currentCase, ir::Expression *compareCase, const lexer::SourcePosition &pos); - varbinder::Variable *FindVariableInFunctionScope(util::StringView name); + varbinder::Variable *FindVariableInFunctionScope( + util::StringView name, const varbinder::ResolveBindingOptions options = varbinder::ResolveBindingOptions::ALL); std::pair FindVariableInClassOrEnclosing( util::StringView name, const ETSObjectType *classType); - varbinder::Variable *FindVariableInGlobal(const ir::Identifier *identifier); + varbinder::Variable *FindVariableInGlobal( + const ir::Identifier *identifier, + const varbinder::ResolveBindingOptions options = varbinder::ResolveBindingOptions::ALL); varbinder::Variable *ExtraCheckForResolvedError(ir::Identifier *ident); void ValidateResolvedIdentifier(ir::Identifier *ident); static bool IsVariableStatic(const varbinder::Variable *var); diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 45f42bbafe..9f7d42fb15 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -35,10 +35,10 @@ namespace ark::es2panda::checker { -varbinder::Variable *ETSChecker::FindVariableInFunctionScope(const util::StringView name) +varbinder::Variable *ETSChecker::FindVariableInFunctionScope(const util::StringView name, + const varbinder::ResolveBindingOptions options) { - return Scope() != nullptr ? Scope()->FindInFunctionScope(name, varbinder::ResolveBindingOptions::ALL).variable - : nullptr; + return Scope() != nullptr ? Scope()->FindInFunctionScope(name, options).variable : nullptr; } std::pair ETSChecker::FindVariableInClassOrEnclosing( @@ -60,11 +60,10 @@ std::pair ETSChecker::FindVariable return {resolved, classType}; } -varbinder::Variable *ETSChecker::FindVariableInGlobal(const ir::Identifier *const identifier) +varbinder::Variable *ETSChecker::FindVariableInGlobal(const ir::Identifier *const identifier, + const varbinder::ResolveBindingOptions options) { - return Scope() != nullptr - ? Scope()->FindInGlobal(identifier->Name(), varbinder::ResolveBindingOptions::ALL).variable - : nullptr; + return Scope() != nullptr ? Scope()->FindInGlobal(identifier->Name(), options).variable : nullptr; } bool ETSChecker::IsVariableStatic(const varbinder::Variable *var) @@ -299,11 +298,14 @@ Type *ETSChecker::ResolveIdentifier(ir::Identifier *ident) return GetTypeOfVariable(resolved); } - auto *resolved = FindVariableInFunctionScope(ident->Name()); + const auto options = varbinder::ResolveBindingOptions::ALL_VARIABLES | + varbinder::ResolveBindingOptions::ALL_METHOD | + varbinder::ResolveBindingOptions::ALL_DECLARATION; + auto *resolved = FindVariableInFunctionScope(ident->Name(), options); if (resolved == nullptr) { // If the reference is not found already in the current class, then it is not bound to the class, so we have to // find the reference in the global class first, then in the global scope - resolved = FindVariableInGlobal(ident); + resolved = FindVariableInGlobal(ident, options); if (UNLIKELY(resolved == nullptr && debugInfoPlugin_ != nullptr)) { // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) resolved = debugInfoPlugin_->FindIdentifier(ident); diff --git a/ets2panda/test/runtime/ets/typealias_function_name_conflict.ets b/ets2panda/test/runtime/ets/typealias_function_name_conflict.ets new file mode 100644 index 0000000000..51b024f890 --- /dev/null +++ b/ets2panda/test/runtime/ets/typealias_function_name_conflict.ets @@ -0,0 +1,31 @@ +/* + * 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. + */ + +type bar = () => number; + +function bar(): string { + return "This is the function's result"; +} + +function main(): int { + let result = bar(); + let expected = "This is the function's result"; + arktest.assertEQ(result.toString(), expected) + + let result2: bar = () => 123; + let expected2 = 123; + arktest.assertEQ(result2(), expected2) + return 0; +} diff --git a/ets2panda/test/test-lists/recheck/recheck-ignored.txt b/ets2panda/test/test-lists/recheck/recheck-ignored.txt index 59b03fc984..edafd06581 100644 --- a/ets2panda/test/test-lists/recheck/recheck-ignored.txt +++ b/ets2panda/test/test-lists/recheck/recheck-ignored.txt @@ -87,6 +87,7 @@ runtime/ets/lambda_with_receiver/lambda_with_receiver_trailing_in_function_with_ runtime/ets/lambda_with_receiver/lambda_with_receiver_trailing_name_duplicated.ets runtime/ets/generic_lambda_6.ets runtime/ets/implement_interface.ets +runtime/ets/typealias_function_name_conflict.ets #Test that failed before CheckerPhase (on ConstantExpressionLowering) ast/parser/ets/InvalidLexer.ets #Test that failed with abort before plugins-after-check phase -- Gitee