From 8527e7486508bf28659c04c3f242a6c24da19987 Mon Sep 17 00:00:00 2001 From: xutianyi <452312890@qq.com> Date: Thu, 3 Apr 2025 14:06:44 +0800 Subject: [PATCH] Producing CTE for invalid field modifier Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/IBNXG5 Signed-off-by: xutianyi <452312890@qq.com> --- ets2panda/checker/ETSAnalyzer.cpp | 19 +++++++++++ .../compiler/ets/invalid_field_modifier.ets | 34 +++++++++++++++++++ ets2panda/util/diagnostic/semantic.yaml | 4 +++ 3 files changed, 57 insertions(+) create mode 100644 ets2panda/test/ast/compiler/ets/invalid_field_modifier.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index febef1ccd5..d7295d15b3 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -73,6 +73,24 @@ checker::Type *ETSAnalyzer::Check(ir::ClassDefinition *node) const return node->TsType(); } +static void CheckFieldModifiers(ETSChecker *checker, ir::ClassProperty *st) +{ + auto fieldName = st->Id()->Name(); + + if (st->IsAbstract() && st->Parent()->IsClassDefinition()) { + checker->LogError(diagnostic::FIELD_ILLEGAL_MODIFIER, {fieldName, "abstract"}, st->Start()); + } + if (st->IsFinal()) { + checker->LogError(diagnostic::FIELD_ILLEGAL_MODIFIER, {fieldName, "final"}, st->Start()); + } + if (st->IsNative()) { + checker->LogError(diagnostic::FIELD_ILLEGAL_MODIFIER, {fieldName, "native"}, st->Start()); + } + if (st->IsAsync()) { + checker->LogError(diagnostic::FIELD_ILLEGAL_MODIFIER, {fieldName, "async"}, st->Start()); + } +} + checker::Type *ETSAnalyzer::Check(ir::ClassProperty *st) const { if (st->TsType() != nullptr) { @@ -89,6 +107,7 @@ checker::Type *ETSAnalyzer::Check(ir::ClassProperty *st) const ES2PANDA_ASSERT(st->Id()->Variable() != nullptr); + CheckFieldModifiers(checker, st); checker->CheckAnnotations(st->Annotations()); if (st->TypeAnnotation() != nullptr) { st->TypeAnnotation()->Check(checker); diff --git a/ets2panda/test/ast/compiler/ets/invalid_field_modifier.ets b/ets2panda/test/ast/compiler/ets/invalid_field_modifier.ets new file mode 100644 index 0000000000..4c2f80554b --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/invalid_field_modifier.ets @@ -0,0 +1,34 @@ +/* + * 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. + */ +class A { + abstract a1: number + final a2: number + async a3: number + native a4: number +} + +class B { + abstract native b1: number + abstract final b2: number +} + +/* @@? 16:14 Error TypeError: Field 'a1' has illegal modifier 'abstract'. */ +/* @@? 17:11 Error TypeError: Field 'a2' has illegal modifier 'final'. */ +/* @@? 18:11 Error TypeError: Field 'a3' has illegal modifier 'async'. */ +/* @@? 19:12 Error TypeError: Field 'a4' has illegal modifier 'native'. */ +/* @@? 23:21 Error TypeError: Field 'b1' has illegal modifier 'abstract'. */ +/* @@? 23:21 Error TypeError: Field 'b1' has illegal modifier 'native'. */ +/* @@? 24:20 Error TypeError: Field 'b2' has illegal modifier 'abstract'. */ +/* @@? 24:20 Error TypeError: Field 'b2' has illegal modifier 'final'. */ diff --git a/ets2panda/util/diagnostic/semantic.yaml b/ets2panda/util/diagnostic/semantic.yaml index 1ce3c79fce..023ea4ab96 100644 --- a/ets2panda/util/diagnostic/semantic.yaml +++ b/ets2panda/util/diagnostic/semantic.yaml @@ -1106,3 +1106,7 @@ semantic: - name: LOCAL_CLASS_NATIVE_METHOD id: 278 message: "Local class '{}' shouldn't have native methods/constructors" + +- name: FIELD_ILLEGAL_MODIFIER + id: 279 + message: "Field '{}' has illegal modifier '{}'." \ No newline at end of file -- Gitee