JDK-6569277 : inference: uninferred type args should follow their upper bounds
  • Type: Bug
  • Component: specification
  • Sub-Component: language
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2007-06-13
  • Updated: 2010-10-04
  • Resolved: 2010-10-04
Related Reports
Relates :  
Relates :  
Description
javac accepts this case:

class X {
  static <T,U extends Number> void f(T x) { }
  public static void main(String[] args) { f(new Integer(37)); }
}

It is claimed that, according to the spec, a type argument that cannot be inferred from the call is supposed to be given the type Object, but Object does not satisfy the bound of U; one would therefore expect a compiler to reject the code.

Comments
EVALUATION It is obvious that JLS3 15.12.2.8 should make a formal type parameter that is not inferred from actual arguments, be inferred as its upper bound, not Object. (If there is no explicit upper bound, the upper bound is Object anyway.) And indeed, JLS3 does not say "a type argument that cannot be inferred from the call is supposed to be given the type Object". A careful reading of 15.12.2.8 reveals that, since U is not inferred from the call and the call is not subject to assignment conversion, inference restarts assuming the call _is_ subject to assignment to Object. This gives rise to a constraint that takes bounds into account (namely, Number >> U). Eventually everything works out. javac should not reject the code.
13-06-2007