JDK-8207224 : Javac compiles source code despite illegal use of unchecked conversions
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8,9,10,11
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2018-07-12
  • Updated: 2018-12-21
  • Resolved: 2018-12-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 13
13 b01Fixed
Related Reports
CSR :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
As recommened by Eclipse IDE Support, I'd like to report my issue to javac team.

See details, attached source code and analysis already done by Eclipse team here:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=536978


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile attached java code
https://bugs.eclipse.org/bugs/attachment.cgi?id=274960

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
javac should generate compilation error the same as Eclipse does:
>> The return type is incompatible with AbstractDemo<Request,Response>.test(Request)
ACTUAL -
javac compiles the code without any errors

---------- BEGIN SOURCE ----------
Source code is attached to Eclipse Bug 536978
https://bugs.eclipse.org/bugs/show_bug.cgi?id=536978

Direct link to the attachment:
https://bugs.eclipse.org/bugs/attachment.cgi?id=274960
---------- END SOURCE ----------

FREQUENCY : always



Comments
Evaluation: yes, this is a javac bug. The question is whether this method: SimpleResult test(AbstractResult request) properly overrides this method: Response test(Request request) Where: type variable Request extends AbstractResult type variable Response extends AbstractResult interface SimpleResult extends AbstractResult When declared in appropriate classes, the first method overrides the second (JLS 8.4.8.1), because the first signature is a subsignature of the second (JLS 8.4.2). Per JLS 8.4.8.3, the first method must be return-type-substitutable for the second. This can be satisfied in 3 ways (JLS 8.4.5): - SimpleResult is a subtype of Response (nope) - SimpleResult can be converted to a subtype of Response by unchecked conversion (nope) - The methods have different signatures (yep) and SimpleResult = erasure(Response) (nope) No case applies, so an error should occur. Some clarifying comments: - The error/warning messages (of both javac and Eclipse) refer to "unchecked conversion", but there is none here: unchecked conversion (JLS 5.1.9) only inserts type arguments for raw types, it doesn't convert to a type variable type. (Yes, it's confusing that "unchecked warnings" and "unchecked casts" may not involve "unchecked conversions".) - Note that the third requirement is SimpleResult = erasure(Response), not SimpleResult <: erasure(Response). The language deliberately uses the less permissive operator.
30-11-2018

This looks like issue in javac, where as eclipse compiler gives error " - The return type is incompatible with AbstractDemo<Request,Response>.test(Request) - implements AbstractDemo<Request,Response>.test" Javac compiler doesn't give any error and throw ClassCastException at runtime -sh-4.2$ /scratch/fairoz/JAVA/jdk11/jdk-11-ea+21/bin/javac SimpleDemo.java Note: SimpleDemo.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. -sh-4.2$ /scratch/fairoz/JAVA/jdk11/jdk-11-ea+21/bin/java SimpleDemo Exception in thread "main" java.lang.ClassCastException: class Result1 cannot be cast to class OtherResult (Result1 and OtherResult are in unnamed module of loader 'app') at SimpleDemo.main(SimpleDemo.java:18)
13-07-2018