Skip to content

Commit 7b8735e

Browse files
pqCommit Queue
authored and
Commit Queue
committed
annotation validation for extension types
validation/tests for application of * `@mustCallSuper` * `@nonVirtual` * `@reopen` * `@sealed` to extension types See: #53434 Change-Id: I005523507044e7d5879a5589c132384e7bb8be56 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/324768 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent c4d2f64 commit 7b8735e

5 files changed

+54
-0
lines changed

pkg/analyzer/lib/src/error/annotation_verifier.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ class AnnotationVerifier {
227227
if ((parent is MethodDeclaration && parent.isStatic) ||
228228
(parent is FieldDeclaration && parent.isStatic) ||
229229
parent.parent is ExtensionDeclaration ||
230+
parent.parent is ExtensionTypeDeclaration ||
230231
parent.parent is EnumDeclaration) {
231232
_errorReporter.reportErrorForNode(
232233
WarningCode.INVALID_ANNOTATION_TARGET,
@@ -247,6 +248,7 @@ class AnnotationVerifier {
247248
}
248249
} else if (parent is MethodDeclaration) {
249250
if (parent.parent is ExtensionDeclaration ||
251+
parent.parent is ExtensionTypeDeclaration ||
250252
parent.isStatic ||
251253
parent.isAbstract) {
252254
_errorReporter.reportErrorForNode(

pkg/analyzer/test/src/diagnostics/invalid_annotation_target_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,19 @@ extension E on String {
334334
]);
335335
}
336336

337+
test_extensionType_instance_method() async {
338+
await assertErrorsInCode(r'''
339+
import 'package:meta/meta.dart';
340+
341+
extension type E(int i) {
342+
@mustCallSuper
343+
void m() { }
344+
}
345+
''', [
346+
error(WarningCode.INVALID_ANNOTATION_TARGET, 62, 14),
347+
]);
348+
}
349+
337350
test_mixin_instance_method() async {
338351
await assertNoErrorsInCode(r'''
339352
import 'package:meta/meta.dart';

pkg/analyzer/test/src/diagnostics/invalid_non_virtual_annotation_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,19 @@ extension E on Object {
167167
]);
168168
}
169169

170+
test_extensionType_instance_method() async {
171+
await assertErrorsInCode(r'''
172+
import 'package:meta/meta.dart';
173+
174+
extension type E(int i) {
175+
@nonVirtual
176+
void m() { }
177+
}
178+
''', [
179+
error(WarningCode.INVALID_NON_VIRTUAL_ANNOTATION, 62, 11),
180+
]);
181+
}
182+
170183
test_import() async {
171184
await assertErrorsInCode(r'''
172185
@nonVirtual

pkg/analyzer/test/src/diagnostics/invalid_reopen_annotation_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,19 @@ base mixin class B implements A {}
108108
]);
109109
}
110110

111+
test_extensionType() async {
112+
await assertErrorsInCode(r'''
113+
import 'package:meta/meta.dart';
114+
115+
@reopen
116+
extension type E(int i) {
117+
void m() { }
118+
}
119+
''', [
120+
error(WarningCode.INVALID_ANNOTATION_TARGET, 35, 6),
121+
]);
122+
}
123+
111124
test_finalClass_supertypeIsFinal() async {
112125
await assertErrorsInCode(r'''
113126
import 'package:meta/meta.dart';

pkg/analyzer/test/src/diagnostics/invalid_sealed_annotation_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ import 'package:meta/meta.dart';
2929
''');
3030
}
3131

32+
test_extensionType() async {
33+
await assertErrorsInCode(r'''
34+
import 'package:meta/meta.dart';
35+
36+
@sealed
37+
extension type E(int i) {
38+
void m() { }
39+
}
40+
''', [
41+
error(WarningCode.INVALID_SEALED_ANNOTATION, 34, 7),
42+
]);
43+
}
44+
3245
test_mixin() async {
3346
await assertErrorsInCode(r'''
3447
import 'package:meta/meta.dart';

0 commit comments

Comments
 (0)