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

Unified Diff: lib/compiler/implementation/compile_time_constants.dart

Issue 10916232: Implement checked mode for const constructors. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 3 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 | lib/compiler/implementation/constant_system.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/compiler/implementation/compile_time_constants.dart
===================================================================
--- lib/compiler/implementation/compile_time_constants.dart (revision 12366)
+++ lib/compiler/implementation/compile_time_constants.dart (working copy)
@@ -642,6 +642,25 @@
return super.visitSend(send);
}
+ void potentiallyCheckType(Node node, Element element, Constant constant) {
+ if (compiler.enableTypeAssertions) {
+ DartType elementType = element.computeType(compiler);
+ DartType constantType = constant.computeType(compiler);
+ // TODO(ngeoffray): Handle type parameters.
+ if (elementType.element.isTypeVariable()) return;
+ if (!constantSystem.isSubtype(compiler, constantType, elementType)) {
+ MessageKind kind = MessageKind.NOT_ASSIGNABLE;
+ compiler.reportError(node, new CompileTimeConstantError(
+ kind, [elementType, constantType]));
+ }
+ }
+ }
+
+ void updateFieldValue(Node node, Element element, Constant constant) {
+ potentiallyCheckType(node, element, constant);
+ fieldValues[element] = constant;
+ }
+
/**
* Given the arguments (a list of constants) assigns them to the parameters,
* updating the definitions map. If the constructor has field-initializer
@@ -653,10 +672,12 @@
int index = 0;
parameters.forEachParameter((Element parameter) {
Constant argument = arguments[index++];
+ Node node = parameter.parseNode(compiler);
+ potentiallyCheckType(node, parameter, argument);
definitions[parameter] = argument;
if (parameter.kind == ElementKind.FIELD_PARAMETER) {
FieldParameterElement fieldParameterElement = parameter;
- fieldValues[fieldParameterElement.fieldElement] = argument;
+ updateFieldValue(node, fieldParameterElement.fieldElement, argument);
}
});
}
@@ -671,6 +692,8 @@
targetConstructor, constantSystem, compiler);
evaluator.evaluateConstructorFieldValues(compiledArguments);
// Copy over the fieldValues from the super/redirect-constructor.
+ // No need to go through [updateFieldValue] because the
+ // assignments have already been checked in checked mode.
evaluator.fieldValues.forEach((key, value) => fieldValues[key] = value);
}
@@ -703,7 +726,7 @@
Link<Node> initArguments = init.arguments;
assert(!initArguments.isEmpty() && initArguments.tail.isEmpty());
Constant fieldValue = evaluate(initArguments.head);
- fieldValues[elements[init]] = fieldValue;
+ updateFieldValue(init, elements[init], fieldValue);
}
}
}
« no previous file with comments | « no previous file | lib/compiler/implementation/constant_system.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698