From edc7e2d47cc7959e456e713e09f23267471feea7 Mon Sep 17 00:00:00 2001 From: XuMoheng Date: Thu, 4 Sep 2025 17:14:25 +0800 Subject: [PATCH] fix crash when apply an annotation Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICOGQ5 Description: mexpr->Object()->TsType() may be nullptr, this commit add some special check for nullptr to avoid crash. Tests: tests/tests-u-runner/runner.sh --astchecker --no-js --show-progress --build-dir out --processes=all Signed-off-by: xumoheng --- ets2panda/ast_verifier/helpers.cpp | 3 +- .../compiler/lowering/ets/unboxLowering.cpp | 5 ++- .../ets/declaration_default_values.ets | 38 +++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/declaration_default_values.ets diff --git a/ets2panda/ast_verifier/helpers.cpp b/ets2panda/ast_verifier/helpers.cpp index a2179fc411..ba2f9d53c4 100644 --- a/ets2panda/ast_verifier/helpers.cpp +++ b/ets2panda/ast_verifier/helpers.cpp @@ -268,7 +268,8 @@ bool ValidateVariableAccess(const varbinder::LocalVariable *propVar, const ir::M // NOTE: need to refactor: type of member expression object can be obtained via // me->ObjType() or me->Object()->TsType() and they may differ!!!! - if (auto objType = const_cast(ast)->Object()->TsType(); objType->IsETSUnionType()) { + if (auto objType = const_cast(ast)->Object()->TsType(); + objType != nullptr && objType->IsETSUnionType()) { bool res = true; for (auto type : objType->AsETSUnionType()->ConstituentTypes()) { const_cast(ast)->SetObjectType(type->AsETSObjectType()); diff --git a/ets2panda/compiler/lowering/ets/unboxLowering.cpp b/ets2panda/compiler/lowering/ets/unboxLowering.cpp index b33a59d270..0cf2d12eff 100644 --- a/ets2panda/compiler/lowering/ets/unboxLowering.cpp +++ b/ets2panda/compiler/lowering/ets/unboxLowering.cpp @@ -1193,7 +1193,7 @@ struct UnboxVisitor : public ir::visitor::EmptyAstVisitor { if (mexpr->Property()->Variable()->Declaration() != nullptr && mexpr->Property()->Variable()->Declaration()->Node() != nullptr && mexpr->Property()->Variable()->Declaration()->Node()->IsTyped() && - !mexpr->Object()->TsType()->IsETSAnyType()) { + mexpr->Object()->TsType() != nullptr && !mexpr->Object()->TsType()->IsETSAnyType()) { HandleDeclarationNode(uctx_, mexpr->Property()->Variable()->Declaration()->Node()); propType = mexpr->Property()->Variable()->Declaration()->Node()->AsTyped()->TsType(); } else if (mexpr->Property()->Variable()->TsType() != nullptr) { @@ -1219,7 +1219,8 @@ struct UnboxVisitor : public ir::visitor::EmptyAstVisitor { mexpr->Property()->AsIdentifier()->Name() == "length") { mexpr->SetTsType(uctx_->checker->GlobalIntType()); } - if (mexpr->Object()->TsType()->IsETSPrimitiveType() && !IsStaticMemberExpression(mexpr)) { + if (mexpr->Object()->TsType() != nullptr && mexpr->Object()->TsType()->IsETSPrimitiveType() && + !IsStaticMemberExpression(mexpr)) { // NOTE(gogabr): need to handle some elementary method calls as intrinsics mexpr->SetObject(InsertBoxing(uctx_, mexpr->Object())); } diff --git a/ets2panda/test/ast/compiler/ets/declaration_default_values.ets b/ets2panda/test/ast/compiler/ets/declaration_default_values.ets new file mode 100644 index 0000000000..373f0f09c5 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/declaration_default_values.ets @@ -0,0 +1,38 @@ +/* + * 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 MyNamespace { + export enum MyEnum { + EnumField1 = 0, + EnumField2 = 1 + } +} + +@interface ClassAnnotation { + field1: MyNamespace.MyEnum = MyNamespace.MyEnum.EnumField1; +} + + +@ClassAnnotation +class MyClass { + field2: MyNamespace.MyEnum = MyNamespace.MyEnum.EnumField2; +} + +function test() { + let myClass = new MyClass(); + arktest.assertEQ(myClass.field2, 1); +} + +test() -- Gitee