Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(459)

Unified Diff: pkg/compiler/lib/src/typechecker.dart

Issue 2250433007: Type check field initializers in the correct context. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Updated cf. comments. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/language/typecheck_multifield_declaration_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/compiler/lib/src/typechecker.dart
diff --git a/pkg/compiler/lib/src/typechecker.dart b/pkg/compiler/lib/src/typechecker.dart
index 17f9c68b47052f889668b2783a5bdefff1ba9dd5..01cff2d5f281a33291f931338dffa55057979835 100644
--- a/pkg/compiler/lib/src/typechecker.dart
+++ b/pkg/compiler/lib/src/typechecker.dart
@@ -64,8 +64,12 @@ class TypeCheckerTask extends CompilerTask {
compiler, resolvedAst.elements, compiler.types);
if (element.isField) {
visitor.analyzingInitializer = true;
+ DartType type =
+ visitor.analyzeVariableTypeAnnotation(resolvedAst.node);
+ visitor.analyzeVariableInitializer(element, type, resolvedAst.body);
+ } else {
+ resolvedAst.node.accept(visitor);
}
- resolvedAst.node.accept(visitor);
});
});
}
@@ -1641,6 +1645,8 @@ class TypeCheckerVisitor extends Visitor<DartType> {
checkPrivateAccess(node, element, element.name);
DartType newType = elements.getType(node);
+ assert(invariant(node, newType != null,
+ message: "No new type registered in $elements."));
DartType constructorType = computeConstructorType(element, newType);
analyzeArguments(node.send, element, constructorType);
return newType;
@@ -1758,12 +1764,25 @@ class TypeCheckerVisitor extends Visitor<DartType> {
return elements.getType(node);
}
- DartType visitVariableDefinitions(VariableDefinitions node) {
+ DartType analyzeVariableTypeAnnotation(VariableDefinitions node) {
DartType type = analyzeWithDefault(node.type, const DynamicType());
if (type.isVoid) {
reportTypeWarning(node.type, MessageKind.VOID_VARIABLE);
type = const DynamicType();
}
+ return type;
+ }
+
+ void analyzeVariableInitializer(
+ Spannable spannable, DartType declaredType, Node initializer) {
+ if (initializer == null) return;
+
+ DartType expressionType = analyzeNonVoid(initializer);
+ checkAssignable(spannable, expressionType, declaredType);
+ }
+
+ DartType visitVariableDefinitions(VariableDefinitions node) {
+ DartType type = analyzeVariableTypeAnnotation(node);
for (Link<Node> link = node.definitions.nodes;
!link.isEmpty;
link = link.tail) {
@@ -1772,8 +1791,10 @@ class TypeCheckerVisitor extends Visitor<DartType> {
message: 'expected identifier or initialization');
if (definition is SendSet) {
SendSet initialization = definition;
- DartType initializer = analyzeNonVoid(initialization.arguments.head);
- checkAssignable(initialization.assignmentOperator, initializer, type);
+ analyzeVariableInitializer(
+ initialization.assignmentOperator,
+ type,
+ initialization.arguments.head);
// TODO(sigmund): explore inferring a type for `var` using the RHS (like
// DDC does), for example:
// if (node.type == null && node.modifiers.isVar &&
« no previous file with comments | « no previous file | tests/language/typecheck_multifield_declaration_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698