Skip to content

Commit a9dfc31

Browse files
committed
Infer fields/getters/setters types according to the new top-level inference rules.
[email protected] BUG= #28219 Review-Url: https://codereview.chromium.org/2761633002 .
1 parent 72c0c77 commit a9dfc31

File tree

8 files changed

+688
-834
lines changed

8 files changed

+688
-834
lines changed

pkg/analyzer/lib/src/task/strong_mode.dart

Lines changed: 172 additions & 126 deletions
Large diffs are not rendered by default.

pkg/analyzer/test/generated/strong_mode_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2750,7 +2750,7 @@ class D extends C {
27502750
Source source = addSource(r'''
27512751
abstract class A {
27522752
num get x;
2753-
set x(covariant num);
2753+
set x(covariant num _);
27542754
}
27552755
27562756
class B extends A {

pkg/analyzer/test/src/summary/element_text.dart

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ void applyCheckElementTextReplacements() {
5353
* actual text with the given [expected] one.
5454
*/
5555
void checkElementText(LibraryElement library, String expected,
56-
{bool withOffsets: false}) {
57-
var writer = new _ElementWriter(withOffsets: withOffsets);
56+
{bool withOffsets: false,
57+
bool withSyntheticAccessors: false,
58+
bool withSyntheticFields: false}) {
59+
var writer = new _ElementWriter(
60+
withOffsets: withOffsets,
61+
withSyntheticAccessors: withSyntheticAccessors,
62+
withSyntheticFields: withSyntheticFields);
5863
writer.writeLibraryElement(library);
5964

6065
String actualText = writer.buffer.toString();
@@ -117,14 +122,21 @@ void checkElementText(LibraryElement library, String expected,
117122
class _ElementWriter {
118123
final bool withOffsets;
119124
final bool withConstElements;
125+
final bool withSyntheticAccessors;
126+
final bool withSyntheticFields;
120127
final StringBuffer buffer = new StringBuffer();
121128

122-
_ElementWriter({this.withOffsets: false, this.withConstElements: true});
129+
_ElementWriter(
130+
{this.withOffsets: false,
131+
this.withConstElements: true,
132+
this.withSyntheticAccessors,
133+
this.withSyntheticFields: false});
123134

124135
bool isDynamicType(DartType type) => type is DynamicTypeImpl;
125136

126-
bool isEnumElement(Element e) {
127-
return e is ClassElement && e.isEnum;
137+
bool isEnumField(Element e) {
138+
Element enclosing = e.enclosingElement;
139+
return enclosing is ClassElement && enclosing.isEnum;
128140
}
129141

130142
void newLineIfNotEmpty() {
@@ -175,7 +187,7 @@ class _ElementWriter {
175187

176188
buffer.writeln(' {');
177189

178-
e.fields.forEach(writeFieldElement);
190+
e.fields.forEach(writePropertyInducingElement);
179191
e.accessors.forEach(writePropertyAccessorElement);
180192

181193
if (e.isEnum) {
@@ -410,22 +422,6 @@ class _ElementWriter {
410422
}
411423
}
412424

413-
void writeFieldElement(FieldElement e) {
414-
if (e.isSynthetic && !isEnumElement(e.enclosingElement)) {
415-
return;
416-
}
417-
418-
writeDocumentation(e, ' ');
419-
writeMetadata(e, ' ', '\n');
420-
421-
buffer.write(' ');
422-
423-
writeIf(e.isStatic, 'static ');
424-
writeIf(e is FieldElementImpl && e.isCovariant, 'covariant ');
425-
426-
writePropertyInducingElement(e);
427-
}
428-
429425
void writeFunctionElement(FunctionElement e) {
430426
writeIf(e.isExternal, 'external ');
431427

@@ -623,7 +619,7 @@ class _ElementWriter {
623619
}
624620

625621
void writePropertyAccessorElement(PropertyAccessorElement e) {
626-
if (e.isSynthetic) {
622+
if (e.isSynthetic && !withSyntheticAccessors) {
627623
return;
628624
}
629625

@@ -633,10 +629,12 @@ class _ElementWriter {
633629

634630
buffer.write(' ');
635631

632+
writeIf(e.isSynthetic, 'synthetic ');
636633
writeIf(e.isStatic, 'static ');
637634
} else {
638635
writeDocumentation(e);
639636
writeMetadata(e, '', '\n');
637+
writeIf(e.isSynthetic, 'synthetic ');
640638
}
641639

642640
writeIf(e.isExternal, 'external ');
@@ -669,9 +667,27 @@ class _ElementWriter {
669667
}
670668

671669
void writePropertyInducingElement(PropertyInducingElement e) {
670+
if (e.isSynthetic && !withSyntheticFields && !isEnumField(e)) {
671+
return;
672+
}
673+
672674
DartType type = e.type;
673675
expect(type, isNotNull);
674676

677+
if (e.enclosingElement is ClassElement) {
678+
writeDocumentation(e, ' ');
679+
writeMetadata(e, ' ', '\n');
680+
681+
buffer.write(' ');
682+
683+
writeIf(e.isSynthetic, 'synthetic ');
684+
writeIf(e.isStatic, 'static ');
685+
writeIf(e is FieldElementImpl && e.isCovariant, 'covariant ');
686+
} else {
687+
writeDocumentation(e);
688+
writeMetadata(e, '', '\n');
689+
}
690+
675691
writeIf(e.isFinal, 'final ');
676692
writeIf(e.isConst, 'const ');
677693
writeType2(type);
@@ -693,15 +709,6 @@ class _ElementWriter {
693709
buffer.writeln(';');
694710
}
695711

696-
void writeTopLevelVariableElement(TopLevelVariableElement e) {
697-
if (e.isSynthetic) {
698-
return;
699-
}
700-
writeDocumentation(e);
701-
writeMetadata(e, '', '\n');
702-
writePropertyInducingElement(e);
703-
}
704-
705712
void writeType(DartType type) {
706713
if (type is InterfaceType) {
707714
buffer.write(type.element.name);
@@ -739,7 +746,7 @@ class _ElementWriter {
739746
e.functionTypeAliases.forEach(writeFunctionTypeAliasElement);
740747
e.enums.forEach(writeClassElement);
741748
e.types.forEach(writeClassElement);
742-
e.topLevelVariables.forEach(writeTopLevelVariableElement);
749+
e.topLevelVariables.forEach(writePropertyInducingElement);
743750
e.accessors.forEach(writePropertyAccessorElement);
744751
e.functions.forEach(writeFunctionElement);
745752
}

0 commit comments

Comments
 (0)