diff --git a/dev/core/src/com/google/gwt/dev/jjs/impl/GwtAstBuilder.java b/dev/core/src/com/google/gwt/dev/jjs/impl/GwtAstBuilder.java index 91ef6a00af..354d9cdb65 100644 --- a/dev/core/src/com/google/gwt/dev/jjs/impl/GwtAstBuilder.java +++ b/dev/core/src/com/google/gwt/dev/jjs/impl/GwtAstBuilder.java @@ -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 List pop(Statement[] statements) { if (statements == null) { return Collections.emptyList(); } - List result = (List) popList(statements.length); + List result = Lists.newArrayList(); + List stack = popList(statements.length); int i = 0; - for (ListIterator it = result.listIterator(); it.hasNext(); ++i) { + for (ListIterator 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; diff --git a/dev/core/test/com/google/gwt/dev/jjs/impl/Java17AstTest.java b/dev/core/test/com/google/gwt/dev/jjs/impl/Java17AstTest.java index 4e855c5ced..6279f751e9 100644 --- a/dev/core/test/com/google/gwt/dev/jjs/impl/Java17AstTest.java +++ b/dev/core/test/com/google/gwt/dev/jjs/impl/Java17AstTest.java @@ -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() { }