eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
161 stars 130 forks source link

[Switch] default->null caused a building problem. #3135

Closed wdollar closed 3 days ago

wdollar commented 3 days ago
public static void main(String[] args) {
    int i = 3;
    int[] arr = { 1, 2, 3 };
    System.out.println((switch (i) {
        case 3 -> arr;
        default -> null; // Replacing null with a non-null value can avoid this issue.
    })[0]);
}

I'm using jdt_3.19.600.v20240903-0240, and this code caused the following error.

Problems occurred when invoking code from plug-in: "org.eclipse.jdt.core.manipulation".

java.lang.ClassCastException: class org.eclipse.jdt.internal.compiler.lookup.NullTypeBinding cannot be cast to class org.eclipse.jdt.internal.compiler.lookup.ArrayBinding (org.eclipse.jdt.internal.compiler.lookup.NullTypeBinding and org.eclipse.jdt.internal.compiler.lookup.ArrayBinding are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @697f9f69)
    at org.eclipse.jdt.internal.compiler.codegen.OperandStack.xaload(OperandStack.java:156)
    at org.eclipse.jdt.internal.compiler.codegen.CodeStream.iaload(CodeStream.java:3867)
    at org.eclipse.jdt.internal.compiler.codegen.CodeStream.arrayAt(CodeStream.java:433)
    at org.eclipse.jdt.internal.compiler.ast.ArrayReference.generateCode(ArrayReference.java:113)
    at org.eclipse.jdt.internal.compiler.ast.Statement.generateArguments(Statement.java:440)
    at org.eclipse.jdt.internal.compiler.ast.MessageSend.generateCode(MessageSend.java:626)
    at org.eclipse.jdt.internal.compiler.ast.Expression.generateCode(Expression.java:771)
    at org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration.generateCode(AbstractMethodDeclaration.java:387)
    at org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration.generateCode(AbstractMethodDeclaration.java:323)
    at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.generateCode(TypeDeclaration.java:759)
    at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.generateCode(TypeDeclaration.java:829)
    at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.generateCode(CompilationUnitDeclaration.java:412)
    at org.eclipse.jdt.core.dom.CompilationUnitResolver.resolve(CompilationUnitResolver.java:1378)
    at org.eclipse.jdt.core.dom.CompilationUnitResolver.resolve(CompilationUnitResolver.java:837)
    at org.eclipse.jdt.core.dom.CompilationUnitResolver.toCompilationUnit(CompilationUnitResolver.java:1467)
    at org.eclipse.jdt.core.dom.CompilationUnitResolver$ECJCompilationUnitResolver.toCompilationUnit(CompilationUnitResolver.java:117)
    at org.eclipse.jdt.core.dom.ASTParser.internalCreateASTCached(ASTParser.java:1263)
    at org.eclipse.jdt.core.dom.ASTParser.lambda$0(ASTParser.java:1142)
    at org.eclipse.jdt.internal.core.JavaModelManager.cacheZipFiles(JavaModelManager.java:5770)
    at org.eclipse.jdt.core.dom.ASTParser.internalCreateAST(ASTParser.java:1142)
    at org.eclipse.jdt.core.dom.ASTParser.createAST(ASTParser.java:882)
    at org.eclipse.jdt.core.manipulation.CoreASTProvider$1.run(CoreASTProvider.java:294)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:47)
    at org.eclipse.jdt.core.manipulation.CoreASTProvider.createAST(CoreASTProvider.java:286)
    at org.eclipse.jdt.core.manipulation.CoreASTProvider.getAST(CoreASTProvider.java:199)
    at org.eclipse.jdt.core.manipulation.SharedASTProviderCore.getAST(SharedASTProviderCore.java:138)
    at org.eclipse.jdt.internal.ui.viewsupport.SelectionListenerWithASTManager$PartListenerGroup.calculateASTandInform(SelectionListenerWithASTManager.java:167)
    at org.eclipse.jdt.internal.ui.viewsupport.SelectionListenerWithASTManager$PartListenerGroup$1.lambda$0(SelectionListenerWithASTManager.java:149)
    at org.eclipse.jdt.internal.core.JavaModelManager.cacheZipFiles(JavaModelManager.java:5770)
    at org.eclipse.jdt.internal.core.JavaModelManager.callReadOnly(JavaModelManager.java:5759)
    at org.eclipse.jdt.core.JavaCore.callReadOnly(JavaCore.java:6188)
    at org.eclipse.jdt.internal.ui.viewsupport.SelectionListenerWithASTManager$PartListenerGroup$1.run(SelectionListenerWithASTManager.java:149)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
srikanth-sankaran commented 3 days ago

Reproduced, I will follow up. Thanks!

srikanth-sankaran commented 3 days ago

See also https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3042