JDK-6811943 : inference: javac 1.6 & Eclipse infer more types than JLS (umbrella CR)
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: unknown
  • Submitted: 2009-03-02
  • Updated: 2014-10-08
  • Resolved: 2014-07-31
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 8
8Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
The original implementation of type-inference was very different w.r.t. to what is mandated by JLS3. In particular one of the features of jdk 5/6 javac that is not supported by the JLS is the ability of applying inference from argument types (15.12.2.7) several times (not just one as JLS mandate) until no further progress is made. This feature allows javac to infer more types than JLS does, e.g.

1) class MyMap implements Map<String, Integer> { ... }

<Z extends Map<X,Y>, X, Y> X getMap(Z z) { ... }

getMap(new MyMap());

This calls infers Z = MyMap and then, in a subsequent inference round, also X = String and Y = Integer are derived.

2) Another common situation where javac behaved differently w.r.t. the JLS3 is the following:

<X extends Y, Y> Y get(X x) { ... }

get("Hello!");

In this case, the first inference round infers X = String, but also, as a side effect sets a lower bound (String) on Y; this lower bound can be exploited in a subsequent inference round so that Y = lub(String) = String.

Bottom line: javac's type-inference used to be more powerful than JLS type-inference. In an attempt to make javac more compliant w.r.t. to JLS, a CCC request has been submitted 2 years ago and the jdk 7 compiler (and 6-open as well, as the fix got in before the 6-open branch) is now more JLS-compliant; which unfortunately means that javac now rejects a lot of code that used to compile without problems.

Comments
JLS 8 introduces the "incorporation" phase of inference (18.3), which includes the logic being requested.
31-07-2014

EVALUATION Local changes to JLS 15.12.2.7/8 are desirable to make it accept more programs. The changes are in the SeeAlso CRs.
05-03-2009