From bec18dfbf4ff0a9c0b4347a7f94981ed97795430 Mon Sep 17 00:00:00 2001 From: oh-rgx Date: Mon, 14 Jul 2025 21:40:22 +0800 Subject: [PATCH] Fix clone ETSimportDecl crash Issue: #ICLIN0 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 5541ac57d5..afae2c3d40 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -1888,8 +1888,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(); @@ -1916,7 +1918,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..3fb9464550 --- /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. */ +/* @@? 22:70 Error SyntaxError: Expected '}', got 'end of stream'. */ \ No newline at end of file -- Gitee