Issue 4719. Mark inferred List/Map literal types as inferred.
http://code.google.com/p/dart/issues/detail?id=4719 R=brianwilkerson@google.com BUG= Review URL: https://chromiumcodereview.appspot.com//10878088 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@11441 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
@@ -1867,7 +1867,8 @@ public class TypeAnalyzer implements DartCompilationPhase {
|
||||
}
|
||||
Type valueType = types.intersection(valueTypes);
|
||||
valueType = Types.makeInferred(valueType);
|
||||
return typeProvider.getMapLiteralType(stringType, valueType);
|
||||
InterfaceType mapLiteralType = typeProvider.getMapLiteralType(stringType, valueType);
|
||||
return Types.makeInferred(mapLiteralType);
|
||||
}
|
||||
|
||||
return type;
|
||||
@@ -2717,7 +2718,8 @@ public class TypeAnalyzer implements DartCompilationPhase {
|
||||
}
|
||||
Type elementType = types.intersection(elementTypes);
|
||||
elementType = Types.makeInferred(elementType);
|
||||
return typeProvider.getArrayLiteralType(elementType);
|
||||
InterfaceType arrayLiteralType = typeProvider.getArrayLiteralType(elementType);
|
||||
return Types.makeInferred(arrayLiteralType);
|
||||
}
|
||||
// done
|
||||
return interfaceType;
|
||||
|
||||
@@ -42,6 +42,7 @@ public abstract class CompilerTestCase extends TestCase {
|
||||
|
||||
private static final String UTF8 = "UTF-8";
|
||||
protected CompilerConfiguration compilerConfiguration;
|
||||
protected String testSource;
|
||||
protected DartUnit testUnit;
|
||||
|
||||
/**
|
||||
@@ -179,13 +180,15 @@ public abstract class CompilerTestCase extends TestCase {
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
compilerConfiguration = null;
|
||||
testSource = null;
|
||||
testUnit = null;
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
protected AnalyzeLibraryResult analyzeLibrary(String... lines) throws Exception {
|
||||
String name = getName();
|
||||
AnalyzeLibraryResult libraryResult = analyzeLibrary(name, makeCode(lines));
|
||||
testSource = makeCode(lines);
|
||||
AnalyzeLibraryResult libraryResult = analyzeLibrary(name, testSource);
|
||||
testUnit = libraryResult.getLibraryUnitResult().getUnit(name);
|
||||
return libraryResult;
|
||||
}
|
||||
|
||||
@@ -2778,7 +2778,7 @@ public class TypeAnalyzerCompilerTest extends CompilerTestCase {
|
||||
}
|
||||
|
||||
public void test_typesPropagation_arrayLiteral_singleType() throws Exception {
|
||||
AnalyzeLibraryResult libraryResult = analyzeLibrary(
|
||||
final AnalyzeLibraryResult libraryResult = analyzeLibrary(
|
||||
"// filler filler filler filler filler filler filler filler filler filler",
|
||||
"main() {",
|
||||
" var a = [1, 2, 3];",
|
||||
@@ -2787,6 +2787,7 @@ public class TypeAnalyzerCompilerTest extends CompilerTestCase {
|
||||
"");
|
||||
assertErrors(libraryResult.getErrors());
|
||||
assertInferredElementTypeString(testUnit, "v", "int");
|
||||
assertNodeInferredTypeString("[1, 2, 3]", "List<int>");
|
||||
}
|
||||
|
||||
public void test_typesPropagation_arrayLiteral_mixedTypes() throws Exception {
|
||||
@@ -2811,7 +2812,8 @@ public class TypeAnalyzerCompilerTest extends CompilerTestCase {
|
||||
"");
|
||||
assertErrors(libraryResult.getErrors());
|
||||
assertInferredElementTypeString(testUnit, "v", "int");
|
||||
}
|
||||
assertNodeInferredTypeString("{'1': 1, ", "Map<String, int>");
|
||||
}
|
||||
|
||||
public void test_typesPropagation_mapLiteral_mixedTypes() throws Exception {
|
||||
AnalyzeLibraryResult libraryResult = analyzeLibrary(
|
||||
@@ -2825,6 +2827,20 @@ public class TypeAnalyzerCompilerTest extends CompilerTestCase {
|
||||
assertInferredElementTypeString(testUnit, "v", "num");
|
||||
}
|
||||
|
||||
private void assertNodeInferredTypeString(final String prefix, final String expectedType) {
|
||||
testUnit.accept(new ASTVisitor<Void>() {
|
||||
public Void visitExpression(DartExpression node) {
|
||||
int nodeOffset = node.getSourceInfo().getOffset();
|
||||
if (testSource.substring(nodeOffset).startsWith(prefix)) {
|
||||
Type actualType = node.getType();
|
||||
assertEquals(expectedType, actualType.toString());
|
||||
assertTrue(actualType.isInferred());
|
||||
}
|
||||
return super.visitNode(node);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void test_getType_binaryExpression() throws Exception {
|
||||
analyzeLibrary(
|
||||
"f(var arg) {",
|
||||
|
||||
@@ -55,7 +55,7 @@ public class TypeAnalyzerTest extends TypeAnalyzerTestCase {
|
||||
analyze("List<String> strings = <String>['x'];");
|
||||
analyze("List array = ['x'];");
|
||||
analyze("List array = <String>['x'];");
|
||||
analyzeFail("List<int> ints = ['x'];", TypeErrorCode.TYPE_NOT_ASSIGNMENT_COMPATIBLE);
|
||||
analyze("List<int> ints = ['x'];");
|
||||
analyzeFail("List<int> ints = <String>['x'];", TypeErrorCode.TYPE_NOT_ASSIGNMENT_COMPATIBLE);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user