Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 423582

Summary: [1.8] CCE in TypeBinding.isCastCompatible for PolyTypeBinding
Product: [Eclipse Project] JDT Reporter: Markus Keller <markus.kell.r>
Component: CoreAssignee: Srikanth Sankaran <srikanth_sankaran>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: noopur_gupta, shankhba, srikanth_sankaran
Version: 4.4   
Target Milestone: BETA J8   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Bug Depends on:    
Bug Blocks: 423585    
Attachments:
Description Flags
Patch under test none

Description Markus Keller CLA 2013-12-09 07:04:49 EST
CCE when hovering over the reference to 'goo'. Note that 'I' is not a functional interface, and the compiler correctly reports errors for that.

interface I { }

public class X {
    static void goo(I i) {
        System.out.println("goo(I)");
    }
    
    public static void main(String[] args) {
        goo(s -> 0); // hover over 'goo' -> CCE
    }
}

java.lang.ClassCastException: org.eclipse.jdt.internal.compiler.lookup.PolyTypeBinding cannot be cast to org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding
	at org.eclipse.jdt.internal.compiler.ast.Expression.checkCastTypesCompatibility(Expression.java:504)
	at org.eclipse.jdt.core.dom.TypeBinding.isCastCompatible(TypeBinding.java:1056)
	at org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsSubProcessor.doEqualNumberOfParameters(UnresolvedElementsSubProcessor.java:1599)
	at org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsSubProcessor.addParameterMissmatchProposals(UnresolvedElementsSubProcessor.java:1336)
	at org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsSubProcessor.getMethodProposals(UnresolvedElementsSubProcessor.java:1109)
	at org.eclipse.jdt.internal.ui.text.correction.QuickFixProcessor.process(QuickFixProcessor.java:337)
	at org.eclipse.jdt.internal.ui.text.correction.QuickFixProcessor.getCorrections(QuickFixProcessor.java:300)
	at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor$SafeCorrectionCollector.safeRun(JavaCorrectionProcessor.java:378)
	at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor$SafeCorrectionProcessorAccess.run(JavaCorrectionProcessor.java:339)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor$SafeCorrectionProcessorAccess.process(JavaCorrectionProcessor.java:335)
	at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor.collectCorrections(JavaCorrectionProcessor.java:468)
	at org.eclipse.jdt.internal.ui.text.java.hover.ProblemHover$ProblemInfo.getJavaAnnotationFixes(ProblemHover.java:225)
	at org.eclipse.jdt.internal.ui.text.java.hover.ProblemHover$ProblemInfo.getCompletionProposals(ProblemHover.java:199)
	at org.eclipse.jdt.internal.ui.text.java.hover.AbstractAnnotationHover$AnnotationInformationControl.deferredCreateContent(AbstractAnnotationHover.java:285)
	at org.eclipse.jdt.internal.ui.text.java.hover.AbstractAnnotationHover$AnnotationInformationControl.setInput(AbstractAnnotationHover.java:186)
	at org.eclipse.jface.text.AbstractInformationControlManager.internalShowInformationControl(AbstractInformationControlManager.java:1181)
	at org.eclipse.jface.text.AbstractInformationControlManager.presentInformation(AbstractInformationControlManager.java:1150)
	at org.eclipse.jface.text.AbstractHoverInformationControlManager.presentInformation(AbstractHoverInformationControlManager.java:902)
	at org.eclipse.jface.text.TextViewerHoverManager.doPresentInformation(TextViewerHoverManager.java:243)
	at org.eclipse.jface.text.TextViewerHoverManager$5.run(TextViewerHoverManager.java:233)
	at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
	at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
	at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4145)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3762)
Comment 1 Markus Keller CLA 2013-12-09 10:19:05 EST
There are more places where PolyTypeBinding cannot be cast to ReferenceBinding, e.g. from bug 423585 Issue 2 2):

java.lang.ClassCastException: org.eclipse.jdt.internal.compiler.lookup.PolyTypeBinding cannot be cast to org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding
	at org.eclipse.jdt.core.dom.TypeBinding.getInterfaces(TypeBinding.java:501)
	at org.eclipse.jdt.internal.ui.text.correction.ASTResolving.collectRelaxingTypes(ASTResolving.java:1000)
	at org.eclipse.jdt.internal.ui.text.correction.ASTResolving.getRelaxingTypes(ASTResolving.java:994)
	at org.eclipse.jdt.internal.ui.text.correction.proposals.NewMethodCorrectionProposal.evaluateParameterType(NewMethodCorrectionProposal.java:260)
Comment 2 Srikanth Sankaran CLA 2013-12-09 17:44:26 EST
PolyTypeBinding is a placeholder/proxy for the type of an expression that
cannot be typed "yet".  These should vanish from the picture be replaced by the
correct type once the deferred typing is complete or by either null or a 
ProblemReferenceBinding if the context does not furnish a valid type. This 
last step is missing. Should be a straightforward fix. I'll work on it tomorrow.
Comment 3 shankha banerjee CLA 2013-12-10 09:30:49 EST
Working on it.
Comment 4 Srikanth Sankaran CLA 2013-12-13 03:52:25 EST
Shankha, do you see this problem after pulling in http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?h=BETA_JAVA8&id=a98d7d87df415161ba75f53acbdbe8d316ea160c ? 

I don't.
Comment 5 shankha banerjee CLA 2013-12-13 04:06:09 EST
The problem is resolved after pulling n from the latest code.

Thanks
Comment 6 Srikanth Sankaran CLA 2013-12-13 05:12:24 EST
I do - I don't quite know how to consistently see it though.
Comment 7 Srikanth Sankaran CLA 2013-12-13 05:26:17 EST
Created attachment 238326 [details]
Patch under test

I noticed an egregiously bad coding pattern that looks like:

return this.resolvedType = new PolyTypeBinding();

That is not correct since we are in no position to set the resolvedType field
at that point (i.e in an invocation context, with no expected type ...). This
also has the potential to leak PolyTypeBindings to escape the overload resolution
process and leak into perpetuity.

Patch under test
Comment 8 Noopur Gupta CLA 2013-12-13 05:44:15 EST
(In reply to Srikanth Sankaran from comment #7)
> Created attachment 238326 [details] [diff]
> Patch under test
Verified that the patch resolves the CCE issue.
Comment 9 Srikanth Sankaran CLA 2013-12-13 05:46:11 EST
(In reply to Noopur Gupta from comment #8)
> (In reply to Srikanth Sankaran from comment #7)
> > Created attachment 238326 [details] [diff]
> > Patch under test
> Verified that the patch resolves the CCE issue.

Thanks Noopur. All JDT/Core tests are green. Released the fix here: 
http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?h=BETA_JAVA8&id=3a372fa727520c809be84deca2b7b9c51a2e6869
Comment 10 Srikanth Sankaran CLA 2013-12-13 05:53:12 EST
*** Bug 423574 has been marked as a duplicate of this bug. ***