From 20f61a680065531549b5b52ab6325c74f3178e71 Mon Sep 17 00:00:00 2001 From: zengzengran Date: Mon, 4 Aug 2025 11:20:42 +0800 Subject: [PATCH] Fix namespace same name builtin class crash Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICQTLF 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 | 7 +- ets2panda/compiler/scripts/signatures.yaml | 2 + .../ast/compiler/ets/namespace_class_decl.ets | 74 +++++++++++++++++++ .../test/runtime/ets/namespace_class_decl.ets | 34 +++++++++ 4 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/compiler/ets/namespace_class_decl.ets create mode 100644 ets2panda/test/runtime/ets/namespace_class_decl.ets diff --git a/ets2panda/checker/ets/typeCreation.cpp b/ets2panda/checker/ets/typeCreation.cpp index 0647747376..c6396ad643 100644 --- a/ets2panda/checker/ets/typeCreation.cpp +++ b/ets2panda/checker/ets/typeCreation.cpp @@ -319,7 +319,7 @@ static ETSObjectType *InitializeGlobalBuiltinObjectType(ETSChecker *checker, Glo return bigIntObj; } case GlobalTypeId::ETS_ARRAY_BUILTIN: { - if (declNode->AsClassDefinition()->InternalName().Utf8() != "escompat.Array") { + if (declNode->AsClassDefinition()->InternalName().Utf8() != compiler::Signatures::ESCOMPAT_ARRAY) { return checker->CreateETSObjectType(declNode, flags); } auto *arrayObj = @@ -328,6 +328,10 @@ static ETSObjectType *InitializeGlobalBuiltinObjectType(ETSChecker *checker, Glo return arrayObj; } case GlobalTypeId::ETS_READONLY_ARRAY: + if (declNode->IsClassDefinition() || declNode->AsTSInterfaceDeclaration()->InternalName().Utf8() != + compiler::Signatures::ESCOMPAT_READONLYARRAY) { + return checker->CreateETSObjectType(declNode, flags); + } return setType(globalId, create(ETSObjectFlags::BUILTIN_READONLY_ARRAY))->AsETSObjectType(); case GlobalTypeId::ETS_BOOLEAN_BUILTIN: return create(ETSObjectFlags::BUILTIN_BOOLEAN); @@ -355,6 +359,7 @@ ETSObjectType *ETSChecker::CreateETSObjectTypeOrBuiltin(ir::AstNode *declNode, E if (LIKELY(HasStatus(CheckerStatus::BUILTINS_INITIALIZED))) { return CreateETSObjectType(declNode, flags); } + // Note (zengran): should be determined whether is a builtin type through InternalName instead of DeclName. auto const globalId = GetGlobalTypesHolder()->NameToId(GetObjectTypeDeclNames(declNode).first); if (!globalId.has_value()) { return CreateETSObjectType(declNode, flags); diff --git a/ets2panda/compiler/scripts/signatures.yaml b/ets2panda/compiler/scripts/signatures.yaml index 3a97066b38..3265edf49b 100644 --- a/ets2panda/compiler/scripts/signatures.yaml +++ b/ets2panda/compiler/scripts/signatures.yaml @@ -184,6 +184,8 @@ defines: ref: SYMBOL - name: 'escompat.Array' ref: ESCOMPAT_ARRAY + - name: 'escompat.ReadonlyArray' + ref: ESCOMPAT_READONLYARRAY - name: 'ByteType.VAL' ref: BYTETYPE_VAL - name: 'IntType.VAL' diff --git a/ets2panda/test/ast/compiler/ets/namespace_class_decl.ets b/ets2panda/test/ast/compiler/ets/namespace_class_decl.ets new file mode 100644 index 0000000000..2cc3edecb5 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/namespace_class_decl.ets @@ -0,0 +1,74 @@ +/* + * 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' */ diff --git a/ets2panda/test/runtime/ets/namespace_class_decl.ets b/ets2panda/test/runtime/ets/namespace_class_decl.ets new file mode 100644 index 0000000000..9b04937170 --- /dev/null +++ b/ets2panda/test/runtime/ets/namespace_class_decl.ets @@ -0,0 +1,34 @@ +/* + * 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 NS { + export interface ReadonlyArray { + index:number; + } +} + +namespace NS2 { + export class ReadonlyArray { + index:number = 10; + } +} + +function main(){ + let x1:NS.ReadonlyArray = {index:10}; + arktest.assertEQ(x1.index,10); + + let x2 = new NS2.ReadonlyArray(); + arktest.assertEQ(x2.index,10) +} -- Gitee