From 4552886d4253c00e91ca8b5130ee21945a765989 Mon Sep 17 00:00:00 2001 From: xuxinjie4 Date: Wed, 13 Aug 2025 15:40:42 +0800 Subject: [PATCH] Fix self import bug Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICSRG1?from=project-issue Signed-off-by: xuxinjie4 --- ets2panda/checker/types/ets/etsObjectType.cpp | 9 ++----- ets2panda/ir/ets/etsTypeReferencePart.cpp | 5 ++++ .../ets/circular_inheritance_parameter.ets | 2 +- .../ets/import_tests/import_self/export.ets | 24 ++++++++++++++++++ .../import_tests/import_self/import_self.ets | 25 +++++++++++++++++++ .../ets/import_tests/import_self/test.ets | 20 +++++++++++++++ 6 files changed, 77 insertions(+), 8 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/import_tests/import_self/export.ets create mode 100644 ets2panda/test/ast/compiler/ets/import_tests/import_self/import_self.ets create mode 100644 ets2panda/test/ast/compiler/ets/import_tests/import_self/test.ets diff --git a/ets2panda/checker/types/ets/etsObjectType.cpp b/ets2panda/checker/types/ets/etsObjectType.cpp index 4bf8a2f7c1..3652998750 100644 --- a/ets2panda/checker/types/ets/etsObjectType.cpp +++ b/ets2panda/checker/types/ets/etsObjectType.cpp @@ -1436,7 +1436,7 @@ ETSChecker *ETSObjectType::GetETSChecker() void ETSObjectType::CheckAndInstantiateProperties() const { - auto *checker = relation_->GetChecker()->AsETSChecker(); + [[maybe_unused]] auto *checker = relation_->GetChecker()->AsETSChecker(); auto *declNode = GetDeclNode(); if (HasObjectFlag(ETSObjectFlags::BUILTIN_TYPE) && declNode == nullptr) { declNode = SuperType()->GetDeclNode(); @@ -1445,11 +1445,6 @@ void ETSObjectType::CheckAndInstantiateProperties() const ES2PANDA_ASSERT(checker->IsAnyError()); return; } - - TypeStackElement tse {checker, this, {{diagnostic::CYCLIC_INHERITANCE, {this->Name()}}}, declNode->Start()}; - if (tse.HasTypeError()) { - return; - } InstantiateProperties(); } @@ -1463,7 +1458,7 @@ void ETSObjectType::InstantiateProperties() const return; } - ES2PANDA_ASSERT(!propertiesInstantiated_); + ES2PANDA_ASSERT(!propertiesInstantiated_ && declNode_ != nullptr); declNode_->Check(checker); auto subst = effectiveSubstitution_ == nullptr diff --git a/ets2panda/ir/ets/etsTypeReferencePart.cpp b/ets2panda/ir/ets/etsTypeReferencePart.cpp index 2491ba33fd..909e33fa5c 100644 --- a/ets2panda/ir/ets/etsTypeReferencePart.cpp +++ b/ets2panda/ir/ets/etsTypeReferencePart.cpp @@ -208,6 +208,11 @@ checker::Type *ETSTypeReferencePart::HandleInternalTypes(checker::ETSChecker *co if (name->IsIdentifier()) { variable = ident->Variable(); } else { + checker::TypeStackElement tse { + checker, this, {{diagnostic::CIRCULAR_DEPENDENCY, {GetIdent()->Name()}}}, Start()}; + if (tse.HasTypeError()) { + return checker->GlobalTypeError(); + } if (name->AsTSQualifiedName()->Left()->Variable() != nullptr && name->AsTSQualifiedName()->Left()->Variable()->TsType() != nullptr && name->AsTSQualifiedName()->Left()->Variable()->TsType()->IsETSObjectType()) { diff --git a/ets2panda/test/ast/compiler/ets/circular_inheritance_parameter.ets b/ets2panda/test/ast/compiler/ets/circular_inheritance_parameter.ets index 467fc6d5c7..cb99d022d2 100644 --- a/ets2panda/test/ast/compiler/ets/circular_inheritance_parameter.ets +++ b/ets2panda/test/ast/compiler/ets/circular_inheritance_parameter.ets @@ -17,5 +17,5 @@ class D{ f7(x: string, y: D.f7){} } -/* @@? 16:8 Error TypeError: Cyclic inheritance involving D. */ +/* @@? 17:20 Error TypeError: Circular dependency detected for identifier: f7 */ /* @@? 17:22 Error TypeError: 'f7' type does not exist. */ diff --git a/ets2panda/test/ast/compiler/ets/import_tests/import_self/export.ets b/ets2panda/test/ast/compiler/ets/import_tests/import_self/export.ets new file mode 100644 index 0000000000..5ef41650a3 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/import_tests/import_self/export.ets @@ -0,0 +1,24 @@ +/* + * 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 {C, I} from "./import_self" + +export function foo(){} + +export class A implements I { + onTest(p: C){ + console.log(p.name) + } +} \ No newline at end of file diff --git a/ets2panda/test/ast/compiler/ets/import_tests/import_self/import_self.ets b/ets2panda/test/ast/compiler/ets/import_tests/import_self/import_self.ets new file mode 100644 index 0000000000..b205b73bec --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/import_tests/import_self/import_self.ets @@ -0,0 +1,25 @@ +/* + * 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 * as File from "./import_self" + +export class C { + name: string = "123" + constructor() { } +} + +export interface I { + onTest(p: File.C) +} \ No newline at end of file diff --git a/ets2panda/test/ast/compiler/ets/import_tests/import_self/test.ets b/ets2panda/test/ast/compiler/ets/import_tests/import_self/test.ets new file mode 100644 index 0000000000..11da459b9b --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/import_tests/import_self/test.ets @@ -0,0 +1,20 @@ +/* + * 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 {foo, A} from "./export" + +function main(){ + new A().onTest({name: "12345"}) +} \ No newline at end of file -- Gitee