Index: tests/compiler/dart2js/type_inference7_test.dart |
diff --git a/tests/compiler/dart2js/type_inference7_test.dart b/tests/compiler/dart2js/type_inference7_test.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5a3b5f5530de1a188500ee33033e338f46b26dbe |
--- /dev/null |
+++ b/tests/compiler/dart2js/type_inference7_test.dart |
@@ -0,0 +1,81 @@ |
+// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+import "package:async_helper/async_helper.dart"; |
+import "package:expect/expect.dart"; |
+import 'compiler_helper.dart'; |
+import 'type_mask_test_helper.dart'; |
+ |
+import 'dart:async'; |
+ |
+const String TEST = r""" |
+foo(x, [y]) => y; |
+ |
+main() { |
+ assert(foo("Hi", true), foo(true)); |
+ foo(1); |
+} |
+"""; |
+ |
+Future runTest() async { |
+ Uri uri = new Uri(scheme: 'source'); |
+ { |
+ // Checked mode: |
+ var compiler = compilerFor(TEST, uri, enableTypeAssertions: true); |
+ await compiler.runCompiler(uri); |
+ var typesTask = compiler.typesTask; |
+ var typesInferrer = typesTask.typesInferrer; |
+ var element = findElement(compiler, "foo"); |
+ var parameters = element.parameters; |
+ // Return type is null|bool. |
+ var mask = typesInferrer.getReturnTypeOfElement(element); |
+ Expect.isTrue(mask.isNullable); |
+ Expect.equals(typesTask.boolType, simplify(mask.nonNullable(), compiler)); |
+ // First parameter is uint31|String|bool. |
+ var mask1 = typesInferrer.getTypeOfElement(parameters[0]); |
+ Expect.isTrue(mask1.isUnion); |
+ var expectedTypes = new Set.from([typesTask.uint31Type, |
+ typesTask.stringType, |
+ typesTask.boolType]); |
+ for (var typeMask in mask1.disjointMasks) { |
+ Expect.isFalse(typeMask.isNullable); |
+ var simpleType = simplify(typeMask, compiler); |
+ Expect.isTrue(expectedTypes.remove(simpleType), "$simpleType"); |
+ } |
+ Expect.isTrue(expectedTypes.isEmpty); |
+ // Second parameter is bool or null. |
+ var mask2 = typesInferrer.getTypeOfElement(parameters[1]); |
+ Expect.isTrue(mask2.isNullable); |
+ Expect.equals(typesTask.boolType, simplify(mask2.nonNullable(), compiler)); |
+ } |
+ { |
+ // Production mode: |
+ var compiler = compilerFor(TEST, uri, enableTypeAssertions: false); |
+ await compiler.runCompiler(uri); |
+ var typesTask = compiler.typesTask; |
+ var typesInferrer = typesTask.typesInferrer; |
+ var element = findElement(compiler, "foo"); |
+ var parameters = element.parameters; |
+ // Return type is null. |
+ var mask = typesInferrer.getReturnTypeOfElement(element); |
+ Expect.isTrue(mask.isNullable); |
+ Expect.equals(typesTask.emptyType, simplify(mask.nonNullable(), compiler)); |
+ // First parameter is uint31. |
+ var mask1 = typesInferrer.getTypeOfElement(parameters[0]); |
+ Expect.isFalse(mask1.isNullable); |
+ Expect.equals(typesTask.uint31Type, simplify(mask1, compiler)); |
+ // Second parameter is null. |
+ var mask2 = typesInferrer.getTypeOfElement(parameters[1]); |
+ Expect.isTrue(mask2.isNullable); |
+ Expect.equals(typesTask.emptyType, simplify(mask2.nonNullable(), compiler)); |
+ } |
+} |
+ |
+main() { |
+ asyncStart(); |
+ runTest().then((_) { |
+ // Make sure that the type is still correct when we do a second compilation. |
+ return runTest(); |
+ }).whenComplete(asyncEnd); |
+} |