Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev/core/src/com/google/gwt/dev/jjs/ast/JExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public JExpression(SourceInfo info) {
/**
* Returns a statement that executes this expression.
*/
public final JExpressionStatement makeStatement() {
public JStatement makeStatement() {
return new JExpressionStatement(getSourceInfo(), this);
}

Expand Down
2 changes: 1 addition & 1 deletion dev/core/src/com/google/gwt/dev/jjs/ast/JProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public int compare(JArrayType o1, JArrayType o2) {
/**
* Helper to create an assignment, used to initialize fields, etc.
*/
public static JExpressionStatement createAssignmentStmt(SourceInfo info, JExpression lhs,
public static JStatement createAssignmentStmt(SourceInfo info, JExpression lhs,
JExpression rhs) {
return createAssignment(info, lhs, rhs).makeStatement();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ public void traverse(JVisitor visitor, Context ctx) {
public JType getType() {
return type;
}

@Override
public JStatement makeStatement() {
return new JSwitchStatement(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -700,16 +700,7 @@ public JsNode transformDoStatement(JDoStatement doStatement) {

@Override
public JsNode transformExpressionStatement(JExpressionStatement statement) {
if (statement.getExpr() instanceof JSwitchExpression) {
if (statement.getExpr().getType() == JPrimitiveType.VOID) {
return transformSwitchStatement(new JSwitchStatement((JSwitchExpression)
statement.getExpr()));
} else {
throw new IllegalStateException("top-level switch expr");
}
} else {
return transform(statement.getExpr()).makeStmt();
}
return transform(statement.getExpr()).makeStmt();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,46 @@ private static String arrowWithStatement() {
};
return "success";
}


// https://github.com/gwtproject/gwt/issues/10121
public void testUnusedSwitchExprResult() {
// When the result of a switch expression is unused, the switch expression is made into
// a statement. When this is done by wrapping the SwitchExpression in an ExpressionStatement,
// it causes an error when generating JavaScript.
int unused1 = switch(0) {
case 0 -> 1;
case 1 -> 2;
default -> 3;
};
boolean called = false;
int unused2 = switch(0) {
case 0 -> {
called = true;
assertTrue(true);
yield 1;
}
case 1 -> {
fail();
yield 2;
}
default -> {
fail();
yield 3;
}
};
assertTrue(called);

Void unused3 = switch(0) {
case 0 -> throwIfFalse(true);
case 1 -> throwIfFalse(false);
default -> throwIfFalse(false);
};
}
private static Void throwIfFalse(boolean condition) {
if (!condition) {
fail();
}
return null;
}
}
5 changes: 5 additions & 0 deletions user/test/com/google/gwt/dev/jjs/test/Java17Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,15 @@ public void testSwitchExprInlining() {
public void testInlinedStringConstantsInCase() {
assertFalse(isGwtSourceLevel17());
}

public void testCaseArrowLabelsVoidExpression() {
assertFalse(isGwtSourceLevel17());
}

public void testUnusedSwitchExprResult() {
assertFalse(isGwtSourceLevel17());
}

private boolean isGwtSourceLevel17() {
return JUnitShell.getCompilerOptions().getSourceLevel().compareTo(SourceLevel.JAVA17) >= 0;
}
Expand Down