From a6f9fcf6336ae1344df245931c242c271085c37e Mon Sep 17 00:00:00 2001 From: zengzengran Date: Sat, 2 Aug 2025 22:57:32 +0800 Subject: [PATCH] Fix namespace same name builtin class crash Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICQMNY Description: For namespace, it is permissible to define classes and interfaces with the same names as built-in ones, and it should not return the built-in ETSObjectType. Tested-by: ninja tests (passed) ets_testrunner (passed) Signed-off-by: zengzengran # --- ets2panda/checker/ets/typeCreation.cpp | 11 +-- .../ast/compiler/ets/namespace_class_decl.ets | 74 ------------------- 2 files changed, 1 insertion(+), 84 deletions(-) delete mode 100644 ets2panda/test/ast/compiler/ets/namespace_class_decl.ets diff --git a/ets2panda/checker/ets/typeCreation.cpp b/ets2panda/checker/ets/typeCreation.cpp index 343f1d5486..0647747376 100644 --- a/ets2panda/checker/ets/typeCreation.cpp +++ b/ets2panda/checker/ets/typeCreation.cpp @@ -350,22 +350,13 @@ static ETSObjectType *InitializeGlobalBuiltinObjectType(ETSChecker *checker, Glo } } -static bool DeclarationInNameSpace(ETSChecker *checker) -{ - auto *conclass = checker->Context().ContainingClass(); - if (conclass == nullptr || conclass->GetDeclNode() == nullptr || !conclass->GetDeclNode()->IsClassDefinition()) { - return false; - } - return conclass->GetDeclNode()->AsClassDefinition()->IsNamespaceTransformed(); -} - ETSObjectType *ETSChecker::CreateETSObjectTypeOrBuiltin(ir::AstNode *declNode, ETSObjectFlags flags) { if (LIKELY(HasStatus(CheckerStatus::BUILTINS_INITIALIZED))) { return CreateETSObjectType(declNode, flags); } auto const globalId = GetGlobalTypesHolder()->NameToId(GetObjectTypeDeclNames(declNode).first); - if (!globalId.has_value() || DeclarationInNameSpace(this)) { + if (!globalId.has_value()) { return CreateETSObjectType(declNode, flags); } return InitializeGlobalBuiltinObjectType(this, globalId.value(), declNode, flags); diff --git a/ets2panda/test/ast/compiler/ets/namespace_class_decl.ets b/ets2panda/test/ast/compiler/ets/namespace_class_decl.ets deleted file mode 100644 index 2cc3edecb5..0000000000 --- a/ets2panda/test/ast/compiler/ets/namespace_class_decl.ets +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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. - */ - -namespace testing { - export class Array { - $_get(index: number): Array {return this;} - $_set(index: number, value: Array) {} - } - export class ReadonlyArray { - $_get(index: number): ReadonlyArray {return this;} - $_set(index: number, value: ReadonlyArray) {} - } - export class ConcatArray { - this.tabInfo.recentCount--; - -} - export class ArrayLike { - $_get(index: number): ArrayLike {return this;} - $_set(index: number, value: ArrayLike) {} - } -} - -namespace testing2 { - export class Array {[x: number]: any} - export class ReadonlyArray {[x: number]: any} - export class ConcatArray {[x: number]: any} - export class ArrayLike {[x: number]: any} -} - -function retA() { - let a: Array = [1,2,3]; - let b: ReadonlyArray = [1,2,3]; - let c: ConcatArray = [1,2,3]; - let d: ArrayLike = [1,2,3]; -} - -function retB() { - let a = new testing.Array(); - let b = new testing.ReadonlyArray(); - let c = new testing.ConcatArray(); - let d = new testing.ArrayLike(); -} - -function retC() { - let a = new testing2.Array(); - let b = new testing2.ReadonlyArray(); - let c = new testing2.ConcatArray(); - let d = new testing2.ArrayLike(); -} - -/* @@? 26:13 Error SyntaxError: Unexpected token 'this'. */ -/* @@? 26:17 Error SyntaxError: Unexpected token '.'. */ -/* @@? 26:25 Error SyntaxError: Field type annotation expected. */ -/* @@? 26:25 Error SyntaxError: Unexpected token '.'. */ -/* @@? 26:37 Error SyntaxError: Field type annotation expected. */ -/* @@? 26:37 Error SyntaxError: Unexpected token '--'. */ -/* @@? 36:29 Error TypeError: Indexed signatures are not allowed. Use arrays instead! */ -/* @@? 37:37 Error TypeError: Indexed signatures are not allowed. Use arrays instead! */ -/* @@? 38:35 Error TypeError: Indexed signatures are not allowed. Use arrays instead! */ -/* @@? 39:33 Error TypeError: Indexed signatures are not allowed. Use arrays instead! */ -/* @@? 45:34 Error TypeError: Type 'Array' cannot be assigned to type 'ConcatArray' */ -/* @@? 46:32 Error TypeError: Type 'Array' cannot be assigned to type 'ArrayLike' */ -- Gitee