From 557e5c80a225da6efa08fba46a515c1075cb5973 Mon Sep 17 00:00:00 2001 From: zmw Date: Thu, 31 Jul 2025 21:55:02 +0800 Subject: [PATCH] Fix this in wrong contxt crash Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICQ7TD Description: Fix this in wrong contxt crash Signed-off-by: zmw Change-Id: Ifa8b9ac570fe97fb7e2b196ee7282119b4642eb6 --- ets2panda/checker/ets/object.cpp | 3 ++- .../compiler/ets/this_in_wrong_context.ets | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/compiler/ets/this_in_wrong_context.ets diff --git a/ets2panda/checker/ets/object.cpp b/ets2panda/checker/ets/object.cpp index 6bd7718a43..3ac6f832f9 100644 --- a/ets2panda/checker/ets/object.cpp +++ b/ets2panda/checker/ets/object.cpp @@ -1815,7 +1815,8 @@ ETSObjectType *ETSChecker::CheckThisOrSuperAccess(ir::Expression *node, ETSObjec return classType; } - if (classType->GetDeclNode()->IsClassDefinition() && classType->GetDeclNode()->AsClassDefinition()->IsGlobal()) { + if (classType == nullptr || + (classType->GetDeclNode()->IsClassDefinition() && classType->GetDeclNode()->AsClassDefinition()->IsGlobal())) { LogError(diagnostic::CTOR_REF_INVALID_CTX_GLOBAL, {msg}, node->Start()); return GlobalBuiltinErrorType(); } diff --git a/ets2panda/test/ast/compiler/ets/this_in_wrong_context.ets b/ets2panda/test/ast/compiler/ets/this_in_wrong_context.ets new file mode 100644 index 0000000000..c1d9cb8eaf --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/this_in_wrong_context.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. + */ + +export declare @interface ClassAuthor { + color: Color[] = [this.listener !== undefined] +} + +/* @@? 1:3 Error TypeError: Cannot find type 'Color'. */ +/* @@? 17:5 Error TypeError: Invalid annotation field type. Only numeric, boolean, string, enum, or arrays of these types are permitted for annotation fields. */ +/* @@? 17:22 Error TypeError: Invalid value for annotation field, expected a constant literal. */ +/* @@? 17:23 Error TypeError: Cannot reference 'this' in this context. */ +/* @@? 17:28 Error TypeError: Property 'listener' does not exist on type 'Error' */ -- Gitee