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

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

Issue 10869060: Infer type of List and Map literals (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 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
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 700c08723921ce6f348ee43a9e89a210ce4f8d0d..b30a25f3ab72da808e0ffd6b4c7751c601a3ff84 100644
--- a/compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java
+++ b/compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java
@@ -20,8 +20,8 @@ import com.google.dart.compiler.DartCompilationPhase;
import com.google.dart.compiler.DartCompilerContext;
import com.google.dart.compiler.ErrorCode;
import com.google.dart.compiler.ErrorSeverity;
-import com.google.dart.compiler.Source;
import com.google.dart.compiler.PackageLibraryManager;
+import com.google.dart.compiler.Source;
import com.google.dart.compiler.ast.ASTVisitor;
import com.google.dart.compiler.ast.DartArrayAccess;
import com.google.dart.compiler.ast.DartArrayLiteral;
@@ -163,6 +163,7 @@ public class TypeAnalyzer implements DartCompilationPhase {
@VisibleForTesting
static class Analyzer extends ASTVisitor<Type> {
+ private final CoreTypeProvider typeProvider;
private final DynamicType dynamicType;
private final Type stringType;
private final InterfaceType defaultLiteralMapType;
@@ -199,6 +200,7 @@ public class TypeAnalyzer implements DartCompilationPhase {
Analyzer(DartCompilerContext context, CoreTypeProvider typeProvider,
Set<ClassElement> diagnosedAbstractClasses) {
this.context = context;
+ this.typeProvider = typeProvider;
this.developerModeChecks = context.getCompilerConfiguration().developerModeChecks();
this.types = Types.getInstance(typeProvider);
this.dynamicType = typeProvider.getDynamicType();
@@ -1826,11 +1828,13 @@ public class TypeAnalyzer implements DartCompilationPhase {
checkAssignable(node, defaultLiteralMapType, type);
// Check the map literal entries against the return type.
- Type valueType = type.getArguments().get(1);
- for (DartMapLiteralEntry literalEntry : node.getEntries()) {
- boolean isValueAssignable = checkAssignable(literalEntry, typeOf(literalEntry), valueType);
- if (developerModeChecks && !isValueAssignable) {
- typeError(literalEntry, ResolverErrorCode.MAP_LITERAL_ELEMENT_TYPE, valueType);
+ {
+ Type valueType = type.getArguments().get(1);
+ for (DartMapLiteralEntry literalEntry : node.getEntries()) {
+ boolean isValueAssignable = checkAssignable(literalEntry, typeOf(literalEntry), valueType);
+ if (developerModeChecks && !isValueAssignable) {
+ typeError(literalEntry, ResolverErrorCode.MAP_LITERAL_ELEMENT_TYPE, valueType);
+ }
}
}
@@ -1847,6 +1851,23 @@ public class TypeAnalyzer implements DartCompilationPhase {
}
}
+ // no type arguments, try to infer
+ if (node.getTypeArguments().isEmpty()) {
+ List<Type> valueTypes = Lists.newArrayList();
+ for (DartMapLiteralEntry literalEntry : node.getEntries()) {
+ DartExpression value = literalEntry.getValue();
+ if (value != null) {
+ Type valueType = typeOf(value);
+ if (valueType != null ) {
+ valueTypes.add(valueType);
+ }
+ }
+ }
+ Type valueType = types.intersection(valueTypes);
+ valueType = Types.makeInferred(valueType);
+ return typeProvider.getMapLiteralType(stringType, valueType);
+ }
+
return type;
}
@@ -2681,6 +2702,20 @@ public class TypeAnalyzer implements DartCompilationPhase {
}
}
}
+ // no type arguments, try to infer
+ if (node.getTypeArguments().isEmpty()) {
+ List<Type> elementTypes = Lists.newArrayList();
+ for (DartExpression expression : node.getExpressions()) {
+ Type elementType = expression.getType();
+ if (elementType != null ) {
+ elementTypes.add(elementType);
+ }
+ }
+ Type elementType = types.intersection(elementTypes);
+ elementType = Types.makeInferred(elementType);
+ return typeProvider.getArrayLiteralType(elementType);
+ }
+ // done
return interfaceType;
}

Powered by Google App Engine
This is Rietveld 408576698