Index: pkg/analyzer/lib/src/generated/static_type_analyzer.dart |
diff --git a/pkg/analyzer/lib/src/generated/static_type_analyzer.dart b/pkg/analyzer/lib/src/generated/static_type_analyzer.dart |
index f68147180e5529973e12766669e0348cd963a001..9f4aa69f923023697973a431b924f09849a2e168 100644 |
--- a/pkg/analyzer/lib/src/generated/static_type_analyzer.dart |
+++ b/pkg/analyzer/lib/src/generated/static_type_analyzer.dart |
@@ -1029,8 +1029,18 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<Object> { |
*/ |
@override |
Object visitNullLiteral(NullLiteral node) { |
- // TODO(jmesserly): in strong mode, should we just use the context type? |
- _recordStaticType(node, _typeProvider.bottomType); |
+ DartType type; |
+ if (_strongMode) { |
+ // In strong mode, we use the surrounding context type if we have one. |
+ // |
+ // Otherwise we use dynamic, because that's what we want to be inferred |
+ // by default. For example in `var x = null;` the type of `x` is dynamic. |
+ type = InferenceContext.getType(node) ?? _typeProvider.dynamicType; |
+ } else { |
+ // In Dart 1, null is bottom to make it assignable to everything. |
+ type = _typeProvider.bottomType; |
+ } |
+ _recordStaticType(node, type); |
return null; |
} |