changeset 55901:850fab702c3d switch2

Adding test to verify definite assignment rules for swich statements.
author jlahoda
date Mon, 29 Apr 2019 14:32:32 +0200
parents 07493e0a0be6
children 0689af638ab6
files test/langtools/tools/javac/switchextra/DefiniteAssignment1.java test/langtools/tools/javac/switchextra/DefiniteAssignment2.java test/langtools/tools/javac/switchextra/DefiniteAssignment2.out
diffstat 3 files changed, 202 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/switchextra/DefiniteAssignment1.java	Mon Apr 29 14:32:32 2019 +0200
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @summary Verify that definite assignment works (legal code)
+ * @compile --enable-preview -source ${jdk.version} DefiniteAssignment1.java
+ * @run main/othervm --enable-preview DefiniteAssignment1
+ */
+public class DefiniteAssignment1 {
+    public static void main(String[] args) {
+        int a = 0;
+
+        {
+        int x;
+
+        switch(a) {
+            case 0: x = 0; break;
+            default: x = 1; break;
+        }
+
+        if (x != 0)
+            throw new IllegalStateException("Unexpected value.");
+        }
+
+        {
+        int x;
+
+        switch(a) {
+            case 1: x = 1; break;
+            case 0:
+            default: x = 0; break;
+        }
+
+        if (x != 0)
+            throw new IllegalStateException("Unexpected value.");
+        }
+
+        {
+        int x;
+
+        switch(a) {
+            case 1: x = 1; break;
+            case 0:
+            default: x = 0;
+        }
+
+        if (x != 0)
+            throw new IllegalStateException("Unexpected value.");
+        }
+
+        {
+        int x;
+
+        switch(a) {
+            case 0 -> x = 0;
+            default -> x = 1;
+        }
+
+        if (x != 0)
+            throw new IllegalStateException("Unexpected value.");
+        }
+
+        {
+        int x;
+
+        switch(a) {
+            case 1: x = 1; break;
+            case 0:
+            default: throw new IllegalStateException();
+        }
+
+        if (x != 0)
+            throw new IllegalStateException("Unexpected value.");
+        }
+
+        {
+        int x;
+
+        switch(a) {
+            case 0 -> x = 0;
+            default -> throw new IllegalStateException();
+        }
+
+        if (x != 0)
+            throw new IllegalStateException("Unexpected value.");
+        }
+    }
+
+    enum E {
+        A, B, C;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/switchextra/DefiniteAssignment2.java	Mon Apr 29 14:32:32 2019 +0200
@@ -0,0 +1,80 @@
+/**
+ * @test /nodynamiccopyright/
+ * @summary Verify that definite assignment works (illegal code)
+ * @compile/fail/ref=DefiniteAssignment2.out -XDrawDiagnostics --enable-preview -source ${jdk.version} DefiniteAssignment2.java
+ */
+public class DefiniteAssignment2 {
+
+    public static void main(String[] args) {
+        int a = 0;
+        E e = E.A;
+
+        {
+        int x;
+
+        switch(a) {
+            case 0: break;
+            default: x = 1; break;
+        }
+
+        System.err.println(x);
+        }
+
+        {
+        int x;
+
+        switch(a) {
+            case 0 -> {}
+            default -> x = 1;
+        }
+
+        System.err.println(x);
+        }
+
+        {
+        int x;
+
+        switch(a) {
+            case 0: x = 0; break;
+            default:
+        }
+
+        System.err.println(x);
+        }
+
+        {
+        int x;
+
+        switch(e) {
+            case A, B, C -> x = 0;
+        }
+
+        System.err.println(x);
+        }
+
+        {
+        int x;
+
+        switch(e) {
+            case A, B, C -> { x = 0; }
+        }
+
+        System.err.println(x);
+        }
+
+        {
+        int x;
+
+        switch(e) {
+            case A, B -> { x = 0; }
+            case C -> throw new IllegalStateException();
+        }
+
+        System.err.println(x);
+        }
+    }
+
+    enum E {
+        A, B, C;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/switchextra/DefiniteAssignment2.out	Mon Apr 29 14:32:32 2019 +0200
@@ -0,0 +1,9 @@
+DefiniteAssignment2.java:20:28: compiler.err.var.might.not.have.been.initialized: x
+DefiniteAssignment2.java:31:28: compiler.err.var.might.not.have.been.initialized: x
+DefiniteAssignment2.java:42:28: compiler.err.var.might.not.have.been.initialized: x
+DefiniteAssignment2.java:52:28: compiler.err.var.might.not.have.been.initialized: x
+DefiniteAssignment2.java:62:28: compiler.err.var.might.not.have.been.initialized: x
+DefiniteAssignment2.java:73:28: compiler.err.var.might.not.have.been.initialized: x
+- compiler.note.preview.filename: DefiniteAssignment2.java
+- compiler.note.preview.recompile
+6 errors