From a97c648f5f93485dbb4f2930824726fdccab2fa3 Mon Sep 17 00:00:00 2001 From: yp9522 Date: Fri, 1 Aug 2025 15:43:46 +0800 Subject: [PATCH] Import type Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICJBRV 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 +++++-- 6 files changed, 52 insertions(+), 2 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..231e63567f --- /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: Cannot import 'B', imported type imports only exported types. */ +/* @@? 17:24 Error TypeError: Cannot import 'A', imported type imports only exported types. */ +/* @@? 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..94dbbcb45d 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: Cannot import 'C', imported type imports only exported types. */ \ 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..68b0958822 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: Cannot import 'StandardExportedClass', imported type imports only exported types. */ \ 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..98dc3fb3d8 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: Cannot import 'StandardExportedClass', imported type imports only exported types. */ \ No newline at end of file diff --git a/ets2panda/varbinder/ETSBinder.cpp b/ets2panda/varbinder/ETSBinder.cpp index fceb39a277..a30898e9af 100644 --- a/ets2panda/varbinder/ETSBinder.cpp +++ b/ets2panda/varbinder/ETSBinder.cpp @@ -1011,8 +1011,12 @@ bool ETSBinder::AddImportSpecifiersToTopBindings(Span re auto *node = FindNodeInAliasMap(import->ResolvedSource(), imported); - ValidateImportVariable(node != nullptr ? node : var->Declaration()->Node(), var->Declaration()->Node(), imported, - importPath); + auto *targetNode = node != nullptr ? node : var->Declaration()->Node(); + if (import->IsTypeKind() && !targetNode->IsExportedType()) { + ThrowError(importPath->Start(), diagnostic::IMPORTING_NONEXPORTED_TYPE, {imported}); + } + + ValidateImportVariable(targetNode, var->Declaration()->Node(), imported, importPath); const auto localName = importSpecifier->Local()->Name(); auto varInGlobalClassScope = Program()->GlobalClassScope()->FindLocal(localName, ResolveBindingOptions::ALL); -- Gitee