diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index febef1ccd56bfcd5733b3a88efbd17703f7f7ab4..d7295d15b3b56c0088b81214cce411aec08093fa 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 0000000000000000000000000000000000000000..4c2f80554b19891f72f10b80a5f9de5052b6d48c --- /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 1ce3c79fce67dd10ce2a5743b446f63e905b6e5f..023ea4ab96379ebd167a161952089b235eccc1a7 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