From 0d543b40ce6eee87d7dd12fa279b87ff626eedbe Mon Sep 17 00:00:00 2001 From: yp9522 Date: Wed, 6 Aug 2025 18:13:02 +0800 Subject: [PATCH] Import type Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICVKC8 Signed-off-by: yp9522 --- .../ast/compiler/ets/import_export/file1.ets | 19 +++++++++++++++++ .../ast/compiler/ets/import_export/file2.ets | 21 +++++++++++++++++++ .../ets/import_type_without_export.ets | 2 ++ .../test/ast/compiler/ets/type_binding_01.ets | 2 ++ .../test/ast/compiler/ets/type_binding_02.ets | 2 ++ ets2panda/varbinder/ETSBinder.cpp | 8 +++++-- ets2panda/varbinder/ETSBinder.h | 2 +- 7 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/import_export/file1.ets create mode 100644 ets2panda/test/ast/compiler/ets/import_export/file2.ets diff --git a/ets2panda/test/ast/compiler/ets/import_export/file1.ets b/ets2panda/test/ast/compiler/ets/import_export/file1.ets new file mode 100644 index 0000000000..d12297a35d --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/import_export/file1.ets @@ -0,0 +1,19 @@ +/* + * 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. + */ + + +export default class A {} + +export class B {} \ No newline at end of file diff --git a/ets2panda/test/ast/compiler/ets/import_export/file2.ets b/ets2panda/test/ast/compiler/ets/import_export/file2.ets new file mode 100644 index 0000000000..3819082f2d --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/import_export/file2.ets @@ -0,0 +1,21 @@ +/* + * 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. + */ + +import type { B } from './file1'; +import type { A } from './file1'; + +/* @@? 16:24 Error TypeError: Imported element not exported 'B' */ +/* @@? 17:24 Error TypeError: Imported element not exported 'A' */ +/* @@? 17:24 Error TypeError: Use the default import syntax to import a default exported element */ \ No newline at end of file diff --git a/ets2panda/test/ast/compiler/ets/import_type_without_export.ets b/ets2panda/test/ast/compiler/ets/import_type_without_export.ets index c831615f2a..883a47d7b1 100644 --- a/ets2panda/test/ast/compiler/ets/import_type_without_export.ets +++ b/ets2panda/test/ast/compiler/ets/import_type_without_export.ets @@ -17,3 +17,5 @@ import type {A, C} from /* @@ label */'./export_type.ets' let a = new A(); let b = new C(); + +/* @@? 16:39 Error TypeError: Imported element not exported 'C' */ \ No newline at end of file diff --git a/ets2panda/test/ast/compiler/ets/type_binding_01.ets b/ets2panda/test/ast/compiler/ets/type_binding_01.ets index 009b7a28b2..e09409d73d 100644 --- a/ets2panda/test/ast/compiler/ets/type_binding_01.ets +++ b/ets2panda/test/ast/compiler/ets/type_binding_01.ets @@ -19,3 +19,5 @@ import type {StandardExportedClass as IT1, ClassForExport as IT2} from /* @@ lab function main () { let c1 = new IT1 // compile-time error: cannot create objects of type IT1 } + +/* @@? 17:86 Error TypeError: Imported element not exported 'StandardExportedClass' */ \ No newline at end of file diff --git a/ets2panda/test/ast/compiler/ets/type_binding_02.ets b/ets2panda/test/ast/compiler/ets/type_binding_02.ets index 417f0159e1..db6fdb2c1e 100644 --- a/ets2panda/test/ast/compiler/ets/type_binding_02.ets +++ b/ets2panda/test/ast/compiler/ets/type_binding_02.ets @@ -19,3 +19,5 @@ import type {StandardExportedClass as IT1, ClassForExport as IT2} from /* @@ lab function main () { let c2 = new IT2 // compile-time error: cannot create objects of type IT2 } + +/* @@? 17:86 Error TypeError: Imported element not exported 'StandardExportedClass' */ \ No newline at end of file diff --git a/ets2panda/varbinder/ETSBinder.cpp b/ets2panda/varbinder/ETSBinder.cpp index 878296e704..686158d14d 100644 --- a/ets2panda/varbinder/ETSBinder.cpp +++ b/ets2panda/varbinder/ETSBinder.cpp @@ -928,8 +928,12 @@ std::pair ETSBinder::FindImportDeclIn } void ETSBinder::ValidateImportVariable(const ir::AstNode *node, const ir::AstNode *declNode, - const util::StringView &imported, const ir::StringLiteral *const importPath) + const util::StringView &imported, const ir::ETSImportDeclaration *const import) { + const ir::StringLiteral *const importPath = import->Source(); + if (import->IsTypeKind() && !node->IsExportedType()) { + ThrowError(importPath->Start(), diagnostic::IMPORTED_NOT_EXPORTED, {imported}); + } if (node->IsDefaultExported() && !declNode->IsExported()) { ThrowError(importPath->Start(), diagnostic::DEFAULT_EXPORT_DIRECT_IMPORTED); } else if (!node->IsExported() && !node->IsDefaultExported() && !node->HasExportAlias()) { @@ -1037,7 +1041,7 @@ bool ETSBinder::AddImportSpecifiersToTopBindings(Span re } ValidateImportVariable(exportNode != nullptr ? exportNode : var->Declaration()->Node(), var->Declaration()->Node(), - imported, importPath); + imported, import); const auto localName = importSpecifier->Local()->Name(); auto varInGlobalClassScope = Program()->GlobalClassScope()->FindLocal(localName, ResolveBindingOptions::ALL); diff --git a/ets2panda/varbinder/ETSBinder.h b/ets2panda/varbinder/ETSBinder.h index 025f4c30d9..60103c0404 100644 --- a/ets2panda/varbinder/ETSBinder.h +++ b/ets2panda/varbinder/ETSBinder.h @@ -176,7 +176,7 @@ public: ir::ImportDefaultSpecifier *importDefaultSpecifier, const ir::ETSImportDeclaration *import); void ValidateImportVariable(const ir::AstNode *node, const ir::AstNode *declNode, const util::StringView &imported, - const ir::StringLiteral *const importPath); + const ir::ETSImportDeclaration *const import); Variable *FindImportSpecifiersVariable(const util::StringView &imported, const varbinder::Scope::VariableMap &globalBindings, Span record); -- Gitee