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

CCE: LocalDeclaration cannot be cast to class ForeachStatement #1263

Closed
iloveeclipse opened this issue Aug 3, 2023 · 5 comments · Fixed by #1349
Closed

CCE: LocalDeclaration cannot be cast to class ForeachStatement #1263

iloveeclipse opened this issue Aug 3, 2023 · 5 comments · Fixed by #1349
Assignees
Labels
bug Something isn't working

Comments

@iloveeclipse
Copy link
Member

Using latest build I20230802-1800.
Open org.eclipse.jdt.internal.launching.DetectVMInstallationsJob class in the IDE.
Hold "Ctrl" and hover over the "arrows" in lambda expressions in the run() method.

Exception reported:

java.lang.ClassCastException: class org.eclipse.jdt.internal.compiler.ast.LocalDeclaration cannot be cast to class org.eclipse.jdt.internal.compiler.ast.ForeachStatement (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration and org.eclipse.jdt.internal.compiler.ast.ForeachStatement are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @2f9236d8)
	at org.eclipse.jdt.internal.compiler.parser.Parser.consumeEnhancedForStatement(Parser.java:3435)
	at org.eclipse.jdt.internal.compiler.parser.Parser.consumeRule(Parser.java:8471)
	at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:13145)
	at org.eclipse.jdt.internal.codeassist.impl.AssistParser.parseBlockStatements(AssistParser.java:2139)
	at org.eclipse.jdt.internal.codeassist.impl.AssistParser.parseBlockStatements(AssistParser.java:1979)
	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.SelectionEngine.select(SelectionEngine.java:1081)
	at org.eclipse.jdt.internal.core.Openable.codeSelect(Openable.java:167)
	at org.eclipse.jdt.internal.core.CompilationUnit.codeSelect(CompilationUnit.java:389)
	at org.eclipse.jdt.internal.core.CompilationUnit.codeSelect(CompilationUnit.java:382)
	at org.eclipse.jdt.internal.ui.javaeditor.JavaElementHyperlinkDetector.detectHyperlinks(JavaElementHyperlinkDetector.java:124)
	at org.eclipse.ui.texteditor.HyperlinkDetectorRegistry$HyperlinkDetectorDelegate.detectHyperlinks(HyperlinkDetectorRegistry.java:81)
	at org.eclipse.jface.text.hyperlink.HyperlinkManager.findHyperlinks(HyperlinkManager.java:289)
	at org.eclipse.jface.text.hyperlink.HyperlinkManager.findHyperlinks(HyperlinkManager.java:262)
@iloveeclipse iloveeclipse added the bug Something isn't working label Aug 3, 2023
@srikanth-sankaran
Copy link
Contributor

Likely a duplicate of #1195

See #1195 (comment) for a growing list of duplicates.

It matches the scenario that the concerned method uses pattern matching instanceof expression.

@srikanth-sankaran srikanth-sankaran self-assigned this Aug 4, 2023
@srikanth-sankaran
Copy link
Contributor

The PR under test for #1195 i.e. #1349 fixes this problem too

@srikanth-sankaran
Copy link
Contributor

Here is a simple extract from the concerned file:

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;

public class DetectVMInstallationsJob  {

	protected void run() {
		Collection<File> candidates = null;
		Set<File> knownVMs = null;
		Collection<Object> systemVMs = Collections.EMPTY_LIST;
		if ("".equals("")) {
				systemVMs = new ArrayList<>();
				systemVMs.removeIf(t -> knownVMs.contains(null));
				for (int systemVM : new int[] { 10 }) {
					candidates.removeIf(t -> t.equals(null));
				}
		}
		for (File f : candidates) {
			String install = null;
			if (!(install instanceof String vm && vm.hashCode() != 0)) {
			}
		}
	}
}

srikanth-sankaran added a commit to srikanth-sankaran/eclipse.jdt.core that referenced this issue Sep 12, 2023
@srikanth-sankaran
Copy link
Contributor

#1349 fixes this problem and includes a regression test from this issue. Having established clearly this is a duplicate of #1195 and is subsumed by the fix for it, I will close this issue as duplicate

@srikanth-sankaran
Copy link
Contributor

Duplicate of #1195

@srikanth-sankaran srikanth-sankaran marked this as a duplicate of #1195 Sep 12, 2023
@srikanth-sankaran srikanth-sankaran closed this as not planned Won't fix, can't repro, duplicate, stale Sep 12, 2023
srikanth-sankaran added a commit to srikanth-sankaran/eclipse.jdt.core that referenced this issue Sep 12, 2023
srikanth-sankaran added a commit to srikanth-sankaran/eclipse.jdt.core that referenced this issue Sep 14, 2023
srikanth-sankaran added a commit that referenced this issue Sep 14, 2023
This is a substantial reimplementation of the code selection support for pattern matching constructs. By using auxiliary stacks to record the state of the parser and by using that state to drive the bottom up context recovery and parse tree construction, we now rebuild the parse tree to sufficient detail to ascertain liveness of pattern binding variables at the point of selection.

Fixes #1195
Fixes #769
Fixes #1263
Fixes #1360
Fixes #1364
Fixes #1278
Fixes #1288

Verifies https://bugs.eclipse.org/bugs/show_bug.cgi?id=573257
Verifies https://bugs.eclipse.org/bugs/show_bug.cgi?id=572975
Verifies https://bugs.eclipse.org/bugs/show_bug.cgi?id=576794
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants