JDK-6182630 : raw bounds on type variables avoids unchecked warning
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: linux,windows_xp
  • CPU: x86
  • Submitted: 2004-10-21
  • Updated: 2010-04-02
  • Resolved: 2004-11-06
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b12Fixed
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Linux 2.4.22-1.2199.nptl #1 Wed Aug 4 12:25:07 EDT 2004 i686 athlon i386 GNU/Linux
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
As mentioned on the forums:
http://forum.java.sun.com/thread.jsp?forum=316&thread=563260

The type-safety provided by compile-time warnings can be circumvented through the use of a parameterised method, where the parameter is bound to a raw type.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the provided code, note the lack of any warnings. A String is added to a Collection<Integer>, resulting in a ClassCastException at runtime - exactly the sort of behaviour that the compiler is supposed to catch.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Warnings will always be emitted when type-safety is compromised when no explicit casting is done.
ACTUAL -
There are no warnings for the type-unsafe behaviour described in this report.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Paradoxically, if there were any, this wouldn't be an error.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.*;

public class TypeUnsafe
{
	static public <T extends Collection> void workaround(T a, T b)
	{
		a.addAll(b);
	}

	static public void main(String[] args)
	{
		Collection<Integer> a = new ArrayList<Integer>();
		Collection<String> b = new ArrayList<String>();
		b.add("Bwahahaha");
		workaround(a, b);
		System.out.println(a.iterator().next().intValue()); // ClassCastException
	}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Don't use methods with parameters bound to raw types, or don't trust that you will be warned about type-unsafe behaviour if you do.
###@###.### 10/21/04 17:15 GMT

Comments
SUGGESTED FIX Index: src/share/classes/com/sun/tools/javac/comp/Attr.java =========================================================== @@ -1911,14 +1911,14 @@ pkind == VAR && v.owner.kind == TYP && (v.flags() & STATIC) == 0 && - site.tag == CLASS) { + (site.tag == CLASS || site.tag == TYPEVAR)) { Type s = types.asOuterSuper(site, v.owner); if (s != null && s.isRaw() && !types.isSameType(v.type, v.erasure(types))) { chk.warnUnchecked(tree.pos, "unchecked.assign.to.var", - v, site); + v, s); } } // The computed type of a variable is the type of the @@ -2043,14 +2043,14 @@ // an unchecked warning if its argument types change under erasure. if (allowGenerics && (sym.flags() & STATIC) == 0 && - site.tag == CLASS) { + (site.tag == CLASS || site.tag == TYPEVAR)) { Type s = types.asOuterSuper(site, sym.owner); if (s != null && s.isRaw() && !types.isSameTypes(sym.type.argtypes(), sym.erasure(types).argtypes())) { chk.warnUnchecked(env.tree.pos, "unchecked.call.mbr.of.raw.type", - sym, site); + sym, s); } } =========================================================== ###@###.### 10/22/04 01:35 GMT
21-10-2004

EVALUATION This is a compiler bug. A warning should have been issued on this line: a.addAll(b); ###@###.### 10/21/04 21:46 GMT
21-10-2004