test/langtools/tools/javac/switchexpr/WarnWrongYieldTest.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 55573 e709712a8188
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
     1
/*
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
     4
 *
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
     8
 *
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    14
 *
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    18
 *
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    21
 * questions.
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    22
 */
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    23
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    24
/*
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    25
 * @test
55561
4c0a7916d3cd 8226522: No compilation error reported when yield is used in incorrect context
jlahoda
parents: 55306
diff changeset
    26
 * @bug 8223305 8226522
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    27
 * @summary Verify correct warnings w.r.t. yield
55382
30b1b7b4dd86 8214546: Start of release updates for JDK 14
darcy
parents: 55306
diff changeset
    28
 * @compile/ref=WarnWrongYieldTest.out -source ${jdk.version} -XDrawDiagnostics -XDshould-stop.at=ATTR WarnWrongYieldTest.java
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    29
 */
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    30
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    31
package t;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    32
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    33
//ERROR - type called yield:
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    34
import t.WarnWrongYieldTest.yield;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    35
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    36
public class WarnWrongYieldTest {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    37
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    38
    // ERROR -  class called yield
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    39
    class yield { }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    40
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    41
    // OK to have fields called yield
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    42
    String[] yield = null;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    43
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    44
    // ERROR - field of type yield
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    45
    yield y;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    46
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    47
    // OK to have methods called yield
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    48
    // Nullary yield method
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    49
    String[] yield() {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    50
        return null;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    51
    }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    52
    // Unary yield method
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    53
    String[] yield(int i) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    54
        return null;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    55
    }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    56
    // Binary yield method
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    57
    String[] yield(int i, int j) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    58
        return null;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    59
    }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    60
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    61
    // OK to declare a local called yield
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    62
    void LocalDeclaration1() {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    63
       int yield;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    64
    }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    65
    // OK to declare and initialise a local called yield
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    66
    void LocalDeclaration2() {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    67
        int yield = 42;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    68
    }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    69
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    70
    void YieldTypedLocals(int i) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    71
        // ERROR - Parsed as yield statement, and y1 is unknown
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    72
        yield y1 = null;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    73
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    74
        // ERROR - Parsed as yield statement, and y2 is unknown
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    75
        yield y2 = new yield();
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    76
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    77
        // ERROR - can not create an yield-valued local of type Object
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    78
        Object y3 = new yield();
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    79
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    80
        // ERROR - can not create a final yield-valued local of type yield
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    81
        final yield y4 = new yield();
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    82
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    83
        // ERROR - can create a non-final local of type yield using qualified typename
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    84
        WarnWrongYieldTest.yield y5 = new yield();
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    85
    }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    86
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    87
    void MethodInvocation(int i) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    88
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    89
        // OK - can access a field called yield
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    90
        String[] x = this.yield;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    91
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    92
        // ERROR - calling nullary yield method using simple name parsed as yield statement
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    93
        yield();
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    94
        // OK - can call nullary yield method using qualified name
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    95
        this.yield();
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    96
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    97
        // ERROR - Calling unary yield method using simple name is parsed as yield statement
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    98
        yield(2);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
    99
        // OK - calling unary yield method using qualified name
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   100
        this.yield(2);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   101
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   102
        // ERROR - Calling binary yield method using simple name is parsed as yield statement
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   103
        yield(2, 2); //error
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   104
        // OK - calling binary yield method using qualified name
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   105
        this.yield(2, 2);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   106
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   107
        // ERROR - nullary yield method as receiver is parsed as yield statement
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   108
        yield().toString();
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   109
        // OK - nullary yield method as receiver using qualified name
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   110
        this.yield().toString();
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   111
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   112
        // ERROR - unary yield method as receiver is parsed as yield statement
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   113
        yield(2).toString();
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   114
        // OK - unary yield method as receiver using qualified name
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   115
        this.yield(2).toString();
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   116
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   117
        // ERROR - binary yield method as receiver is parsed as yield statement
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   118
        yield(2, 2).toString();
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   119
        // OK - binary yield method as receiver using qualified name
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   120
        this.yield(2, 2).toString();
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   121
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   122
        // OK - yield method call is in an expression position
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   123
        String str = yield(2).toString();
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   124
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   125
        //OK - yield is a variable
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   126
        yield.toString();
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   127
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   128
        // OK - parsed as method call (with qualified local yield as receiver)
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   129
        this.yield.toString();
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   130
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   131
        yield[0].toString(); //error
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   132
    }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   133
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   134
    private void yieldLocalVar1(int i) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   135
        int yield = 0;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   136
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   137
        //OK - yield is a variable:
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   138
        yield++;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   139
        yield--;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   140
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   141
        //OK - yield is a variable:
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   142
        yield = 3;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   143
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   144
        //OK - yield is a variable:
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   145
        for (int j = 0; j < 3; j++)
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   146
            yield += 1;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   147
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   148
        //OK - yield is a variable and not at the beginning of the statement:
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   149
        yieldLocalVar1(yield);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   150
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   151
        //ERROR - unqualified yield method invocation:
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   152
        yieldLocalVar1(yield().length);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   153
        yieldLocalVar1(yield.class.getModifiers());
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   154
    }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   155
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   156
    private void yieldLocalVar2(int i) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   157
        int[] yield = new int[1];
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   158
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   159
        //OK - yield is a variable:
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   160
        yield[0] = 5;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   161
    }
55561
4c0a7916d3cd 8226522: No compilation error reported when yield is used in incorrect context
jlahoda
parents: 55306
diff changeset
   162
4c0a7916d3cd 8226522: No compilation error reported when yield is used in incorrect context
jlahoda
parents: 55306
diff changeset
   163
    private void lambda() {
4c0a7916d3cd 8226522: No compilation error reported when yield is used in incorrect context
jlahoda
parents: 55306
diff changeset
   164
        SAM s = (yield y) -> {};
4c0a7916d3cd 8226522: No compilation error reported when yield is used in incorrect context
jlahoda
parents: 55306
diff changeset
   165
    }
4c0a7916d3cd 8226522: No compilation error reported when yield is used in incorrect context
jlahoda
parents: 55306
diff changeset
   166
4c0a7916d3cd 8226522: No compilation error reported when yield is used in incorrect context
jlahoda
parents: 55306
diff changeset
   167
    interface SAM {
4c0a7916d3cd 8226522: No compilation error reported when yield is used in incorrect context
jlahoda
parents: 55306
diff changeset
   168
        public void m(yield o);
4c0a7916d3cd 8226522: No compilation error reported when yield is used in incorrect context
jlahoda
parents: 55306
diff changeset
   169
    }
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents:
diff changeset
   170
}