test/langtools/tools/javac/switchexpr/SwitchExpressionIsNotAConstant.java
author jlahoda
Tue, 12 Nov 2019 06:32:13 +0000
changeset 59021 cfc7bb9a5a92
parent 55306 ea43db53de91
permissions -rw-r--r--
8232684: Make switch expressions final Reviewed-by: alanb, mcimadamore, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52679
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
     1
/*
53878
4584d0331318 8219254: Update explicit uses of latest source/target in langtools tests to a property
darcy
parents: 53023
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
52679
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
     4
 *
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
     8
 *
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    14
 *
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    18
 *
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    21
 * questions.
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    22
 */
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    23
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    24
/**
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    25
 * @test
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    26
 * @bug 8214113
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    27
 * @summary Verify the switch expression's type does not have a constant attached,
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    28
 *          and so the switch expression is not elided.
59021
cfc7bb9a5a92 8232684: Make switch expressions final
jlahoda
parents: 55306
diff changeset
    29
 * @compile SwitchExpressionIsNotAConstant.java
cfc7bb9a5a92 8232684: Make switch expressions final
jlahoda
parents: 55306
diff changeset
    30
 * @run main SwitchExpressionIsNotAConstant
52679
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    31
 */
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    32
public class SwitchExpressionIsNotAConstant {
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    33
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    34
    public static void main(String[] args) {
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    35
        int i = 0;
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    36
        {
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    37
            i = 0;
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    38
            int dummy = 1 + switch (i) {
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    39
                default -> {
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    40
                    i++;
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
    41
                    yield 1;
52679
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    42
                }
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    43
            };
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    44
            if (i != 1) {
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    45
                throw new IllegalStateException("Side effects missing.");
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    46
            }
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    47
        }
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    48
        {
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    49
            i = 0;
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    50
            int dummy = 1 + switch (i) {
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    51
                case -1 -> 1;
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    52
                default -> {
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    53
                    i++;
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
    54
                    yield 1;
52679
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    55
                }
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    56
            };
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    57
            if (i != 1) {
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    58
                throw new IllegalStateException("Side effects missing.");
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    59
            }
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    60
        }
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    61
        {
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    62
            i = 0;
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    63
            int dummy = 1 + switch (i) {
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    64
                 default :
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    65
                    i++;
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
    66
                    yield 1;
52679
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    67
            };
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    68
            if (i != 1) {
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    69
                throw new IllegalStateException("Side effects missing.");
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    70
            }
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    71
        }
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    72
        {
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    73
            i = 0;
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    74
            int dummy = 1 + switch (i) {
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
    75
                case -1: yield 1;
52679
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    76
                default:
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    77
                    i++;
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53878
diff changeset
    78
                    yield 1;
52679
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    79
            };
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    80
            if (i != 1) {
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    81
                throw new IllegalStateException("Side effects missing.");
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    82
            }
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    83
        }
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    84
    }
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    85
c6df6671267e 8214113: Switch expressions may have constant type and may be skipped during write
jlahoda
parents:
diff changeset
    86
}