Return type inference: no unique maximal instance exists difference between 1.6 and 1.7 javac.

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Mon Nov 29 03:31:47 PST 2010


Hi Dawid
> 1) Is the difference between javac 1.6 and 1.7 a bug in 1.6's
> compiler? I checked a few past snapshots for 1.7 and they all worked,
> so it must be a change done a good while ago. If this was a bug at
> some point, is there a bugs parade entry for it anywhere (I did search
> for something matching this, but couldn't find anything).
>    
This is a bug in javac 6 - see:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6302954

[as you noted this one got fixed very early in 7]
> 2) We both tried to formally prove the above code correct or incorrect
> wrt the Java specification, but the complexity of type inference
> rules, especially in assignment expressions, is, ehm, overwhelming. We
> would appreciate if somebody could explain what the actual process of
> inferring the type of B (or A) is in the above example;
>    
Type inference is a process structured in two steps:

1) first, generic method type-arguments are inferred from the actual 
argument types supplied in the method call - this section is covered in 
JLS 3rd 15.12.2.7; since there are no actuals here, it doesn't apply in 
this particular case.

2) Secondly, any uninferred type-variable is inferred by collecting 
constraints from (i) declared bounds of type-variables and (ii) expected 
type in assignment context. This rules are described in JLS 3rd, 15.12.2.8.

If we apply (2) in this particular case:

public<A>  A x() { ... }

public<B>  void y() {
    B o = x(); //we need to infer a type for A
}


We have that A's bound is Object. Also, from the assignment context, we 
have that the inferred type for A must be a subtype of B (written A <: B).

More formally:

A <: Object
A <: B

Section 15.12.2.8 says that the inferred type for A is the glb() of all 
the constraint we were able to collect:

A = glb(Object, B) = B

Which means that, in this example, the method call to x() is legal and 
the A type-variable should be inferred to be B.

Maurizio






More information about the compiler-dev mailing list