Skip to content

Commit e030c53

Browse files
authored
Correct handling for SwitchStatements without a containing Block (#10034)
Also fixed incorrect generics for related code, spotted while auditing other incomplete typechecks. Fixes #10024 See #10027
1 parent 0056242 commit e030c53

File tree

2 files changed

+73
-9
lines changed

2 files changed

+73
-9
lines changed

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,26 +2812,31 @@ protected JDeclarationStatement pop(LocalDeclaration decl) {
28122812

28132813
protected JStatement pop(Statement x) {
28142814
JNode pop = (x == null) ? null : pop();
2815-
if (x instanceof Expression) {
2815+
if (x instanceof Expression && pop instanceof JExpression) {
28162816
return maybeBoxOrUnbox((JExpression) pop, (Expression) x).makeStatement();
28172817
}
28182818
return (JStatement) pop;
28192819
}
28202820

2821-
@SuppressWarnings("unchecked")
28222821
protected <T extends JStatement> List<T> pop(Statement[] statements) {
28232822
if (statements == null) {
28242823
return Collections.emptyList();
28252824
}
2826-
List<T> result = (List<T>) popList(statements.length);
2825+
List<T> result = Lists.newArrayList();
2826+
List<? extends JNode> stack = popList(statements.length);
28272827
int i = 0;
2828-
for (ListIterator<T> it = result.listIterator(); it.hasNext(); ++i) {
2828+
for (ListIterator<? extends JNode> it = stack.listIterator(); it.hasNext(); ++i) {
28292829
Object element = it.next();
2830-
if (element == null) {
2831-
it.remove();
2832-
} else if (element instanceof JExpression) {
2833-
it.set((T)
2834-
maybeBoxOrUnbox((JExpression) element, (Expression) statements[i]).makeStatement());
2830+
if (element != null) {
2831+
if (element instanceof JExpression) {
2832+
JExpression unboxed = maybeBoxOrUnbox((JExpression) element, (Expression) statements[i]);
2833+
result.add((T) unboxed.makeStatement());
2834+
} else if (element instanceof JStatement) {
2835+
result.add((T) element);
2836+
} else {
2837+
throw new IllegalStateException(
2838+
"Unexpected element type, expected statement or expression: " + element);
2839+
}
28352840
}
28362841
}
28372842
return result;

dev/core/test/com/google/gwt/dev/jjs/impl/Java17AstTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,65 @@ public void testInstanceOfPatternMatchingWithConditionalOperator()
397397
"}");
398398
}
399399

400+
// The JDT ast nodes for both switch statements and expressions extend Expression, and specific
401+
// ast builder traversals previously. Test switch expressions/statements where statements/blocks
402+
// can be encountered.
403+
public void testSwitchesWithoutBlocks() throws UnableToCompleteException {
404+
compileSnippet("void",
405+
"for (int i = 0; i < 10; i++) " +
406+
" switch(i) { " +
407+
" case 1: break;" +
408+
" case 2: " +
409+
" default:" +
410+
" }");
411+
compileSnippet("void",
412+
"for (int i : new int[] {1, 2, 3, 4}) " +
413+
" switch(i) { " +
414+
" case 1:" +
415+
" case 2:" +
416+
" default:" +
417+
" }");
418+
compileSnippet("void",
419+
"switch (4) {" +
420+
"case 1:" +
421+
" switch (2) {" +
422+
" case 2:" +
423+
" case 4:" +
424+
" default:" +
425+
" }" +
426+
"}");
427+
compileSnippet("void",
428+
"if (true == false) " +
429+
" switch (7) {" +
430+
" case 4: {" +
431+
" break;" +
432+
" }" +
433+
" }" +
434+
"else " +
435+
" switch (8) {" +
436+
" case 9:" +
437+
" }");
438+
439+
compileSnippet("void",
440+
"while(true)" +
441+
" switch(99) { " +
442+
" default:" +
443+
" }");
444+
445+
compileSnippet("void",
446+
"do" +
447+
" switch(0) { " +
448+
" default:" +
449+
" }" +
450+
"while (false);");
451+
452+
compileSnippet("void",
453+
"foo:" +
454+
" switch(123) { " +
455+
" default:" +
456+
" }");
457+
}
458+
400459
@Override
401460
protected void optimizeJava() {
402461
}

0 commit comments

Comments
 (0)