From 7000cb55afb634ba66369bf23c9565daeffe08f0 Mon Sep 17 00:00:00 2001 From: oh-rgx Date: Thu, 10 Jul 2025 20:26:06 +0800 Subject: [PATCH] Fix clone ETSimportDecl crash Issue: #ICLIPU Signed-off-by: oh-rgx --- ets2panda/parser/ETSparser.cpp | 4 +++- .../import_within_scope/scope_import_1.ets | 1 - .../test/ast/compiler/ets/parser_import.ets | 22 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/parser_import.ets diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index fdd530052b..732b3d7979 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -1850,8 +1850,10 @@ void ETSParser::ParseCatchParamTypeAnnotation([[maybe_unused]] ir::AnnotatedExpr ir::Statement *ETSParser::ParseImportDeclaration([[maybe_unused]] StatementParsingFlags flags) { + bool isError = false; if ((flags & StatementParsingFlags::GLOBAL) == 0) { LogError(diagnostic::IMPORT_TOP_LEVEL); + isError = true; } char32_t nextChar = Lexer()->Lookahead(); @@ -1878,7 +1880,7 @@ ir::Statement *ETSParser::ParseImportDeclaration([[maybe_unused]] StatementParsi importDeclaration = ParseImportPathBuildImport(std::move(specifiers), false, startLoc, ir::ImportKinds::ALL); } - return importDeclaration; + return isError ? AllocBrokenStatement(startLoc) : importDeclaration; } ir::Statement *ETSParser::ParseExportDeclaration([[maybe_unused]] StatementParsingFlags flags) diff --git a/ets2panda/test/ast/compiler/ets/import_tests/import_within_scope/scope_import_1.ets b/ets2panda/test/ast/compiler/ets/import_tests/import_within_scope/scope_import_1.ets index f6843e6780..e7628a6a70 100644 --- a/ets2panda/test/ast/compiler/ets/import_tests/import_within_scope/scope_import_1.ets +++ b/ets2panda/test/ast/compiler/ets/import_tests/import_within_scope/scope_import_1.ets @@ -18,4 +18,3 @@ namespace ns { } /* @@? 17:5 Error SyntaxError: Import declarations can only be used on the top level and before any other declaration, top level statement or directive. */ -/* @@? 17:12 Error TypeError: Variable 'nsi' has already been declared. */ diff --git a/ets2panda/test/ast/compiler/ets/parser_import.ets b/ets2panda/test/ast/compiler/ets/parser_import.ets new file mode 100644 index 0000000000..ae31ad17bb --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/parser_import.ets @@ -0,0 +1,22 @@ +/* + * 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. + */ + +let callbackc = (arrElem: JSValue) => { + import {a} from +} + +/* @@? 17:5 Error SyntaxError: Import declarations can only be used on the top level and before any other declaration, top level statement or directive. */ +/* @@? 18:1 Error SyntaxError: Unexpected token, expected string literal. */ +/* @@? 23:1 Error SyntaxError: Expected '}', got 'end of stream'. */ -- Gitee