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

Unified Diff: tests/compiler/dart2js/type_inference_switch_test.dart

Issue 1439603002: Fix a bug in type inference for locals in switch statements. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 1 month 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: tests/compiler/dart2js/type_inference_switch_test.dart
diff --git a/tests/compiler/dart2js/type_inference_switch_test.dart b/tests/compiler/dart2js/type_inference_switch_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..795684cdff0417a96c1f84cde53e9e4b17ff0f39
--- /dev/null
+++ b/tests/compiler/dart2js/type_inference_switch_test.dart
@@ -0,0 +1,159 @@
+// 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 TEST1 = r"""
+foo(int x) {
+ var a = "one";
+ switch (x) {
+ case 1:
+ a = "two";
+ break;
+ case 2:
+ break;
+ }
+
+ return a;
+}
+
+main() {
+ foo(new DateTime.now().millisecondsSinceEpoch);
+}
+""";
+
+const String TEST2 = r"""
+foo(int x) {
+ var a;
+ switch (x) {
+ case 1:
+ a = "two";
+ break;
+ case 2:
+ break;
+ }
+
+ return a;
+}
+
+main() {
+ foo(new DateTime.now().millisecondsSinceEpoch);
+}
+""";
+
+const String TEST3 = r"""
+foo(int x) {
+ var a;
+ switch (x) {
+ case 1:
+ a = 1;
+ case 2: // illegal fall through
+ a = 2;
+ break;
+ }
+
+ return a;
+}
+
+main() {
+ foo(new DateTime.now().millisecondsSinceEpoch);
+}
+""";
+
+const String TEST4 = r"""
+foo(int x) {
+ var a;
+ switch (x) {
+ case 1:
+ a = 1;
+ case 2: // illegal fall through
+ a = 2;
+ break;
+ default:
+ a = 0;
+ }
+
+ return a;
+}
+
+main() {
+ foo(new DateTime.now().millisecondsSinceEpoch);
+}
+""";
+
+const String TEST5 = r"""
+foo(int x) {
+ var a;
+ switch (x) {
+ case 1:
+ a = 1;
+ break;
+ case 2:
+ a = 2;
+ break;
+ default:
+ }
+
+ return a;
+}
+
+main() {
+ foo(new DateTime.now().millisecondsSinceEpoch);
+}
+""";
+
+const String TEST6 = r"""
+foo(int x) {
+ var a;
+ do { // add extra locals scope
+ switch (x) {
+ case 1:
+ a = 1;
+ break;
+ case 2:
+ a = 2;
+ break;
+ }
+ } while (false);
+
+ return a;
+}
+
+main() {
+ foo(new DateTime.now().millisecondsSinceEpoch);
+}
+""";
+
+Future runTest(String test, checker) {
+ Uri uri = new Uri(scheme: 'source');
+ var compiler = compilerFor(test, uri);
+
+ checkTypeOf(String name, TypeMask type) {
+ var typesTask = compiler.typesTask;
+ var typesInferrer = typesTask.typesInferrer;
+ var element = findElement(compiler, name);
+ var mask = typesInferrer.getReturnTypeOfElement(element);
+ Expect.equals(type, simplify(mask, compiler));
+ }
+
+ return compiler.run(uri).then((_) {
+ checker(compiler.typesTask, checkTypeOf);
+ });
+}
+
+main() {
+ asyncTest(() async {
+ await runTest(TEST1, (t, c) => c("foo", t.stringType));
+ await runTest(TEST2, (t, c) => c("foo", t.stringType.nullable()));
+ await runTest(TEST3, (t, c) => c("foo", t.uint31Type.nullable()));
+ await runTest(TEST4, (t, c) => c("foo", t.uint31Type));
+ await runTest(TEST5, (t, c) => c("foo", t.uint31Type.nullable()));
+ await runTest(TEST6, (t, c) => c("foo", t.uint31Type.nullable()));
+ });
+}

Powered by Google App Engine
This is Rietveld 408576698