Skip to content

Commit bca914b

Browse files
lgxbslgxVicente Romero
authored andcommitted
8268670: yield statements doesn't allow ~ or ! unary operators in expression
Reviewed-by: vromero, jlahoda
1 parent c088d09 commit bca914b

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,6 +2668,9 @@ List<JCStatement> blockStatement() {
26682668
case PLUSPLUS: case SUBSUB:
26692669
isYieldStatement = S.token(2).kind != SEMI;
26702670
break;
2671+
case BANG: case TILDE:
2672+
isYieldStatement = S.token(1).kind != SEMI;
2673+
break;
26712674
case LPAREN:
26722675
int lookahead = 2;
26732676
int balance = 1;

test/langtools/tools/javac/switchexpr/ExpressionSwitch.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* @test /nodynamiccopyright/
3-
* @bug 8206986 8222169 8224031 8240964 8267119
3+
* @bug 8206986 8222169 8224031 8240964 8267119 8268670
44
* @summary Check expression switch works.
55
* @compile/fail/ref=ExpressionSwitch-old.out -source 9 -Xlint:-options -XDrawDiagnostics ExpressionSwitch.java
66
* @compile ExpressionSwitch.java
@@ -120,6 +120,26 @@ private Object yieldDisambiguationLiterals(String s) {
120120
};
121121
}
122122

123+
private int yieldUnaryNumberOperator(String s, int a) {
124+
return switch (s) {
125+
case "a": yield +a;
126+
case "b": yield -a;
127+
case "c": yield ~a; // intentionally repeated ~a, test the case clause
128+
case "d": yield ++a;
129+
case "e": yield --a;
130+
case "f": yield a++;
131+
case "g": yield a--;
132+
default: yield ~a; // intentionally repeated ~a, test the default clause
133+
};
134+
}
135+
136+
private boolean yieldUnaryNotOperator(String s, boolean b) {
137+
return switch (s) {
138+
case "a": yield !b; // intentionally repeated !b, test the case clause
139+
default: yield !b; // intentionally repeated !b, test the default clause
140+
};
141+
}
142+
123143
private void localClass(T t) {
124144
String good = "good";
125145
class L {

0 commit comments

Comments
 (0)