JDK-8144209 : Type variable capture fails to imply type of lower bound via loose compatibility
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux_ubuntu
  • CPU: x86_64
  • Submitted: 2015-10-24
  • Updated: 2017-09-07
  • Resolved: 2015-12-09
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
javac 1.8.0_60

java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Linux eli-desktop 4.2.0-16-generic #19-Ubuntu SMP Thu Oct 8 15:35:06 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
Type variable capture fails during inference of a type variable under certain circumstances.

Consider the following method declaration, where T is a type parameter on the declaring class:

    static <T> void test(List<? super T> list) {}

And the following invocation:

    Test.test((List<?>) null);

I would expect T to be inferred as a type variable capture with an upper bound on Object.

But compilation fails with javac. It passes with EDT, and with my own implementation... Are we wrong?

My interpretation of the spec is that this should be fairly straightforward; the invocation implies the constraint:

���List<CAP#1> <: List<? super T>���

which reduces to:

���? super T <= CAP#1���

which reduces to:

���T <: CAP#1���

and so T is ultimately inferred to be CAP#1 during resolution

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Try to compile the source given below

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The source should compile
ACTUAL -
There is an error...

ERROR MESSAGES/STACK TRACES THAT OCCUR :
error: method test in class Test cannot be applied to given types;
		Test.test(list);
		    ^
  required: List<? super U>
  found: List<CAP#1>
  reason: cannot infer type-variable(s) U
    (argument mismatch; List<CAP#1> cannot be converted to List<? super U>)
  where U is a type-variable:
    U extends Object declared in method <U>test(List<? super U>)
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Object from capture of ?
1 error


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
class Test {
	static <T> void test(List<? super T> list) {}

	void fail(List<?> list) {
		Test.test(list);
	}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
In general, none exists that I'm aware of.


Comments
The changes between these two builds are: 2397[jdk9-b12]:2396,2395 72efbe612e49 2014-05-01 14:21 -0700 lana Merge 2398 9cfffe95a5fc 2014-05-02 22:35 +0100 pgovereau 8033437: javac, inconsistent generic types behaviour when compiling together vs. separate 2399 5fbdcc2e3a75 2014-05-02 16:41 -0700 jjg 8042358: some tests have placeholder bugid 1234567 2400 54a460e0ac76 2014-05-06 15:46 -0600 dlsmith 8033718: Inference ignores capture variable as upper bound 2401 5df0952dff0e 2014-05-07 14:22 -0700 anazarov 8040129: Implement classfile tests for SourceFile attribute. 2402 08889445d3dc 2014-05-07 23:55 +0200 ohrstrom 8042699: sjavac does not track dependencies 2403 aba030ee43b5 2014-05-08 00:22 +0200 ohrstrom 8042441: sjavac does not track dependencies 2404 39f1e6271591 2014-05-07 15:54 -0700 ksrini 8042654: [javadoc] revert the default methods list.sort to Collections.sort 2405 baf35a88504b 2014-05-07 17:29 +0200 alundblad 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath. 2406:2397 955d619d42e6 2014-05-08 01:05 -0700 katleman Added tag jdk9-b12 for changeset 72efbe612e49 2407[jdk9-b13]:2406,2405 2c8bb81b5d48 2014-05-08 15:28 -0700 lana Merge so likely candidates are 2398 or 2400.
09-12-2015

First of all the test is missing import java.util.List, adding that, the test works with latest jdk9 but fails with jdk7 and jdk8, this was fixed in jdk9-b13, so you might want to look at the changesets between b12 and b13. This is the error with b12: $ /java/jdk/9/promoted/all/b12/binaries/windows-x64/bin/javac Test.java Test.java:6: error: method test in class Test cannot be applied to given types; Test.test(list); ^ required: List<? super T> found: List<CAP#1> reason: cannot infer type-variable(s) T (argument mismatch; List<CAP#1> cannot be converted to List<? super T>) where T is a type-variable: T extends Object declared in method <T>test(List<? super T>) where CAP#1 is a fresh type-variable: CAP#1 extends Object from capture of ? 1 error
09-12-2015

Test Result: ########## OS: Oracle Linux 7- 64 bit ********************************** JDK: 8u0 b132 : Fail 8u51 b16 : Fail 8u60 b27 : Fail 9-ea b80 : Pass ####################################################################################### 8u0,8u51,8u60: ******************** [ababroy@localhost tools]$ java -version java version "1.8.0_60" Java(TM) SE Runtime Environment (build 1.8.0_60-b27) Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode) [ababroy@localhost tools]$ [ababroy@localhost tools]$ javac Test.java Test.java:7: error: method test in class Test cannot be applied to given types; Test.test(list); ^ required: List<? super T> found: List<CAP#1> reason: cannot infer type-variable(s) T (argument mismatch; List<CAP#1> cannot be converted to List<? super T>) where T is a type-variable: T extends Object declared in method <T>test(List<? super T>) where CAP#1 is a fresh type-variable: CAP#1 extends Object from capture of ? 1 error [ababroy@localhost tools]$ 9-ea b64,b80, b92: ************************ [ababroy@localhost tools]$ java -version java version "1.9.0-ea" Java(TM) SE Runtime Environment (build 1.9.0-ea-b80) Java HotSpot(TM) 64-Bit Server VM (build 1.9.0-ea-b80, mixed mode) [ababroy@localhost tools]$ javac Test.java [ababroy@localhost tools]$ ################################################################################################################################################################################ OS: Windows 7 - 64 bit ***************************** JAVAC, Netbeans IDE ***************************** 8u0 b132 : Fail 8u65 b17 : Fail 9-ea b 64 : Fail 9-ea b 94 : Pass ****************************** Eclipse IDE Luna ***************************** 8u0 b132 : Fail 8u65 b17 : Fail ***************************** Eclipse IDE Mars ***************************** 8u0 b132 : Pass 8u65 b17 : Pass ######################################################################################## 8u0,8u65,9-ea: [JAVAC] ******************** ********** c:\Abhijit\tools>java -version java version "1.8.0" Java(TM) SE Runtime Environment (build 1.8.0-b132) Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode) c:\Abhijit\tools>javac Test1.java Test1.java:7: error: cannot find symbol Test.test((List<? super T>) list); ^ symbol: class T location: class Test1 Test1.java:7: error: cannot find symbol Test.test((List<? super T>) list); ^ symbol: method test(List<CAP#1>) location: class Test where CAP#1 is a fresh type-variable: CAP#1 extends Object super: T from capture of ? super T 2 errors c:\Abhijit\tools> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
09-12-2015