JDK-6299211 : method type variable: inference broken for null
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0
  • Priority: P5
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-07-20
  • Updated: 2011-02-16
  • Resolved: 2006-12-05
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_03-b07)
Java HotSpot(TM) Client VM (build 1.5.0_03-b07, mixed mode)

A DESCRIPTION OF THE PROBLEM :
This program does not compile:

public class Try {
	void m() {
		java.util.Collections.max(null);
	}
}

incompatible types; inferred type argument(s) java.lang.Comparable<? super T> do not conform to bounds of type variable(s) T
found   : <T>T
required: java.lang.Object
		java.util.Collections.max(null);
                                         ^
1 error


REPRODUCIBILITY :
This bug can be reproduced always.
###@###.### 2005-07-20 09:52:48 GMT

Comments
EVALUATION This is not a bug. The inference algorithm cannot gather any information from the argument (null) and the method is not called in a place where there are any expectations on the returned value. In such cases the compiler must infer java.lang.Object for the type variable. However, Object is not a subtype of Comparable<? super Object> and thus not within the bounds of the type variable. After fixing 6369605, the error message reflects this more precisely: incompatible types; inferred type argument(s) java.lang.Object do not conform to bounds of type variable(s) T found : <T>T required: java.lang.Object java.util.Collections.max(null); ^ However, all these should compile: public class Test { String m() { String s = java.util.Collections.max(null); java.util.Collections.<String>max(null); return java.util.Collections.max(null); } } And will when 6369605 is fixed. I'm including the above as a regression test.
05-12-2006