diff --git a/ets2panda/checker/ETSchecker.h b/ets2panda/checker/ETSchecker.h index 9494c61473b709f37495366474d87abba159ed63..ad880280ae0c4b304992e79cdc3bea9a312e2420 100644 --- a/ets2panda/checker/ETSchecker.h +++ b/ets2panda/checker/ETSchecker.h @@ -689,10 +689,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 6fe0fe66c0f3958b5b18ff82db4cd0ad72026a81..9661099a4790f009e1b7b97a9b7465a44a1b6ce1 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) @@ -295,11 +294,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 0000000000000000000000000000000000000000..51b024f8904e78739ee0a8c5880bc08d4c436830 --- /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 59b03fc9848d1e8515413f49c97b930d037463e0..edafd06581ad1d5e6eb2857f5d549adc9094684e 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