test/langtools/tools/javac/switchexpr/ExpressionSwitchBugs.java
author darcy
Thu, 13 Dec 2018 19:06:11 +0100
changeset 53023 6879069d9d94
parent 52936 9745e4e36dd1
child 53878 4584d0331318
permissions -rw-r--r--
8205626: Start of release updates for JDK 13 8205393: Add SourceVersion.RELEASE_13 8205394: Add source 13 and target 13 to javac 8205645: Bump maximum recognized class file version to 57 for JDK 13 8214825: Update preview language features for start of JDK 13 Reviewed-by: erikj, alanb, mchung, mcimadamore, dholmes, smarks, jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     1
/*
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     4
 *
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     8
 *
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    14
 *
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    18
 *
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    21
 * questions.
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    22
 */
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    23
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    24
/*
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    25
 * @test
52936
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
    26
 * @bug 8206986 8214114 8214529
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    27
 * @summary Verify various corner cases with nested switch expressions.
53023
6879069d9d94 8205626: Start of release updates for JDK 13
darcy
parents: 52936
diff changeset
    28
 * @compile --enable-preview -source 13 ExpressionSwitchBugs.java
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    29
 * @run main/othervm --enable-preview ExpressionSwitchBugs
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    30
 */
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    31
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    32
public class ExpressionSwitchBugs {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    33
    public static void main(String... args) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    34
        new ExpressionSwitchBugs().testNested();
52796
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
    35
        new ExpressionSwitchBugs().testAnonymousClasses();
52936
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
    36
        new ExpressionSwitchBugs().testFields();
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    37
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    38
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    39
    private void testNested() {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    40
        int i = 0;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    41
        check(42, id(switch (42) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    42
            default: i++; break 42;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    43
        }));
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    44
        i = 0;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    45
        check(43, id(switch (42) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    46
            case 42: while (i == 0) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    47
                i++;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    48
            }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    49
            break 42 + i;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    50
            default: i++; break 42;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    51
        }));
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    52
        i = 0;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    53
        check(42, id(switch (42) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    54
            case 42: if (i == 0) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    55
                break 42;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    56
            }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    57
            default: i++; break 43;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    58
        }));
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    59
        i = 0;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    60
        check(42, id(switch (42) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    61
            case 42: if (i == 0) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    62
                break 41 + switch (0) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    63
                    case 0 -> 1;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    64
                    default -> -1;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    65
                };
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    66
            }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    67
            default: i++; break 43;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    68
        }));
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    69
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
    70
52796
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
    71
    private void testAnonymousClasses() {
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
    72
        for (int i : new int[] {1, 2}) {
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
    73
            check(3, id((switch (i) {
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
    74
                case 1: break new I() {
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
    75
                    public int g() { return 3; }
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
    76
                };
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
    77
                default: break (I) () -> { return 3; };
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
    78
            }).g()));
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
    79
            check(3, id((switch (i) {
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
    80
                case 1 -> new I() {
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
    81
                    public int g() { return 3; }
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
    82
                };
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
    83
                default -> (I) () -> { return 3; };
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
    84
            }).g()));
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
    85
        }
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
    86
    }
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
    87
52936
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
    88
    private void testFields() {
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
    89
        check(3, field);
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
    90
        check(3, ExpressionSwitchBugs.staticField);
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
    91
    }
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
    92
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
    93
    private final int value = 2;
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
    94
    private final int field = id(switch(value) {
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
    95
        case 0 -> -1;
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
    96
        case 2 -> {
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
    97
            int temp = 0;
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
    98
            temp += 3;
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
    99
            break temp;
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
   100
        }
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
   101
        default -> throw new IllegalStateException();
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
   102
    });
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
   103
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
   104
    private static final int staticValue = 2;
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
   105
    private static final int staticField = new ExpressionSwitchBugs().id(switch(staticValue) {
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
   106
        case 0 -> -1;
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
   107
        case 2 -> {
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
   108
            int temp = 0;
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
   109
            temp += 3;
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
   110
            break temp;
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
   111
        }
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
   112
        default -> throw new IllegalStateException();
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
   113
    });
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52796
diff changeset
   114
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   115
    private int id(int i) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   116
        return i;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   117
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   118
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   119
    private int id(Object o) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   120
        return -1;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   121
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   122
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   123
    private void check(int actual, int expected) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   124
        if (actual != expected) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   125
            throw new AssertionError("Unexpected result: " + actual);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   126
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   127
    }
52796
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
   128
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
   129
    public interface I {
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
   130
        public int g();
2c8e6decb1c3 8214529: Exception while using Anonymous class in switch expression
jlahoda
parents: 51563
diff changeset
   131
    }
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents:
diff changeset
   132
}