From 1217a48d2a7219062c6f3ecef448a3db9cfc19e4 Mon Sep 17 00:00:00 2001 From: "scheglov@google.com" Date: Tue, 28 Aug 2012 13:20:29 +0000 Subject: [PATCH] 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 --- .../dart/compiler/type/TypeAnalyzer.java | 6 ++++-- .../dart/compiler/CompilerTestCase.java | 5 ++++- .../type/TypeAnalyzerCompilerTest.java | 20 +++++++++++++++++-- .../dart/compiler/type/TypeAnalyzerTest.java | 2 +- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java b/compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java index 457941fb91d..1f9124c7c6d 100644 --- a/compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java +++ b/compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java @@ -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; diff --git a/compiler/javatests/com/google/dart/compiler/CompilerTestCase.java b/compiler/javatests/com/google/dart/compiler/CompilerTestCase.java index bd143952515..e5388c61139 100644 --- a/compiler/javatests/com/google/dart/compiler/CompilerTestCase.java +++ b/compiler/javatests/com/google/dart/compiler/CompilerTestCase.java @@ -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; } diff --git a/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerCompilerTest.java b/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerCompilerTest.java index 1be1d5d55f4..85edda5f888 100644 --- a/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerCompilerTest.java +++ b/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerCompilerTest.java @@ -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"); } 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"); +} 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() { + 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) {", diff --git a/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerTest.java b/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerTest.java index 1ba37a03e47..798fe521332 100644 --- a/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerTest.java +++ b/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerTest.java @@ -55,7 +55,7 @@ public class TypeAnalyzerTest extends TypeAnalyzerTestCase { analyze("List strings = ['x'];"); analyze("List array = ['x'];"); analyze("List array = ['x'];"); - analyzeFail("List ints = ['x'];", TypeErrorCode.TYPE_NOT_ASSIGNMENT_COMPATIBLE); + analyze("List ints = ['x'];"); analyzeFail("List ints = ['x'];", TypeErrorCode.TYPE_NOT_ASSIGNMENT_COMPATIBLE); }