Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct handling for SwitchStatements without a containing Block #10034

Merged
merged 1 commit into from
Nov 11, 2024
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
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