From 6082996845ac983aece609ffcc5239b6dc0c39da Mon Sep 17 00:00:00 2001 From: zengzengran Date: Tue, 5 Aug 2025 15:32:30 +0800 Subject: [PATCH] Fix soft keywords cannot used as namespace alias Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICR8ZA Description: Allow soft keywords to be used as overload alias name. Tested-by: ninja tests (passed) ets_testrunner (passed) Signed-off-by: zengzengran # --- ets2panda/parser/ETSparser.cpp | 2 +- ets2panda/parser/ETSparserClasses.cpp | 4 +- .../runtime/ets/first_match/soft_keyword.ets | 40 +++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 ets2panda/test/runtime/ets/first_match/soft_keyword.ets diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index eb1fabd6b9..2cba5e2793 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -2299,7 +2299,7 @@ ir::OverloadDeclaration *ETSParser::ParseOverloadDeclaration(ir::ModifierFlags m { ValidateOverloadDeclarationModifiers(modifiers); Lexer()->TryEatTokenKeyword(lexer::TokenType::KEYW_OVERLOAD); - auto *overloadName = ExpectIdentifier(false, true, TypeAnnotationParsingOptions::REPORT_ERROR); + auto *overloadName = ExpectIdentifier(false, false, TypeAnnotationParsingOptions::REPORT_ERROR); auto *overloadDef = AllocNode(overloadName->Clone(Allocator(), nullptr)->AsExpression(), modifiers, Allocator()); overloadDef->AddOverloadDeclFlag(ir::OverloadDeclFlags::FUNCTION); diff --git a/ets2panda/parser/ETSparserClasses.cpp b/ets2panda/parser/ETSparserClasses.cpp index 3900e08970..7a1dfe9f0e 100644 --- a/ets2panda/parser/ETSparserClasses.cpp +++ b/ets2panda/parser/ETSparserClasses.cpp @@ -549,7 +549,7 @@ ir::OverloadDeclaration *ETSParser::ParseClassOverloadDeclaration(ir::ModifierFl modifiers |= ir::ModifierFlags::CONSTRUCTOR; Lexer()->NextToken(); } else { - overloadName = ExpectIdentifier(false, true, TypeAnnotationParsingOptions::REPORT_ERROR); + overloadName = ExpectIdentifier(false, false, TypeAnnotationParsingOptions::REPORT_ERROR); } auto *overloadDef = AllocNode(overloadName->Clone(Allocator(), nullptr)->AsExpression(), @@ -1108,7 +1108,7 @@ static lexer::SourcePosition GetEndLoc(ir::BlockStatement *body, ir::ScriptFunct ir::OverloadDeclaration *ETSParser::ParseInterfaceOverload(ir::ModifierFlags modifiers) { ValidateOverloadDeclarationModifiers(modifiers); - auto *overloadName = ExpectIdentifier(false, true, TypeAnnotationParsingOptions::REPORT_ERROR); + auto *overloadName = ExpectIdentifier(false, false, TypeAnnotationParsingOptions::REPORT_ERROR); auto *overloadDef = AllocNode(overloadName->Clone(Allocator(), nullptr)->AsExpression(), modifiers, Allocator()); overloadDef->AddOverloadDeclFlag(ir::OverloadDeclFlags::INTERFACE_METHOD); diff --git a/ets2panda/test/runtime/ets/first_match/soft_keyword.ets b/ets2panda/test/runtime/ets/first_match/soft_keyword.ets new file mode 100644 index 0000000000..933459d3d8 --- /dev/null +++ b/ets2panda/test/runtime/ets/first_match/soft_keyword.ets @@ -0,0 +1,40 @@ +/* + * 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 type1(a: number) { return "invoke1" } +function type2(a: string) { return "invoke2" } +overload type{ type1, type2 } + +class A { + type1(a: number) { return "invoke1" } + type2(a: string) { return "invoke2" } + overload type{ type1, type2 } +} + +interface I { + type1(a: number): string + type2(a: string): string + overload type{ type1, type2 } +} + +function main(): void { + arktest.assertEQ(type(123), "invoke1") + arktest.assertEQ(type("abc"), "invoke2") + + let a = new A(); + arktest.assertEQ(a.type(123), "invoke1") + arktest.assertEQ(a.type("abc"), "invoke2") +} -- Gitee