JDK-6450691 : Wrong capture of a ?-extends wildcard with a ?-super wildcard as bound
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: linux
  • CPU: x86
  • Submitted: 2006-07-19
  • Updated: 2010-04-06
  • Resolved: 2009-01-21
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0-rc"
Java(TM) SE Runtime Environment (build 1.6.0-rc-b91)
Java HotSpot(TM) Client VM (build 1.6.0-rc-b91, mixed mode, sharing)


A DESCRIPTION OF THE PROBLEM :
The capture conversion of a ?-extends wildcard that, via the class declaration and another parameter, has a ?-super wildcard as upper bound, is calculated wrong.
This allows code with a type error to pass the compilers type checking.
Example code attached.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile (and run) the example

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Error from the compiler
ACTUAL -
Compiles (and throws a ClassCastException)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
// this code compiles without warnings, has no casts,
// but throws a ClassCastException
public class CaptureBug6b {
	public static void main(String[] args) {
		P<? super B, ? extends A> p = new P<A,A>(new A());
		B b = p.get();
	}
}

class P<U, T extends U> {
	P(T v) {t = v;}
	T t;
	T get() {return t;}
}

class A {}
class B extends A {}

---------- END SOURCE ----------

Comments
EVALUATION This is a bug: P<? super B, ? extends A> is not well-formed: 1) capture: P<#1, #2>, where: ub(#1) = Object, lower(#1) = B ub(#2) = A & #1 2) Bound checking: a) #1 <: [U:=#1]Object ok b) #2 <: [U:=#1]U = #1 #2 <: lower(#1) = B upper(#2) = A & #1 <: B no However this bug is not reproducible - the compiler now rejects the code because of 6732484 fixed in 7 b40
21-01-2009