Skip to content

Commit

Permalink
Correct handling for SwitchStatements without a containing Block (#10027
Browse files Browse the repository at this point in the history
)

Also fixed incorrect generics for related code, spotted while auditing
other incomplete typechecks.

Fixes #10024
  • Loading branch information
niloc132 authored Nov 7, 2024
1 parent a5e628b commit 636db82
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
23 changes: 14 additions & 9 deletions dev/core/src/com/google/gwt/dev/jjs/impl/GwtAstBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2812,26 +2812,31 @@ protected JDeclarationStatement pop(LocalDeclaration decl) {

protected JStatement pop(Statement x) {
JNode pop = (x == null) ? null : pop();
if (x instanceof Expression) {
if (x instanceof Expression && pop instanceof JExpression) {
return maybeBoxOrUnbox((JExpression) pop, (Expression) x).makeStatement();
}
return (JStatement) pop;
}

@SuppressWarnings("unchecked")
protected <T extends JStatement> List<T> pop(Statement[] statements) {
if (statements == null) {
return Collections.emptyList();
}
List<T> result = (List<T>) popList(statements.length);
List<T> result = Lists.newArrayList();
List<? extends JNode> stack = popList(statements.length);
int i = 0;
for (ListIterator<T> it = result.listIterator(); it.hasNext(); ++i) {
for (ListIterator<? extends JNode> it = stack.listIterator(); it.hasNext(); ++i) {
Object element = it.next();
if (element == null) {
it.remove();
} else if (element instanceof JExpression) {
it.set((T)
maybeBoxOrUnbox((JExpression) element, (Expression) statements[i]).makeStatement());
if (element != null) {
if (element instanceof JExpression) {
JExpression unboxed = maybeBoxOrUnbox((JExpression) element, (Expression) statements[i]);
result.add((T) unboxed.makeStatement());
} else if (element instanceof JStatement) {
result.add((T) element);
} else {
throw new IllegalStateException(
"Unexpected element type, expected statement or expression: " + element);
}
}
}
return result;
Expand Down
59 changes: 59 additions & 0 deletions dev/core/test/com/google/gwt/dev/jjs/impl/Java17AstTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,65 @@ public void testInstanceOfPatternMatchingWithConditionalOperator()
"}");
}

// The JDT ast nodes for both switch statements and expressions extend Expression, and specific
// ast builder traversals previously. Test switch expressions/statements where statements/blocks
// can be encountered.
public void testSwitchesWithoutBlocks() throws UnableToCompleteException {
compileSnippet("void",
"for (int i = 0; i < 10; i++) " +
" switch(i) { " +
" case 1: break;" +
" case 2: " +
" default:" +
" }");
compileSnippet("void",
"for (int i : new int[] {1, 2, 3, 4}) " +
" switch(i) { " +
" case 1:" +
" case 2:" +
" default:" +
" }");
compileSnippet("void",
"switch (4) {" +
"case 1:" +
" switch (2) {" +
" case 2:" +
" case 4:" +
" default:" +
" }" +
"}");
compileSnippet("void",
"if (true == false) " +
" switch (7) {" +
" case 4: {" +
" break;" +
" }" +
" }" +
"else " +
" switch (8) {" +
" case 9:" +
" }");

compileSnippet("void",
"while(true)" +
" switch(99) { " +
" default:" +
" }");

compileSnippet("void",
"do" +
" switch(0) { " +
" default:" +
" }" +
"while (false);");

compileSnippet("void",
"foo:" +
" switch(123) { " +
" default:" +
" }");
}

@Override
protected void optimizeJava() {
}
Expand Down

0 comments on commit 636db82

Please sign in to comment.