Skip to content

Commit

Permalink
[Enhanced Switch] Wasteful generation of switch ordinal mapping table
Browse files Browse the repository at this point in the history
for enum switches that are dispatched via enumSwitch indy

* Fixes eclipse-jdt#3447
  • Loading branch information
srikanth-sankaran committed Dec 13, 2024
1 parent 93d8dc5 commit 585c0e4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ else if ((statement.bits & ASTNode.DocumentedFallthrough) == 0) // the case is n
}

final TypeBinding resolvedTypeBinding = this.expression.resolvedType;
if (resolvedTypeBinding.isEnum()) {
if (resolvedTypeBinding.isEnum() && !needPatternDispatchCopy()) {
final SourceTypeBinding sourceTypeBinding = currentScope.classScope().referenceContext.binding;
this.synthetic = sourceTypeBinding.addSyntheticMethodForSwitchEnum(resolvedTypeBinding, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected void runWarningTest(String[] testFiles, String expectedCompilerLog,
runner.runWarningTest();
}

private static void verifyClassFile(String expectedOutput, String classFileName, int mode)
private static void verifyClassFile(String expectedOutput, String unexpectedOutput, String classFileName, int mode)
throws IOException, ClassFormatException {
File f = new File(OUTPUT_DIR + File.separator + classFileName);
byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(f);
Expand All @@ -118,6 +118,15 @@ private static void verifyClassFile(String expectedOutput, String classFileName,
if (index == -1) {
assertEquals("Wrong contents", expectedOutput, result);
}
if (unexpectedOutput != null) {
index = result.indexOf(unexpectedOutput);
assertTrue("Unexpected output found", index == -1);
}
}

private static void verifyClassFile(String expectedOutput, String classFileName, int mode)
throws IOException, ClassFormatException {
verifyClassFile(expectedOutput, null, classFileName, mode);
}
public void testIssue57_001() {
runConformTest(
Expand Down Expand Up @@ -9628,4 +9637,37 @@ public static void main(String[] args) {
"World--Check--Hello--Null--Default--\n" +
"Default--Null--Hello--Check--World--");
}

// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3447
// [Enhanced Switch] Wasteful generation of switch ordinal mapping table for enum switches that are dispatched via enumSwitch indy
public void testIssue3447() throws Exception {
runConformTest(
new String[] {
"X.java",
"""
enum E {
A,
B,
}
public class X {
public static void main(String[] args) {
E e = E.A;
switch (e) {
case A : System.out.println("A");
break;
case B: System.out.println("B");
break;
case null : System.out.println("null");
break;
}
}
}
"""
},
"A");
String expectedOutput = "8 invokedynamic 0 enumSwitch(E, int) : int [22]\n";
String unexpectedOutput = "static synthetic int[] $SWITCH_TABLE$E();\n";
SwitchPatternTest.verifyClassFile(expectedOutput, unexpectedOutput, "X.class", ClassFileBytesDisassembler.SYSTEM);
}
}

0 comments on commit 585c0e4

Please sign in to comment.