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

If the switch expression is used to use boolean, JDTLS does not report an error #3141

Closed
curtishd opened this issue Apr 27, 2024 · 3 comments · Fixed by #3145
Closed

If the switch expression is used to use boolean, JDTLS does not report an error #3141

curtishd opened this issue Apr 27, 2024 · 3 comments · Fixed by #3145
Assignees
Milestone

Comments

@curtishd
Copy link

Swich expressions cannot be passed in boolean, float, double and long primitive types, including their wrapper types. However, JDTLS doesn`t report error here. By the way, My java version is 21.
屏幕截图 2024-04-27 200046
屏幕截图 2024-04-27 200841

@rgrunber rgrunber added the bug label Apr 29, 2024
@rgrunber
Copy link
Contributor

rgrunber commented Apr 29, 2024

I'm able to reproduce as well. I see the following in the logs :

[Error - 13:11:34] Apr. 29, 2024, 1:11:34 p.m. Exception occurred during problem detection:
----------------------------------- SOURCE BEGIN -------------------------------------
package org.example;

public class App {
    public static void main(String[] args) {
        var casuality = new Casuality();
        var r = casuality.key;
        boolean re = switch (r) {
            case false -> {
                yield true;
            }
            default -> {
                yield false;
            }
        };
        System.out.println(re);
    }
}
----------------------------------- SOURCE END -------------------------------------
boolean constant cannot be casted into int
org.eclipse.jdt.internal.compiler.problem.ShouldNotImplement: boolean constant cannot be casted into int
	at org.eclipse.jdt.internal.compiler.impl.Constant.intValue(Constant.java:1509)
	at org.eclipse.jdt.internal.compiler.ast.CaseStatement$ResolvedCase.<init>(CaseStatement.java:143)
	at org.eclipse.jdt.internal.compiler.ast.CaseStatement.resolveCase(CaseStatement.java:236)
	at org.eclipse.jdt.internal.compiler.ast.SwitchStatement.resolve(SwitchStatement.java:1158)
	at org.eclipse.jdt.internal.compiler.ast.SwitchExpression.resolveType(SwitchExpression.java:418)
	at org.eclipse.jdt.internal.compiler.ast.LocalDeclaration.skipResolveInitializerIfAlreadyCalled2(LocalDeclaration.java:383)
	at org.eclipse.jdt.internal.compiler.ast.LocalDeclaration.resolve(LocalDeclaration.java:402)
	at org.eclipse.jdt.internal.compiler.ast.LocalDeclaration.resolve(LocalDeclaration.java:258)
	at org.eclipse.jdt.internal.compiler.ast.Statement.resolveWithBindings(Statement.java:498)
	at org.eclipse.jdt.internal.compiler.ast.ASTNode.resolveStatements(ASTNode.java:726)
	at org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration.resolveStatements(AbstractMethodDeclaration.java:714)
	at org.eclipse.jdt.internal.compiler.ast.MethodDeclaration.resolveStatements(MethodDeclaration.java:409)
	at org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration.resolve(AbstractMethodDeclaration.java:612)
	at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.resolve(TypeDeclaration.java:1517)
	at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.resolve(TypeDeclaration.java:1646)
	at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.resolve(CompilationUnitDeclaration.java:666)
	at org.eclipse.jdt.internal.compiler.Compiler.resolve(Compiler.java:1068)
	at org.eclipse.jdt.internal.compiler.Compiler.resolve(Compiler.java:1110)
	at org.eclipse.jdt.internal.core.CompilationUnitProblemFinder.process(CompilationUnitProblemFinder.java:280)
	at org.eclipse.jdt.internal.core.CompilationUnitProblemFinder.process(CompilationUnitProblemFinder.java:346)
	at org.eclipse.jdt.internal.core.ReconcileWorkingCopyOperation.makeConsistent(ReconcileWorkingCopyOperation.java:186)
	at org.eclipse.jdt.internal.core.ReconcileWorkingCopyOperation.executeOperation(ReconcileWorkingCopyOperation.java:92)
	at org.eclipse.jdt.internal.core.JavaModelOperation.run(JavaModelOperation.java:740)
	at org.eclipse.jdt.internal.core.JavaModelOperation.runOperation(JavaModelOperation.java:805)
	at org.eclipse.jdt.internal.core.CompilationUnit.reconcile(CompilationUnit.java:1311)
	at org.eclipse.jdt.ls.core.internal.handlers.BaseDocumentLifeCycleHandler.publishDiagnostics(BaseDocumentLifeCycleHandler.java:332)
	at org.eclipse.jdt.ls.core.internal.handlers.BaseDocumentLifeCycleHandler.publishDiagnostics(BaseDocumentLifeCycleHandler.java:295)
	at org.eclipse.jdt.ls.core.internal.handlers.BaseDocumentLifeCycleHandler$PublishDiagnosticJob.run(BaseDocumentLifeCycleHandler.java:777)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)

Eclipse does correctly report the error as Cannot switch on a value of type boolean. Only convertible int values, strings or enum variables are permitted so I'm not sure why we're failing on parsing. Update: I must have been using something like Java 17 compliance, but when I set it to 21, I saw the error as well.

@curtishd
Copy link
Author

curtishd commented May 1, 2024

Yes, it's a bit tricky, I've been busy working lately and if someone to pull request, I would appreciate it.

@rgrunber rgrunber self-assigned this May 1, 2024
@rgrunber
Copy link
Contributor

rgrunber commented May 1, 2024

I just updated JDT-LS to a newer version of JDT Core after finding eclipse-jdt/eclipse.jdt.core#2382 , which seems very similar (switch expression on a boolean).

image

Looks like we'll get the fix for free!

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants