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

Unified Diff: compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java

Issue 9289029: Added runtime type checking to the elements of list and map literals in dartc (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 11 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
Index: compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java
diff --git a/compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java b/compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java
index 43951aaee030a2d0d9f235b2915a7725e871082a..9e4eac4771a8a9ce46700e0e7ec7999fc0d56d44 100644
--- a/compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java
+++ b/compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java
@@ -175,6 +175,7 @@ public class TypeAnalyzer implements DartCompilationPhase {
private final Type nullType;
private final InterfaceType functionType;
private final InterfaceType dynamicIteratorType;
+ private final boolean developerModeChecks;
/**
* Keeps track of the number of nested catches, used to detect re-throws
@@ -182,11 +183,11 @@ public class TypeAnalyzer implements DartCompilationPhase {
*/
private int catchDepth = 0;
-
Analyzer(DartCompilerContext context, CoreTypeProvider typeProvider,
ConcurrentHashMap<ClassElement, List<Element>> unimplementedElements,
Set<ClassElement> diagnosedAbstractClasses) {
this.context = context;
+ this.developerModeChecks = context.getCompilerConfiguration().developerModeChecks();
this.unimplementedElements = unimplementedElements;
this.types = Types.getInstance(typeProvider);
this.dynamicType = typeProvider.getDynamicType();
@@ -435,16 +436,18 @@ public class TypeAnalyzer implements DartCompilationPhase {
return member;
}
- private void checkAssignable(DartNode node, Type t, Type s) {
+ private boolean checkAssignable(DartNode node, Type t, Type s) {
t.getClass(); // Null check.
s.getClass(); // Null check.
if (!types.isAssignable(t, s)) {
typeError(node, TypeErrorCode.TYPE_NOT_ASSIGNMENT_COMPATIBLE, s, t);
+ return false;
}
+ return true;
}
- private void checkAssignable(Type targetType, DartExpression node) {
- checkAssignable(node, targetType, nonVoidTypeOf(node));
+ private boolean checkAssignable(Type targetType, DartExpression node) {
+ return checkAssignable(node, targetType, nonVoidTypeOf(node));
}
private Type analyzeMethodInvocation(Type receiver, Member member, String name,
@@ -1066,7 +1069,11 @@ public class TypeAnalyzer implements DartCompilationPhase {
Type valueType = type.getArguments().get(1);
// Check the map literal entries against the return type.
for (DartMapLiteralEntry literalEntry : node.getEntries()) {
- checkAssignable(literalEntry, typeOf(literalEntry), valueType);
+ boolean result = checkAssignable(literalEntry, typeOf(literalEntry), valueType);
+ if (developerModeChecks == true && result == false) {
+ typeError(literalEntry, ResolverErrorCode.MAP_LITERAL_ELEMENT_TYPE,
+ valueType.toString());
+ }
}
return type;
}
@@ -1570,7 +1577,11 @@ public class TypeAnalyzer implements DartCompilationPhase {
InterfaceType type = node.getType();
Type elementType = type.getArguments().get(0);
for (DartExpression expression : node.getExpressions()) {
- checkAssignable(elementType, expression);
+ boolean result = checkAssignable(elementType, expression);
+ if (developerModeChecks == true && result == false) {
+ typeError(expression, ResolverErrorCode.LIST_LITERAL_ELEMENT_TYPE,
+ elementType.toString());
+ }
}
return type;
}

Powered by Google App Engine
This is Rietveld 408576698