eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
161 stars 130 forks source link

ArrayStoreException in SelectionParser.buildMoreCompletionContext #2443

Closed laeubi closed 5 months ago

laeubi commented 5 months ago

I'm not sure how to reproduce that but on some of my code getting the following exception when pressing F3:

Exception

java.lang.reflect.InvocationTargetException
    at org.eclipse.jface.operation.ModalContext.run(ModalContext.java:396)
    at org.eclipse.jface.dialogs.ProgressMonitorDialog.run(ProgressMonitorDialog.java:470)
    at org.eclipse.ui.internal.progress.ProgressMonitorJobsDialog.run(ProgressMonitorJobsDialog.java:230)
    at org.eclipse.ui.internal.progress.ProgressManager.lambda$26(ProgressManager.java:819)
    at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:67)
    at org.eclipse.ui.internal.progress.ProgressManager.busyCursorWhile(ProgressManager.java:849)
    at org.eclipse.ui.internal.progress.ProgressManager.busyCursorWhile(ProgressManager.java:828)
    at org.eclipse.jdt.internal.ui.actions.SelectionConverter.performForkedCodeResolve(SelectionConverter.java:260)
    at org.eclipse.jdt.internal.ui.actions.SelectionConverter.codeResolveForked(SelectionConverter.java:172)
    at org.eclipse.jdt.ui.actions.OpenAction.run(OpenAction.java:164)
    at org.eclipse.jdt.ui.actions.SelectionDispatchAction.dispatchRun(SelectionDispatchAction.java:278)
    at org.eclipse.jdt.ui.actions.SelectionDispatchAction.run(SelectionDispatchAction.java:252)
    at org.eclipse.jface.action.Action.runWithEvent(Action.java:474)
    at org.eclipse.jface.commands.ActionHandler.execute(ActionHandler.java:124)
    at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:98)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:58)
    at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:299)
    at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:233)
    at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:174)
    at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:165)
    at org.eclipse.core.commands.Command.executeWithChecks(Command.java:488)
    at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:485)
    at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:204)
    at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.executeCommand(KeyBindingDispatcher.java:308)
    at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.press(KeyBindingDispatcher.java:569)
    at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.processKeyEvent(KeyBindingDispatcher.java:644)
    at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.filterKeySequenceBindings(KeyBindingDispatcher.java:439)
    at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher$KeyDownFilter.handleEvent(KeyBindingDispatcher.java:96)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:91)
    at org.eclipse.swt.widgets.Display.filterEvent(Display.java:1954)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1616)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1643)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1626)
    at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1665)
    at org.eclipse.swt.widgets.Widget.gtk_key_press_event(Widget.java:994)
    at org.eclipse.swt.widgets.Control.gtk_key_press_event(Control.java:3999)
    at org.eclipse.swt.widgets.Composite.gtk_key_press_event(Composite.java:931)
    at org.eclipse.swt.widgets.Widget.windowProc(Widget.java:2595)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:6833)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:6162)
    at org.eclipse.swt.internal.gtk3.GTK3.gtk_main_do_event(Native Method)
    at org.eclipse.swt.widgets.Display.eventProc(Display.java:1598)
    at org.eclipse.swt.internal.gtk3.GTK3.gtk_main_iteration_do(Native Method)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:4514)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1151)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:339)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1042)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:152)
    at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:639)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:339)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:546)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:173)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:152)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:208)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:143)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:109)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:439)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:271)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:679)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:616)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1492)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1465)
Caused by: java.lang.ArrayStoreException: arraycopy: element type mismatch: can not cast one of the elements of org.eclipse.jdt.internal.compiler.ast.ASTNode[] to the type of the destination array, org.eclipse.jdt.internal.compiler.ast.Statement
    at java.base/java.lang.System.arraycopy(Native Method)
    at org.eclipse.jdt.internal.codeassist.select.SelectionParser.buildMoreCompletionContext(SelectionParser.java:227)
    at org.eclipse.jdt.internal.codeassist.select.SelectionParser.attachOrphanCompletionNode(SelectionParser.java:160)
    at org.eclipse.jdt.internal.codeassist.select.SelectionParser.updateRecoveryState(SelectionParser.java:2020)
    at org.eclipse.jdt.internal.compiler.parser.Parser.resumeOnSyntaxError(Parser.java:14087)
    at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:12613)
    at org.eclipse.jdt.internal.codeassist.impl.AssistParser.parseBlockStatements(AssistParser.java:2104)
    at org.eclipse.jdt.internal.codeassist.impl.AssistParser.parseBlockStatements(AssistParser.java:1940)
    at org.eclipse.jdt.internal.codeassist.impl.Engine.parseBlockStatements(Engine.java:350)
    at org.eclipse.jdt.internal.codeassist.impl.Engine.parseBlockStatements(Engine.java:313)
    at org.eclipse.jdt.internal.codeassist.SelectionEngine.select(SelectionEngine.java:1080)
    at org.eclipse.jdt.internal.core.Openable.codeSelect(Openable.java:166)
    at org.eclipse.jdt.internal.core.CompilationUnit.codeSelect(CompilationUnit.java:382)
    at org.eclipse.jdt.internal.core.CompilationUnit.codeSelect(CompilationUnit.java:375)
    at org.eclipse.jdt.internal.ui.actions.SelectionConverter.codeResolve(SelectionConverter.java:269)
    at org.eclipse.jdt.internal.ui.actions.SelectionConverter$1CodeResolveRunnable.lambda$0(SelectionConverter.java:253)
    at org.eclipse.jdt.internal.core.JavaModelManager.cacheZipFiles(JavaModelManager.java:5765)
    at org.eclipse.jdt.internal.core.JavaModelManager.callReadOnly(JavaModelManager.java:5754)
    at org.eclipse.jdt.core.JavaCore.callReadOnly(JavaCore.java:6130)
    at org.eclipse.jdt.internal.ui.actions.SelectionConverter$1CodeResolveRunnable.run(SelectionConverter.java:253)
    at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:124)
Root exception:
java.lang.ArrayStoreException: arraycopy: element type mismatch: can not cast one of the elements of org.eclipse.jdt.internal.compiler.ast.ASTNode[] to the type of the destination array, org.eclipse.jdt.internal.compiler.ast.Statement
    at java.base/java.lang.System.arraycopy(Native Method)
    at org.eclipse.jdt.internal.codeassist.select.SelectionParser.buildMoreCompletionContext(SelectionParser.java:227)
    at org.eclipse.jdt.internal.codeassist.select.SelectionParser.attachOrphanCompletionNode(SelectionParser.java:160)
    at org.eclipse.jdt.internal.codeassist.select.SelectionParser.updateRecoveryState(SelectionParser.java:2020)
    at org.eclipse.jdt.internal.compiler.parser.Parser.resumeOnSyntaxError(Parser.java:14087)
    at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:12613)
    at org.eclipse.jdt.internal.codeassist.impl.AssistParser.parseBlockStatements(AssistParser.java:2104)
    at org.eclipse.jdt.internal.codeassist.impl.AssistParser.parseBlockStatements(AssistParser.java:1940)
    at org.eclipse.jdt.internal.codeassist.impl.Engine.parseBlockStatements(Engine.java:350)
    at org.eclipse.jdt.internal.codeassist.impl.Engine.parseBlockStatements(Engine.java:313)
    at org.eclipse.jdt.internal.codeassist.SelectionEngine.select(SelectionEngine.java:1080)
    at org.eclipse.jdt.internal.core.Openable.codeSelect(Openable.java:166)
    at org.eclipse.jdt.internal.core.CompilationUnit.codeSelect(CompilationUnit.java:382)
    at org.eclipse.jdt.internal.core.CompilationUnit.codeSelect(CompilationUnit.java:375)
    at org.eclipse.jdt.internal.ui.actions.SelectionConverter.codeResolve(SelectionConverter.java:269)
    at org.eclipse.jdt.internal.ui.actions.SelectionConverter$1CodeResolveRunnable.lambda$0(SelectionConverter.java:253)
    at org.eclipse.jdt.internal.core.JavaModelManager.cacheZipFiles(JavaModelManager.java:5765)
    at org.eclipse.jdt.internal.core.JavaModelManager.callReadOnly(JavaModelManager.java:5754)
    at org.eclipse.jdt.core.JavaCore.callReadOnly(JavaCore.java:6130)
    at org.eclipse.jdt.internal.ui.actions.SelectionConverter$1CodeResolveRunnable.run(SelectionConverter.java:253)
    at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:124)

Eclipse Version

Version: 2024-06 M2 (4.32.0 M2)
Build id: 20240502-0722
srikanth-sankaran commented 5 months ago

Can't proceed without a test case, but I will look into adding some instrumentation that would log which file and which method is being worked on when the error happened. That should help a bit.

jukzi commented 5 months ago

reoccurrence of https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1514, https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1420

srikanth-sankaran commented 5 months ago

reoccurrence of #1514, #1420

Similar symptoms and problem spots perhaps, but those are fixed with a reproducer. So this must be an at least slightly different cause.

laeubi commented 5 months ago

As said I can't reproduce it directly it just "happened" while coding, maybe because there are some compile errors at the moment, for me the code looks like it should not perform an array copy without validate the node is of the correct type and just skip nodes that are not a statement (comments? don't know).

laeubi commented 5 months ago

I think I got a reproducer now, clone the repository:

https://github.com/eclipse-tycho/tycho

then import tycho/tycho-core project and go to SourceCodeAnalyzerPlugin.java line 117 and place the cursor over getContained() method:

analyzer.getContained().put(packageRef);
                    ^

then press F3 and you will get an error popup grafik

srikanth-sankaran commented 5 months ago

I think I got a reproducer now, clone the repository:

Perfect, thanks! I will try to get a fix in for M3, failing that certainly for RC1

iloveeclipse commented 5 months ago

For M3 it is too late, that schould be the build from last night.

srikanth-sankaran commented 5 months ago

Smaller test case:

public class X {

    public static void main(String[] args) {

        for (String sourcePath : args) {
            new X() {
                public void foo(String file) {
                    file.hashCode();
                }

            };
        }
    }
}

Placing the cursor on file in the statement file.hashCode() and pressing F3 triggers java.lang.ArrayStoreException: arraycopy: element type mismatch: can not cast one of the elements of org.eclipse.jdt.internal.compiler.ast.ASTNode[] to the type of the destination array, org.eclipse.jdt.internal.compiler.ast.Statement

Working on it ...

laeubi commented 5 months ago

@srikanth-sankaran great you found a smaller reproducer :+1: a fix for RC1 would be great as it greatly degrades functionality.

srikanth-sankaran commented 5 months ago

@srikanth-sankaran great you found a smaller reproducer 👍 a fix for RC1 would be great as it greatly degrades functionality.

On the case. RC1 is very much the goal.