Skip to content

Commit 0396c65

Browse files
authored
Switch exprs must be transformed rather than wrapped in statements (#10122)
Fixes #10121
1 parent 3e515ee commit 0396c65

File tree

6 files changed

+55
-12
lines changed

6 files changed

+55
-12
lines changed

dev/core/src/com/google/gwt/dev/jjs/ast/JExpression.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public JExpression(SourceInfo info) {
3131
/**
3232
* Returns a statement that executes this expression.
3333
*/
34-
public final JExpressionStatement makeStatement() {
34+
public JStatement makeStatement() {
3535
return new JExpressionStatement(getSourceInfo(), this);
3636
}
3737

dev/core/src/com/google/gwt/dev/jjs/ast/JProgram.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public int compare(JArrayType o1, JArrayType o2) {
209209
/**
210210
* Helper to create an assignment, used to initialize fields, etc.
211211
*/
212-
public static JExpressionStatement createAssignmentStmt(SourceInfo info, JExpression lhs,
212+
public static JStatement createAssignmentStmt(SourceInfo info, JExpression lhs,
213213
JExpression rhs) {
214214
return createAssignment(info, lhs, rhs).makeStatement();
215215
}

dev/core/src/com/google/gwt/dev/jjs/ast/JSwitchExpression.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,9 @@ public void traverse(JVisitor visitor, Context ctx) {
6262
public JType getType() {
6363
return type;
6464
}
65+
66+
@Override
67+
public JStatement makeStatement() {
68+
return new JSwitchStatement(this);
69+
}
6570
}

dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -700,16 +700,7 @@ public JsNode transformDoStatement(JDoStatement doStatement) {
700700

701701
@Override
702702
public JsNode transformExpressionStatement(JExpressionStatement statement) {
703-
if (statement.getExpr() instanceof JSwitchExpression) {
704-
if (statement.getExpr().getType() == JPrimitiveType.VOID) {
705-
return transformSwitchStatement(new JSwitchStatement((JSwitchExpression)
706-
statement.getExpr()));
707-
} else {
708-
throw new IllegalStateException("top-level switch expr");
709-
}
710-
} else {
711-
return transform(statement.getExpr()).makeStmt();
712-
}
703+
return transform(statement.getExpr()).makeStmt();
713704
}
714705

715706
@Override

user/test-super/com/google/gwt/dev/jjs/super/com/google/gwt/dev/jjs/test/Java17Test.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,4 +548,46 @@ private static String arrowWithStatement() {
548548
};
549549
return "success";
550550
}
551+
552+
553+
// https://github.com/gwtproject/gwt/issues/10121
554+
public void testUnusedSwitchExprResult() {
555+
// When the result of a switch expression is unused, the switch expression is made into
556+
// a statement. When this is done by wrapping the SwitchExpression in an ExpressionStatement,
557+
// it causes an error when generating JavaScript.
558+
int unused1 = switch(0) {
559+
case 0 -> 1;
560+
case 1 -> 2;
561+
default -> 3;
562+
};
563+
boolean called = false;
564+
int unused2 = switch(0) {
565+
case 0 -> {
566+
called = true;
567+
assertTrue(true);
568+
yield 1;
569+
}
570+
case 1 -> {
571+
fail();
572+
yield 2;
573+
}
574+
default -> {
575+
fail();
576+
yield 3;
577+
}
578+
};
579+
assertTrue(called);
580+
581+
Void unused3 = switch(0) {
582+
case 0 -> throwIfFalse(true);
583+
case 1 -> throwIfFalse(false);
584+
default -> throwIfFalse(false);
585+
};
586+
}
587+
private static Void throwIfFalse(boolean condition) {
588+
if (!condition) {
589+
fail();
590+
}
591+
return null;
592+
}
551593
}

user/test/com/google/gwt/dev/jjs/test/Java17Test.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,15 @@ public void testSwitchExprInlining() {
121121
public void testInlinedStringConstantsInCase() {
122122
assertFalse(isGwtSourceLevel17());
123123
}
124+
124125
public void testCaseArrowLabelsVoidExpression() {
125126
assertFalse(isGwtSourceLevel17());
126127
}
127128

129+
public void testUnusedSwitchExprResult() {
130+
assertFalse(isGwtSourceLevel17());
131+
}
132+
128133
private boolean isGwtSourceLevel17() {
129134
return JUnitShell.getCompilerOptions().getSourceLevel().compareTo(SourceLevel.JAVA17) >= 0;
130135
}

0 commit comments

Comments
 (0)