From 6410d784deb4e7e016072ffcbfd8700ffbed54fb Mon Sep 17 00:00:00 2001 From: zengzengran Date: Fri, 1 Aug 2025 15:00:07 +0800 Subject: [PATCH] Fix namespace same name builtin class crash Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICQDFV 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, 84 insertions(+), 1 deletion(-) create 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 0647747376..343f1d5486 100644 --- a/ets2panda/checker/ets/typeCreation.cpp +++ b/ets2panda/checker/ets/typeCreation.cpp @@ -350,13 +350,22 @@ 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()) { + if (!globalId.has_value() || DeclarationInNameSpace(this)) { 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 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' */ -- Gitee