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

Commit 0ecf5c0

Browse files
committed
Fixex #5 (fix inference when expression is null)
[email protected] Review URL: https://chromereviews.googleplex.com/128507013
1 parent 24780b4 commit 0ecf5c0

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/src/checker/resolver.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ class RestrictedResolverVisitor extends ResolverVisitor {
110110
/// in the restricted type system and to infer types for untyped local
111111
/// variables.
112112
class RestrictedStaticTypeAnalyzer extends StaticTypeAnalyzer {
113-
RestrictedStaticTypeAnalyzer(ResolverVisitor r) : super(r);
113+
final DartType _bottomType;
114+
RestrictedStaticTypeAnalyzer(ResolverVisitor r)
115+
: _bottomType = r.typeProvider.bottomType, super(r);
114116

115117
@override // to infer type from initializers
116118
Object visitVariableDeclaration(VariableDeclaration node) {
@@ -128,8 +130,9 @@ class RestrictedStaticTypeAnalyzer extends StaticTypeAnalyzer {
128130
if (declaredType != null) return;
129131
VariableElementImpl element = node.element;
130132
if (element.type != dynamicType) return;
131-
element.type = getStaticType(initializer);
132-
return;
133+
var type = getStaticType(initializer);
134+
if (type == _bottomType) return;
135+
element.type = type;
133136
}
134137

135138
@override // to propagate types to identifiers

test/checker/inferred_type_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ main() {
4141
});
4242
});
4343

44+
test('do not infer type when initializer is null', () {
45+
testChecker({
46+
'/main.dart': '''
47+
test() {
48+
var x = null;
49+
x = "hi";
50+
x = /*config:Box*/3;
51+
}
52+
'''
53+
});
54+
});
55+
4456
test('propagate inference to field in class', () {
4557
testChecker({
4658
'/main.dart': '''

0 commit comments

Comments
 (0)