Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit b66ea52

Browse files
scheglovCommit Bot
authored andcommitted
Use elements in GetterSetterTypesVerifier, support fields.
Change-Id: I92376ace55845385e69742e0eaadcae407a71a53 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/232228 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent f64fc40 commit b66ea52

File tree

5 files changed

+149
-53
lines changed

5 files changed

+149
-53
lines changed

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

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/dart/ast/ast.dart';
65
import 'package:analyzer/dart/element/element.dart';
76
import 'package:analyzer/dart/element/type.dart';
87
import 'package:analyzer/error/error.dart';
@@ -32,42 +31,14 @@ class GetterSetterTypesVerifier {
3231

3332
bool get _isNonNullableByDefault => _typeSystem.isNonNullableByDefault;
3433

35-
void checkExtension(ExtensionDeclaration node) {
36-
for (var getterNode in node.members) {
37-
if (getterNode is MethodDeclaration && getterNode.isGetter) {
38-
checkGetter(getterNode.name,
39-
getterNode.declaredElement as PropertyAccessorElement);
34+
void checkExtension(ExtensionElement element) {
35+
for (var getter in element.accessors) {
36+
if (getter.isGetter) {
37+
_checkLocalGetter(getter);
4038
}
4139
}
4240
}
4341

44-
void checkGetter(
45-
SimpleIdentifier nameNode,
46-
PropertyAccessorElement getter,
47-
) {
48-
assert(getter.isGetter);
49-
50-
var setter = getter.correspondingSetter;
51-
if (setter == null) {
52-
return;
53-
}
54-
55-
var getterType = _getGetterType(getter);
56-
var setterType = _getSetterType(setter);
57-
if (setterType == null) {
58-
return;
59-
}
60-
61-
if (!_match(getterType, setterType)) {
62-
var name = nameNode.name;
63-
_errorReporter.reportErrorForNode(
64-
_errorCode,
65-
nameNode,
66-
[name, getterType, setterType, name],
67-
);
68-
}
69-
}
70-
7142
void checkInterface(ClassElement classElement, Interface interface) {
7243
var libraryUri = classElement.library.source.uri;
7344

@@ -113,6 +84,36 @@ class GetterSetterTypesVerifier {
11384
}
11485
}
11586

87+
void checkStaticAccessors(List<PropertyAccessorElement> accessors) {
88+
for (var getter in accessors) {
89+
// TODO(scheglov) Update `isStatic` instead
90+
if ((getter.isStatic ||
91+
getter.enclosingElement is CompilationUnitElement) &&
92+
getter.isGetter) {
93+
_checkLocalGetter(getter);
94+
}
95+
}
96+
}
97+
98+
void _checkLocalGetter(PropertyAccessorElement getter) {
99+
assert(getter.isGetter);
100+
var setter = getter.correspondingSetter;
101+
if (setter != null) {
102+
var getterType = _getGetterType(getter);
103+
var setterType = _getSetterType(setter);
104+
if (setterType != null) {
105+
if (!_match(getterType, setterType)) {
106+
var name = getter.name;
107+
_errorReporter.reportErrorForElement(
108+
_errorCode,
109+
getter,
110+
[name, getterType, setterType, name],
111+
);
112+
}
113+
}
114+
}
115+
}
116+
116117
bool _match(DartType getterType, DartType setterType) {
117118
return _isNonNullableByDefault
118119
? _typeSystem.isSubtypeOf(getterType, setterType)

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,9 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
423423
void visitClassDeclaration(ClassDeclaration node) {
424424
var outerClass = _enclosingClass;
425425
try {
426+
var element = node.declaredElement as ClassElementImpl;
426427
_isInNativeClass = node.nativeClause != null;
427-
_enclosingClass = node.declaredElement as ClassElementImpl;
428+
_enclosingClass = element;
428429

429430
List<ClassMember> members = node.members;
430431
_duplicateDefinitionVerifier.checkClass(node);
@@ -448,6 +449,12 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
448449
_checkForBadFunctionUse(node);
449450
_checkForWrongTypeParameterVarianceInSuperinterfaces();
450451
_checkForMainFunction(node.name);
452+
453+
GetterSetterTypesVerifier(
454+
typeSystem: typeSystem,
455+
errorReporter: errorReporter,
456+
).checkStaticAccessors(element.accessors);
457+
451458
super.visitClassDeclaration(node);
452459
} finally {
453460
_isInNativeClass = false;
@@ -485,10 +492,17 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
485492

486493
@override
487494
void visitCompilationUnit(CompilationUnit node) {
495+
var element = node.declaredElement as CompilationUnitElement;
488496
_featureSet = node.featureSet;
489497
_duplicateDefinitionVerifier.checkUnit(node);
490498
_checkForDeferredPrefixCollisions(node);
491499
_checkForIllegalLanguageOverride(node);
500+
501+
GetterSetterTypesVerifier(
502+
typeSystem: typeSystem,
503+
errorReporter: errorReporter,
504+
).checkStaticAccessors(element.accessors);
505+
492506
super.visitCompilationUnit(node);
493507
_featureSet = null;
494508
}
@@ -589,6 +603,11 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
589603
_checkForMainFunction(node.name);
590604
_checkForEnumInstantiatedToBoundsIsNotWellBounded(node, element);
591605

606+
GetterSetterTypesVerifier(
607+
typeSystem: typeSystem,
608+
errorReporter: errorReporter,
609+
).checkStaticAccessors(element.accessors);
610+
592611
super.visitEnumDeclaration(node);
593612
} finally {
594613
_enclosingClass = outerClass;
@@ -620,15 +639,16 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
620639

621640
@override
622641
void visitExtensionDeclaration(ExtensionDeclaration node) {
623-
_enclosingExtension = node.declaredElement;
642+
var element = node.declaredElement!;
643+
_enclosingExtension = element;
624644
_duplicateDefinitionVerifier.checkExtension(node);
625645
_checkForConflictingExtensionTypeVariableErrorCodes();
626646
_checkForFinalNotInitializedInClass(node.members);
627647

628648
GetterSetterTypesVerifier(
629649
typeSystem: typeSystem,
630650
errorReporter: errorReporter,
631-
).checkExtension(node);
651+
).checkExtension(element);
632652

633653
final name = node.name;
634654
if (name != null) {
@@ -727,13 +747,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
727747
_withEnclosingExecutable(functionElement, () {
728748
SimpleIdentifier identifier = node.name;
729749
TypeAnnotation? returnType = node.returnType;
730-
if (node.isGetter) {
731-
GetterSetterTypesVerifier(
732-
typeSystem: typeSystem,
733-
errorReporter: errorReporter,
734-
).checkGetter(
735-
node.name, node.declaredElement as PropertyAccessorElement);
736-
}
737750
if (node.isSetter) {
738751
FunctionExpression functionExpression = node.functionExpression;
739752
_checkForWrongNumberOfParametersForSetter(
@@ -918,13 +931,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
918931
void visitMethodDeclaration(MethodDeclaration node) {
919932
_withEnclosingExecutable(node.declaredElement!, () {
920933
var returnType = node.returnType;
921-
if (node.isStatic && node.isGetter) {
922-
GetterSetterTypesVerifier(
923-
typeSystem: typeSystem,
924-
errorReporter: errorReporter,
925-
).checkGetter(
926-
node.name, node.declaredElement as PropertyAccessorElement);
927-
}
928934
if (node.isSetter) {
929935
_checkForWrongNumberOfParametersForSetter(node.name, node.parameters);
930936
_checkForNonVoidReturnTypeForSetter(returnType);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ enum E {
538538
await assertNoErrorsInCode(r'''
539539
enum E {
540540
foo;
541-
static set foo(int _) {}
541+
static set foo(_) {}
542542
}
543543
''');
544544
}

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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ class C {
4646
''');
4747
}
4848

49+
test_class_instance_field() async {
50+
await assertErrorsInCode('''
51+
class C {
52+
final num foo = 0;
53+
set foo(int v) {}
54+
}
55+
''', [
56+
error(CompileTimeErrorCode.GETTER_NOT_SUBTYPE_SETTER_TYPES, 22, 3),
57+
]);
58+
}
59+
4960
test_class_instance_interfaces() async {
5061
await assertErrorsInCode(r'''
5162
class A {
@@ -213,6 +224,64 @@ class C {
213224
]);
214225
}
215226

227+
test_class_static_field() async {
228+
await assertErrorsInCode('''
229+
class C {
230+
static final num foo = 0;
231+
static set foo(int v) {}
232+
}
233+
''', [
234+
error(CompileTimeErrorCode.GETTER_NOT_SUBTYPE_SETTER_TYPES, 29, 3),
235+
]);
236+
}
237+
238+
test_enum_instance() async {
239+
await assertErrorsInCode('''
240+
enum E {
241+
v;
242+
num get foo => 0;
243+
set foo(int v) {}
244+
}
245+
''', [
246+
error(CompileTimeErrorCode.GETTER_NOT_SUBTYPE_SETTER_TYPES, 24, 3),
247+
]);
248+
}
249+
250+
test_enum_instance_field() async {
251+
await assertErrorsInCode('''
252+
enum E {
253+
v;
254+
final num foo = 0;
255+
set foo(int v) {}
256+
}
257+
''', [
258+
error(CompileTimeErrorCode.GETTER_NOT_SUBTYPE_SETTER_TYPES, 26, 3),
259+
]);
260+
}
261+
262+
test_enum_static() async {
263+
await assertErrorsInCode('''
264+
enum E {
265+
v;
266+
static num get foo => 0;
267+
static set foo(int v) {}
268+
}
269+
''', [
270+
error(CompileTimeErrorCode.GETTER_NOT_SUBTYPE_SETTER_TYPES, 31, 3),
271+
]);
272+
}
273+
274+
test_enum_static_field() async {
275+
await assertErrorsInCode('''
276+
enum E {
277+
foo;
278+
static set foo(int v) {}
279+
}
280+
''', [
281+
error(CompileTimeErrorCode.GETTER_NOT_SUBTYPE_SETTER_TYPES, 11, 3),
282+
]);
283+
}
284+
216285
test_extension_instance() async {
217286
await assertErrorsInCode('''
218287
extension E on Object {
@@ -235,6 +304,17 @@ extension E on Object {
235304
]);
236305
}
237306

307+
test_extension_static_field() async {
308+
await assertErrorsInCode('''
309+
extension E on Object {
310+
static final int foo = 0;
311+
static set foo(String v) {}
312+
}
313+
''', [
314+
error(CompileTimeErrorCode.GETTER_NOT_SUBTYPE_SETTER_TYPES, 43, 3),
315+
]);
316+
}
317+
238318
test_topLevel() async {
239319
await assertErrorsInCode('''
240320
int get foo => 0;
@@ -266,4 +346,13 @@ int get foo => 0;
266346
set foo(int v) {}
267347
''');
268348
}
349+
350+
test_topLevel_variable() async {
351+
await assertErrorsInCode('''
352+
final int foo = 0;
353+
set foo(String v) {}
354+
''', [
355+
error(CompileTimeErrorCode.GETTER_NOT_SUBTYPE_SETTER_TYPES, 10, 3),
356+
]);
357+
}
269358
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ enum E {
107107
await assertErrorsInCode(r'''
108108
enum E {
109109
v;
110-
set values(int _) {}
110+
set values(_) {}
111111
}
112112
''', [
113113
error(CompileTimeErrorCode.VALUES_DECLARATION_IN_ENUM, 20, 6),
@@ -118,7 +118,7 @@ enum E {
118118
await assertErrorsInCode(r'''
119119
enum E {
120120
v;
121-
static set values(int _) {}
121+
static set values(_) {}
122122
}
123123
''', [
124124
error(CompileTimeErrorCode.VALUES_DECLARATION_IN_ENUM, 27, 6),

0 commit comments

Comments
 (0)