From 59fa41601e24366d60d6239d61f122f398eabf6f Mon Sep 17 00:00:00 2001 From: zmw Date: Thu, 24 Jul 2025 17:24:38 +0800 Subject: [PATCH] Fix invalid this in namspace crash Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICOL4Z Description: Fix invalid this in namspace crash Signed-off-by: zmw Change-Id: I3a7f7a30a92cf8ea96c01e604e79226d73b91cb7 --- ets2panda/checker/ets/object.cpp | 4 +++- .../ets/invalid_this_in_namespace.ets | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/compiler/ets/invalid_this_in_namespace.ets diff --git a/ets2panda/checker/ets/object.cpp b/ets2panda/checker/ets/object.cpp index a0628a3398..c792501b30 100644 --- a/ets2panda/checker/ets/object.cpp +++ b/ets2panda/checker/ets/object.cpp @@ -1809,7 +1809,9 @@ ETSObjectType *ETSChecker::CheckThisOrSuperAccess(ir::Expression *node, ETSObjec return classType; } - if (classType->GetDeclNode()->IsClassDefinition() && classType->GetDeclNode()->AsClassDefinition()->IsGlobal()) { + if (classType->GetDeclNode()->IsClassDefinition() && + (classType->GetDeclNode()->AsClassDefinition()->IsGlobal() || + classType->GetDeclNode()->AsClassDefinition()->IsNamespaceTransformed())) { LogError(diagnostic::CTOR_REF_INVALID_CTX_GLOBAL, {msg}, node->Start()); return GlobalBuiltinErrorType(); } diff --git a/ets2panda/test/ast/compiler/ets/invalid_this_in_namespace.ets b/ets2panda/test/ast/compiler/ets/invalid_this_in_namespace.ets new file mode 100644 index 0000000000..8145aabd6b --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/invalid_this_in_namespace.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. + */ + +namespace X { + this.toString() +} + +/* @@? 17:5 Error TypeError: Cannot reference 'this' in this context. */ -- Gitee