@@ -64,8 +64,12 @@ class TypeCheckerTask extends CompilerTask {
64
64
compiler, resolvedAst.elements, compiler.types);
65
65
if (element.isField) {
66
66
visitor.analyzingInitializer = true ;
67
+ DartType type =
68
+ visitor.analyzeVariableTypeAnnotation (resolvedAst.node);
69
+ visitor.analyzeVariableInitializer (element, type, resolvedAst.body);
70
+ } else {
71
+ resolvedAst.node.accept (visitor);
67
72
}
68
- resolvedAst.node.accept (visitor);
69
73
});
70
74
});
71
75
}
@@ -1641,6 +1645,8 @@ class TypeCheckerVisitor extends Visitor<DartType> {
1641
1645
checkPrivateAccess (node, element, element.name);
1642
1646
1643
1647
DartType newType = elements.getType (node);
1648
+ assert (invariant (node, newType != null ,
1649
+ message: "No new type registered in $elements ." ));
1644
1650
DartType constructorType = computeConstructorType (element, newType);
1645
1651
analyzeArguments (node.send, element, constructorType);
1646
1652
return newType;
@@ -1758,12 +1764,25 @@ class TypeCheckerVisitor extends Visitor<DartType> {
1758
1764
return elements.getType (node);
1759
1765
}
1760
1766
1761
- DartType visitVariableDefinitions (VariableDefinitions node) {
1767
+ DartType analyzeVariableTypeAnnotation (VariableDefinitions node) {
1762
1768
DartType type = analyzeWithDefault (node.type, const DynamicType ());
1763
1769
if (type.isVoid) {
1764
1770
reportTypeWarning (node.type, MessageKind .VOID_VARIABLE );
1765
1771
type = const DynamicType ();
1766
1772
}
1773
+ return type;
1774
+ }
1775
+
1776
+ void analyzeVariableInitializer (
1777
+ Spannable spannable, DartType declaredType, Node initializer) {
1778
+ if (initializer == null ) return ;
1779
+
1780
+ DartType expressionType = analyzeNonVoid (initializer);
1781
+ checkAssignable (spannable, expressionType, declaredType);
1782
+ }
1783
+
1784
+ DartType visitVariableDefinitions (VariableDefinitions node) {
1785
+ DartType type = analyzeVariableTypeAnnotation (node);
1767
1786
for (Link <Node > link = node.definitions.nodes;
1768
1787
! link.isEmpty;
1769
1788
link = link.tail) {
@@ -1772,8 +1791,10 @@ class TypeCheckerVisitor extends Visitor<DartType> {
1772
1791
message: 'expected identifier or initialization' );
1773
1792
if (definition is SendSet ) {
1774
1793
SendSet initialization = definition;
1775
- DartType initializer = analyzeNonVoid (initialization.arguments.head);
1776
- checkAssignable (initialization.assignmentOperator, initializer, type);
1794
+ analyzeVariableInitializer (
1795
+ initialization.assignmentOperator,
1796
+ type,
1797
+ initialization.arguments.head);
1777
1798
// TODO(sigmund): explore inferring a type for `var` using the RHS (like
1778
1799
// DDC does), for example:
1779
1800
// if (node.type == null && node.modifiers.isVar &&
0 commit comments