JDK-6971958 : Missing unchecked warning on cast to inner class of generic class
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2010-07-25
  • Updated: 2012-03-20
  • Resolved: 2010-12-01
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_18"
OpenJDK Runtime Environment (IcedTea6 1.8) (fedora-41.b18.fc13-x86_64)
OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Linux mattlaptop2.local 2.6.33.6-147.fc13.x86_64 #1 SMP Tue Jul 6 22:32:17 UTC 2010 x86_64 x86_64 x86_64 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
javac fails to report an unchecked warning when an object is casted to an inner class of the generic class in which the cast appears, though such a cast is not type-safe.  See the test case.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the test case.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
An unchecked warning at the indicated line.
ACTUAL -
The code compiles without warnings.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
class Outer<T> {

	class Cell {
		final T value;
		Cell(T value) {
			this.value = value;
		}
	}
	
	Outer<T>.Cell cast(Outer<?>.Cell o) {
		return (Cell) o; // expected: unchecked warning
	}
	
	public static void main(String[] args) {
		Outer<Integer>.Cell intCell = new Outer<Integer>().new Cell(314);
		Outer<String>.Cell strCell = new Outer<String>().cast(intCell);
		String val = strCell.value; // ClassCastException
		System.out.println(val);
	}
}
---------- END SOURCE ----------