已合并
override a private field expect CTE #8554
fanglou创建于 2025年9月25日
override a private field expect CTE #8554
已合并
共 3 个文件变更+33-0
| @@ -121,6 +121,11 @@ static void CheckOverride(ir::ClassProperty *st, ETSChecker *checker) | |||
| 121 | auto *propVar = superType->AsETSObjectType()->GetProperty(st->Id()->Name(), searchFlags); | 121 | auto *propVar = superType->AsETSObjectType()->GetProperty(st->Id()->Name(), searchFlags); |
| 122 | classDef = superType->AsETSObjectType()->GetDeclNode()->AsClassDefinition(); | 122 | classDef = superType->AsETSObjectType()->GetDeclNode()->AsClassDefinition(); |
| 123 | if (propVar != nullptr) { | 123 | if (propVar != nullptr) { |
| 124 | + if ((propVar->Declaration()->Node()->Modifiers() & ir::ModifierFlags::PRIVATE) != 0 && | ||
| 125 | + (st->Modifiers() & ir::ModifierFlags::OVERRIDE) != 0) { | ||
| 126 | + checker->LogError(diagnostic::OVERRIDE_NOT_PRIVATE, {propVar->Declaration()->Name(), superName}, | ||
| 127 | + st->Start()); | ||
| 128 | + } | ||
| 124 | return; | 129 | return; |
| 125 | } | 130 | } |
| 126 | } | 131 | } |
| @@ -0,0 +1,24 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Device Co., Ltd. | ||
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | + * you may not use this file except in compliance with the License. | ||
| 5 | + * You may obtain a copy of the License at | ||
| 6 | + * | ||
| 7 | + * http: //www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | + * | ||
| 9 | + * Unless required by applicable law or agreed to in writing, software | ||
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | + * See the License for the specific language governing permissions and | ||
| 13 | + * limitations under the License. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +class C { | ||
| 17 | + private num: number = 1 | ||
| 18 | +} | ||
| 19 | + | ||
| 20 | +class B extends C { | ||
| 21 | + override num: number = 2 | ||
It looks like you missed a space. Shouldn't this be ![]() ![]() | |||
| 22 | +} | ||
| 23 | + | ||
| 24 | +/* @@? 21:14 Error TypeError: The 'override' modifier is invalid here because the property 'num' is private in class 'C'. */ | ||
| @@ -1110,6 +1110,10 @@ semantic: | |||
| 1110 | id: 49702 | 1110 | id: 49702 |
| 1111 | message: "The 'override' modifier is invalid here because the base class '{}' does not declare this member." | 1111 | message: "The 'override' modifier is invalid here because the base class '{}' does not declare this member." |
| 1112 | 1112 | ||
| 1113 | +- name: OVERRIDE_NOT_PRIVATE | ||
| 1114 | + id: 1365480 | ||
| 1115 | + message: "The 'override' modifier is invalid here because the property '{}' is private in class '{}'." | ||
| 1116 | + | ||
| 1113 | - name: PARAM_COUNT_MISMATCH | 1117 | - name: PARAM_COUNT_MISMATCH |
| 1114 | id: 124 | 1118 | id: 124 |
| 1115 | message: "Expected {} arguments, got {}." | 1119 | message: "Expected {} arguments, got {}." |


According to the spec, the
overridemodifier is optional to be written out explicitly. With the patch, the following code will compile, but it's still invalid:class C { private num: number = 1 } class B extends C { num: number = 2 }