diff --git a/ets2panda/checker/ets/object.cpp b/ets2panda/checker/ets/object.cpp index 6bd7718a43e5df73b400ebfeb26c130a764dc256..3ac6f832f9c29d1fa4557f4d0351c969a3b22fce 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 0000000000000000000000000000000000000000..c1d9cb8eaf92d6d67cb3547ac8911f6a35e04c43 --- /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' */