Skip to content

Commit 08c0108

Browse files
scheglovCommit Queue
authored and
Commit Queue
committed
Extension type. Check getter/setter types correspondence.
Change-Id: Idcf1313f0119145fce23b2617ad03b4d852b54d9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/330663 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent e7eda9a commit 08c0108

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class GetterSetterTypesVerifier {
3939
}
4040
}
4141

42+
void checkExtensionType(ExtensionTypeElement element, Interface interface) {
43+
checkInterface(element, interface);
44+
checkStaticAccessors(element.accessors);
45+
}
46+
4247
void checkInterface(InterfaceElement element, Interface interface) {
4348
var libraryUri = element.library.source.uri;
4449

@@ -54,7 +59,12 @@ class GetterSetterTypesVerifier {
5459
if (!_match(getterType, setterType)) {
5560
Element errorElement;
5661
if (getter.enclosingElement == element) {
57-
errorElement = getter;
62+
if (element is ExtensionTypeElement &&
63+
element.representation.getter == getter) {
64+
errorElement = setter;
65+
} else {
66+
errorElement = getter;
67+
}
5868
} else if (setter.enclosingElement == element) {
5969
errorElement = setter;
6070
} else {

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,12 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
721721
_checkForExtensionTypeWithAbstractMember(node);
722722
_checkForWrongTypeParameterVarianceInSuperinterfaces();
723723

724+
final interface = _inheritanceManager.getInterface(element);
725+
GetterSetterTypesVerifier(
726+
typeSystem: typeSystem,
727+
errorReporter: errorReporter,
728+
).checkExtensionType(element, interface);
729+
724730
super.visitExtensionTypeDeclaration(node);
725731
} finally {
726732
_constructorFieldsVerifier.leaveClass();

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,63 @@ extension E on Object {
370370
]);
371371
}
372372

373+
test_extensionType_instance() async {
374+
await assertErrorsInCode('''
375+
extension type A(int it) {
376+
int get foo => 0;
377+
void set foo(String _) {}
378+
}
379+
''', [
380+
error(CompileTimeErrorCode.GETTER_NOT_SUBTYPE_SETTER_TYPES, 37, 3),
381+
]);
382+
}
383+
384+
test_extensionType_instance_fromImplements() async {
385+
await assertErrorsInCode('''
386+
extension type A(int it) {
387+
void set foo(String _) {}
388+
}
389+
390+
extension type B(int it) implements A {
391+
int get foo => 0;
392+
}
393+
''', [
394+
error(CompileTimeErrorCode.GETTER_NOT_SUBTYPE_SETTER_TYPES, 108, 3),
395+
]);
396+
}
397+
398+
test_extensionType_instance_representationField() async {
399+
await assertErrorsInCode('''
400+
extension type A(int it) {
401+
void set it(String _) {}
402+
}
403+
''', [
404+
error(CompileTimeErrorCode.GETTER_NOT_SUBTYPE_SETTER_TYPES, 38, 2),
405+
]);
406+
}
407+
408+
test_extensionType_static() async {
409+
await assertErrorsInCode('''
410+
extension type A(int it) {
411+
static int get foo => 0;
412+
static set foo(String v) {}
413+
}
414+
''', [
415+
error(CompileTimeErrorCode.GETTER_NOT_SUBTYPE_SETTER_TYPES, 44, 3),
416+
]);
417+
}
418+
419+
test_extensionType_static_field() async {
420+
await assertErrorsInCode('''
421+
extension type A(int it) {
422+
static final int foo = 0;
423+
static set foo(String v) {}
424+
}
425+
''', [
426+
error(CompileTimeErrorCode.GETTER_NOT_SUBTYPE_SETTER_TYPES, 46, 3),
427+
]);
428+
}
429+
373430
test_topLevel() async {
374431
await assertErrorsInCode('''
375432
int get foo => 0;

0 commit comments

Comments
 (0)