1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
/*
* @test /nodynamiccopyright/
* @bug 8223305 8226522
* @summary Ensure proper errors are returned for yields.
* @compile/fail/ref=WrongYieldTest.out -XDrawDiagnostics -XDshould-stop.ifError=ATTR -XDshould-stop.ifNoError=ATTR WrongYieldTest.java
*/
package t;
//ERROR - type called yield:
import t.WrongYieldTest.yield;
public class WrongYieldTest {
// ERROR - class called yield
class yield { }
// OK to have fields called yield
String[] yield = null;
// ERROR - field of type yield
yield y;
// OK to have methods called yield
// Nullary yield method
String[] yield() {
return null;
}
// Unary yield method
String[] yield(int i) {
return null;
}
// Binary yield method
String[] yield(int i, int j) {
return null;
}
// OK to declare a local called yield
void LocalDeclaration1() {
int yield;
}
// OK to declare and initialise a local called yield
void LocalDeclaration2() {
int yield = 42;
}
// ERROR can't refer to a local called yield in the initialiser
void LocalDeclaration3() {
int yield = yield + 1;
}
// OK yield gets interpreted as an identifier in a local declaration
void LocalDeclaration4(int i) {
int local = switch (i) {
default -> {
int yield = yield + 1;
yield 42;
}
};
}
// OK - yield a local called yield
void LocalDeclaration5(int i) {
int yield = 42;
int temp = switch (i) {
default -> {
yield yield;
}
};
}
void YieldTypedLocals(int i) {
// ERROR - Parsed as yield statement, and y1 is unknown
yield y1 = null;
// ..whereas..
// ERROR - parsed as yield statement, which has no switch target
Object y1;
yield y1 = null;
// ERROR - Parsed as yield statement, and y2 is unknown
yield y2 = new yield();
// OK - Parsed as yield statement that assigns local y
Object y;
Object o = switch (i) {
default :
yield y = null;
};
// ERROR - Parsed as yield statement that assigns local y,
//but the initializer refers to restricted identifier:
Object y2;
Object o2 = switch (i) {
default :
yield y2 = new yield();
};
// ERROR - can not create an yield-valued local of type Object
Object y3 = new yield();
// ERROR - can not create a final yield-valued local of type yield
final yield y4 = new yield();
// ERROR - can create a non-final local of type yield using qualified typename
WrongYieldTest.yield y5 = new yield();
}
void MethodInvocation(int i) {
// OK - can access a field called yield
String[] x = this.yield;
// ERROR - calling nullary yield method using simple name parsed as yield statement
yield();
// OK - can call nullary yield method using qualified name
this.yield();
// ERROR - Calling unary yield method using simple name is parsed as yield statement
yield(2);
// OK - calling unary yield method using qualified name
this.yield(2);
// ERROR - Calling binary yield method using simple name is parsed as yield statement
yield(2, 2); //error
// OK - calling binary yield method using qualified name
this.yield(2, 2);
// ERROR - nullary yield method as receiver is parsed as yield statement
yield().toString();
// OK - nullary yield method as receiver using qualified name
this.yield().toString();
// ERROR - unary yield method as receiver is parsed as yield statement
yield(2).toString();
// OK - unary yield method as receiver using qualified name
this.yield(2).toString();
// ERROR - binary yield method as receiver is parsed as yield statement
yield(2, 2).toString();
// OK - binary yield method as receiver using qualified name
this.yield(2, 2).toString();
// OK - yield method call is in an expression position
String str = yield(2).toString();
//OK - yield is a variable
yield.toString();
// OK - parsed as method call (with qualified local yield as receiver)
this.yield.toString();
yield[0].toString(); //error
// OK - parsed as yield statement in switch expression
int j = switch (i) {
default:
yield(2);
};
// ERROR - second yield is an unreachable statement.
x = switch (i) {
default: {
yield x = null;
yield null;
}
};
}
private void yieldLocalVar1(int i) {
int yield = 0;
//OK - yield is a variable:
yield++;
yield--;
//ERROR - yield is a statement, but no enclosing switch expr:
yield ++i;
yield --i;
//OK - yield is a variable:
yield = 3;
//OK - yield is a variable:
for (int j = 0; j < 3; j++)
yield += 1;
//OK - yield is a variable and not at the beginning of the statement:
yieldLocalVar1(yield);
//ERROR - unqualified yield method invocation:
yieldLocalVar1(yield().length);
yieldLocalVar1(yield.class.getModifiers());
}
private void yieldLocalVar2(int i) {
int[] yield = new int[1];
//OK - yield is a variable:
yield[0] = 5;
}
private void lambda() {
SAM s = (yield y) -> {};
}
interface SAM {
public void m(Object o);
}
}
|