From c332ed4f7fcb5ba22a0a929daba9e825125a604b Mon Sep 17 00:00:00 2001 From: yp9522 Date: Mon, 28 Jul 2025 15:34:27 +0800 Subject: [PATCH] fix bug for annotation class Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICSHDE Signed-off-by: yp9522 --- .../lowering/ets/enumPostCheckLowering.cpp | 34 ++++++++++++------- .../ast/compiler/ets/annotation_for_class.ets | 29 ++++++++++++++++ 2 files changed, 51 insertions(+), 12 deletions(-) create mode 100755 ets2panda/test/ast/compiler/ets/annotation_for_class.ets diff --git a/ets2panda/compiler/lowering/ets/enumPostCheckLowering.cpp b/ets2panda/compiler/lowering/ets/enumPostCheckLowering.cpp index abd8401d58..ef92dd27aa 100644 --- a/ets2panda/compiler/lowering/ets/enumPostCheckLowering.cpp +++ b/ets2panda/compiler/lowering/ets/enumPostCheckLowering.cpp @@ -382,6 +382,27 @@ ir::SwitchStatement *EnumPostCheckLoweringPhase::GenerateGetOrdinalCallForSwitch return node; } +static void RecheckNode(ir::AstNode *node, checker::ETSChecker *checker) +{ + // No parent class means that this node is not in the inheritance of the class + auto *parentClass = util::Helpers::FindAncestorGivenByType(node, ir::AstNodeType::CLASS_DEFINITION); + if (parentClass == nullptr) { + return; + } + if (node->IsExpression()) { + node->AsExpression()->SetTsType(nullptr); // force recheck + } + checker::SavedCheckerContext savedContext(checker, checker->Context().Status(), + parentClass->AsClassDefinition()->TsType()->AsETSObjectType()); + node->RemoveAstNodeFlags(ir::AstNodeFlags::RECHECK); + node->Check(checker); + + if (node->IsExpression() && node->AsExpression()->TsType() != nullptr && + !node->AsExpression()->TsType()->IsETSIntEnumType()) { + node->RemoveAstNodeFlags(ir::AstNodeFlags::GENERATE_VALUE_OF); + } +} + bool EnumPostCheckLoweringPhase::PerformForModule(public_lib::Context *ctx, parser::Program *program) { if (program->Extension() != ScriptExtension::ETS) { @@ -397,18 +418,7 @@ bool EnumPostCheckLoweringPhase::PerformForModule(public_lib::Context *ctx, pars // clang-format off [this](ir::AstNode *const node) -> ir::AstNode* { if (node->HasAstNodeFlags(ir::AstNodeFlags::RECHECK)) { - if (node->IsExpression()) { - node->AsExpression()->SetTsType(nullptr); // force recheck - } - auto *parentClass = util::Helpers::FindAncestorGivenByType(node, ir::AstNodeType::CLASS_DEFINITION); - checker::SavedCheckerContext savedContext(checker_, checker_->Context().Status(), - parentClass->AsClassDefinition()->TsType()->AsETSObjectType()); - node->RemoveAstNodeFlags(ir::AstNodeFlags::RECHECK); - node->Check(checker_); - if (node->IsExpression() && node->AsExpression()->TsType() != nullptr && - !node->AsExpression()->TsType()->IsETSIntEnumType()) { - node->RemoveAstNodeFlags(ir::AstNodeFlags::GENERATE_VALUE_OF); - } + RecheckNode(node, checker_); } if (node->HasAstNodeFlags(ir::AstNodeFlags::GENERATE_VALUE_OF)) { return GenerateValueOfCall(node); diff --git a/ets2panda/test/ast/compiler/ets/annotation_for_class.ets b/ets2panda/test/ast/compiler/ets/annotation_for_class.ets new file mode 100755 index 0000000000..89298795b2 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/annotation_for_class.ets @@ -0,0 +1,29 @@ +/* + * 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. + */ + +// Step 1: Define an enum with a numeric constant +enum MyEnum { + EnumField = 42 +} + +// Step 2: Define an annotation with a field initialized using the enum value +@interface ClassAnnotation { + field: number = MyEnum.EnumField; +} + +// Step 3: Apply the annotation to a class +@ClassAnnotation +class MyClass { +} -- Gitee