Don't overwrite inherited types of fields with types of initializers.
R=brianwilkerson@google.com
Change-Id: I0a92cc38e6f4bb8fe8f2102bf62f27c61ade1823
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/99715
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/src/summary2/top_level_inference.dart b/pkg/analyzer/lib/src/summary2/top_level_inference.dart
index 89f58a0..d34a336 100644
--- a/pkg/analyzer/lib/src/summary2/top_level_inference.dart
+++ b/pkg/analyzer/lib/src/summary2/top_level_inference.dart
@@ -84,12 +84,13 @@
}
void _performOverrideInference() {
+ var inferrer = new InstanceMemberInferrer(
+ linker.typeProvider,
+ linker.inheritance,
+ )..onlyOverrideInference = true;
for (var builder in linker.builders.values) {
for (var unit in builder.element.units) {
- new InstanceMemberInferrer(
- linker.typeProvider,
- linker.inheritance,
- ).inferCompilationUnit(unit);
+ inferrer.inferCompilationUnit(unit);
}
}
}
@@ -140,8 +141,7 @@
void evaluate() {
_resolveInitializer();
- VariableDeclarationList parent = _node.parent;
- if (parent.type == null) {
+ if (LazyAst.getType(_node) == null) {
var initializerType = _node.initializer.staticType;
initializerType = _dynamicIfNull(initializerType);
LazyAst.setType(_node, initializerType);
@@ -234,8 +234,7 @@
if (element.isSynthetic) return;
VariableDeclaration node = _getLinkedNode(element);
- VariableDeclarationList variableList = node.parent;
- if (variableList.type == null || element.isConst) {
+ if (LazyAst.getType(node) == null || element.isConst) {
if (node.initializer != null) {
_walker.addNode(element, _library, _scope, node);
} else {
diff --git a/pkg/analyzer/lib/src/task/strong_mode.dart b/pkg/analyzer/lib/src/task/strong_mode.dart
index 35a6337..df42a34 100644
--- a/pkg/analyzer/lib/src/task/strong_mode.dart
+++ b/pkg/analyzer/lib/src/task/strong_mode.dart
@@ -42,6 +42,12 @@
InterfaceType interfaceType;
/**
+ * When `true`, only inference using override is performed, and initializers
+ * of fields are ignored.
+ */
+ bool onlyOverrideInference = false;
+
+ /**
* Initialize a newly create inferrer.
*/
InstanceMemberInferrer(this.typeProvider, this.inheritance);
@@ -420,15 +426,22 @@
if (field.hasImplicitType) {
DartType newType = typeResult.type;
- if (newType == null && field.initializer != null) {
- newType = field.initializer.returnType;
- }
- if (newType == null || newType.isBottom || newType.isDartCoreNull) {
- newType = typeProvider.dynamicType;
- }
+ if (onlyOverrideInference) {
+ if (newType != null) {
+ setFieldType(field, newType);
+ }
+ } else {
+ if (newType == null && field.initializer != null) {
+ newType = field.initializer.returnType;
+ }
- setFieldType(field, newType);
+ if (newType == null || newType.isBottom || newType.isDartCoreNull) {
+ newType = typeProvider.dynamicType;
+ }
+
+ setFieldType(field, newType);
+ }
}
if (field.setter != null) {