From 4cb870d78893b599a5fe5fd3f28a5cb7a340a410 Mon Sep 17 00:00:00 2001 From: Boglarka Haag Date: Thu, 26 Jun 2025 13:50:10 +0200 Subject: [PATCH] CTE if more indexer declaration is provided Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICHJ5O Reason: Crash if more than one index signatures were declared Description: Added extra checks to parsing index signatures to throw CTE in time. Fixes internal issue: #26925 Signed-off-by: Haag Boglarka --- ets2panda/parser/parserImpl.cpp | 11 ++++++++ .../ast/parser/ets/ambient_indexer_10.ets | 26 +++++++++++++++++++ ets2panda/util/diagnostic/syntax.yaml | 4 +++ 3 files changed, 41 insertions(+) create mode 100644 ets2panda/test/ast/parser/ets/ambient_indexer_10.ets diff --git a/ets2panda/parser/parserImpl.cpp b/ets2panda/parser/parserImpl.cpp index 4f66296e62..0905295535 100644 --- a/ets2panda/parser/parserImpl.cpp +++ b/ets2panda/parser/parserImpl.cpp @@ -14,6 +14,7 @@ */ #include "parserImpl.h" +#include "forwardDeclForParserImpl.h" #include "parserStatusContext.h" #include "generated/diagnostic.h" @@ -28,6 +29,7 @@ #include "ir/expressions/arrayExpression.h" #include "ir/expressions/assignmentExpression.h" #include "ir/expressions/callExpression.h" +#include "ir/expressions/dummyNode.h" #include "ir/expressions/functionExpression.h" #include "ir/expressions/literals/bigIntLiteral.h" #include "ir/expressions/literals/numberLiteral.h" @@ -837,6 +839,15 @@ ParserImpl::ClassBody ParserImpl::ParseClassBody(ir::ClassDefinitionModifiers mo if (CheckClassElement(property, ctor, properties)) { continue; } + auto isIndexer = [](ir::AstNode *node) { + return node->IsDummyNode() && node->AsDummyNode()->IsDeclareIndexer(); + }; + if (std::any_of(properties.begin(), properties.end(), + [&isIndexer](ir::AstNode *node) { return isIndexer(node); }) && + isIndexer(property)) { + LogError(diagnostic::MORE_INDEXER, {}, property->Start()); + continue; + } properties.push_back(property); } diff --git a/ets2panda/test/ast/parser/ets/ambient_indexer_10.ets b/ets2panda/test/ast/parser/ets/ambient_indexer_10.ets new file mode 100644 index 0000000000..89543725ec --- /dev/null +++ b/ets2panda/test/ast/parser/ets/ambient_indexer_10.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. + */ + +declare class ClassWithSymbols { + public readonly [x: number]: string; + public /* @@ label1 */[x: number]: /* @@ label2 */(/* @@ label3 */) /* @@ label4 */=> /* @@ label5 */void; +} + + +/* @@@ label1 Error SyntaxError: Only one index signature can exist in a class */ +/* @@@ label2 Error SyntaxError: Return type of index signature from exported class or interface need to be identifier. */ +/* @@@ label3 Error SyntaxError: Unexpected token ')'. */ +/* @@@ label4 Error SyntaxError: Unexpected token '=>'. */ +/* @@@ label5 Error SyntaxError: Unexpected token 'void'. */ diff --git a/ets2panda/util/diagnostic/syntax.yaml b/ets2panda/util/diagnostic/syntax.yaml index 43a5a991a0..400f8165a7 100644 --- a/ets2panda/util/diagnostic/syntax.yaml +++ b/ets2panda/util/diagnostic/syntax.yaml @@ -1295,3 +1295,7 @@ syntax: - name: INIT_MODULE_DECLARATION_POSITION id: 322 message: "initModule() must only be called immediately after the import statement, and before any other declarations or statements." + +- name: MORE_INDEXER + id: 323 + message: "Only one index signature can exist in a class" \ No newline at end of file -- Gitee