eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
167 stars 131 forks source link

JavaAllCompletionProposalComputer IfStatement cannot be cast to class LambdaExpression #1163

Open garretwilson opened 1 year ago

garretwilson commented 1 year ago

I originally filed this ticket as eclipse-platform/eclipse.platform.ui#837.

I ran into a problem where Eclipse was incorrectly indicating that an exception should have a catch even though I was using sneaky throws. I filed that problem as #1162.

However when I was trying to find a workaround to that problem, I started to add some catches just to try to get the error to go away so I could determine how best to deal with it. Starting with the code in #1162, I did something like this:

try (final InputStream inputStream = new BufferedInputStream(newInputStream(file))) {
  document = documentBuilder.parse(inputStream);
} catch(final IOException ioEx) {
} catch(final SAXException saxException) {
}

When I put my cursor after ioEx and hit Ctrl+Space to autocomplete to (I had hoped) ioException, Eclipse threw a fit. (See error below.) Unfortunately I have not yet been able to reproduce the problem. I'm filing this ticket in hopes that looking at the stack trace can give you a clue to where the problem is, or at least put in additional logging to detect the cause in the future. When you're investigating #1162 you might keep your eyes open for something that might cause the problem, as they might (or might not) be related.

eclipse.buildId=4.28.0.20230608-1200
java.version=17.0.7
java.vendor=Eclipse Adoptium
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en_US
Framework arguments:  -product org.eclipse.epp.package.jee.product
Command-line arguments:  -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.jee.product

org.eclipse.jdt.ui
Warning
Mon Jun 19 10:10:40 PDT 2023
The 'org.eclipse.jdt.ui.JavaAllCompletionProposalComputer' proposal computer from the 'org.eclipse.jdt.ui' plug-in did not complete normally. The extension has thrown a runtime exception.

java.lang.ClassCastException: class org.eclipse.jdt.internal.compiler.ast.IfStatement cannot be cast to class org.eclipse.jdt.internal.compiler.ast.LambdaExpression (org.eclipse.jdt.internal.compiler.ast.IfStatement and org.eclipse.jdt.internal.compiler.ast.LambdaExpression are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @189e986f)
    at org.eclipse.jdt.internal.compiler.parser.Parser.consumeLambdaExpression(Parser.java:9156)
    at org.eclipse.jdt.internal.codeassist.complete.CompletionParser.consumeLambdaExpression(CompletionParser.java:3747)
    at org.eclipse.jdt.internal.compiler.parser.Parser.consumeRule(Parser.java:7951)
    at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:13180)
    at org.eclipse.jdt.internal.codeassist.impl.AssistParser.parseBlockStatements(AssistParser.java:2131)
    at org.eclipse.jdt.internal.codeassist.impl.AssistParser.parseBlockStatements(AssistParser.java:1971)
    at org.eclipse.jdt.internal.codeassist.complete.CompletionParser.parseBlockStatements(CompletionParser.java:5845)
    at org.eclipse.jdt.internal.codeassist.impl.Engine.parseBlockStatements(Engine.java:349)
    at org.eclipse.jdt.internal.codeassist.impl.Engine.parseBlockStatements(Engine.java:312)
    at org.eclipse.jdt.internal.codeassist.CompletionEngine.complete(CompletionEngine.java:2335)
    at org.eclipse.jdt.internal.core.Openable.codeComplete(Openable.java:136)
    at org.eclipse.jdt.internal.core.CompilationUnit.codeComplete(CompilationUnit.java:367)
    at org.eclipse.jdt.internal.core.CompilationUnit.codeComplete(CompilationUnit.java:357)
    at org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposalComputer.internalComputeCompletionProposals(JavaCompletionProposalComputer.java:256)
    at org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposalComputer.computeCompletionProposals(JavaCompletionProposalComputer.java:218)
    at org.eclipse.jdt.internal.ui.text.java.JavaTypeCompletionProposalComputer.computeCompletionProposals(JavaTypeCompletionProposalComputer.java:65)
    at org.eclipse.jdt.internal.ui.text.java.CompletionProposalComputerDescriptor.computeCompletionProposals(CompletionProposalComputerDescriptor.java:348)
    at org.eclipse.jdt.internal.ui.text.java.CompletionProposalCategory.computeCompletionProposals(CompletionProposalCategory.java:340)
    at org.eclipse.jdt.internal.ui.text.java.ContentAssistProcessor.collectProposals(ContentAssistProcessor.java:333)
    at org.eclipse.jdt.internal.ui.text.java.ContentAssistProcessor.computeCompletionProposals(ContentAssistProcessor.java:289)
    at org.eclipse.jface.text.contentassist.AsyncCompletionProposalPopup.lambda$10(AsyncCompletionProposalPopup.java:378)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
    at org.eclipse.jface.text.contentassist.AsyncCompletionProposalPopup.lambda$9(AsyncCompletionProposalPopup.java:377)
    at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1768)
    at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1760)
    at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)
    at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)
    at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)
    at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)
    at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)
srikanth-sankaran commented 1 year ago

@garretwilson : This smells very strongly like https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1195 and its half a dozen cousins listed there. Since the error message mentions

java.lang.ClassCastException: class org.eclipse.jdt.internal.compiler.ast.IfStatement cannot be cast to class org.eclipse.jdt.internal.compiler.ast.LambdaExpression

I wish you had shared enough of context that mentions the if and the lambda which are not to be encountered in the test case here or in https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1162

Was there pattern matching with instanceof in use ? Do you recall ? Can you check ? TIA

garretwilson commented 1 year ago

Do you recall ?

Unfortunately I don't recall much. As so much of my day nowadays is filled filing bugs, for the ones that I know will be hard to reproduce, I simply must move on to actual work or I'll never get anything done. I hope that at least reporting this will help in some way.

If I thought there was any chance of providing reproducible instructions I would have spent some time—I'm not just being lazy—but in many of these cases there are so many variables involved that it would not be realistic.

I'm hoping that perhaps if enough people file tickets like this it will help the Eclipse team start to see some patterns, or at least, as I mentioned, put some checks and logging in for future releases.

Sorry I couldn't be more help on this one.

garretwilson commented 1 year ago

With some of these autocomplete issues, if I close the file and reopen it, or use the same code in a different file, the bug does not reappear, for example.

srikanth-sankaran commented 1 year ago

@garretwilson : This smells very strongly like #1195 and its half a dozen cousins listed there. Since the error message mentions

java.lang.ClassCastException: class org.eclipse.jdt.internal.compiler.ast.IfStatement cannot be cast to class org.eclipse.jdt.internal.compiler.ast.LambdaExpression

I wish you had shared enough of context that mentions the if and the lambda which are not to be encountered in the test case here or in #1162

Was there pattern matching with instanceof in use ? Do you recall ? Can you check ? TIA

Having worked on #1195 which turned out to be a significant reimplementation of the code selection support for pattern matching constructs, I am less sure this is connected to #1195 - that defect has many duplicates - all of them involving a CCE but the node involved is a LocalDeclaration, here it is an IfStatement - so perhaps this is unrelated