JDK-7005671 : Regression: compiler accepts invalid cast from X[] to primitive array
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: unknown
  • Submitted: 2010-12-09
  • Updated: 2011-03-08
  • Resolved: 2011-03-08
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 7
7 b123Fixed
Related Reports
Relates :  
Relates :  
Description
This program should be rejected:

class Test<X> {
    Object o = (byte[])(X[])null;
}

Instead, the compiler accepts it, starting from JDK 7 b112. The problem is likely to be related to the changes in cats conversion involving 292 (see 6979683).

Comments
EVALUATION http://hg.openjdk.java.net/jdk7/build/langtools/rev/2ca5866a8dfb
25-12-2010

SUGGESTED FIX A webrev of this fix is available at the following URL: http://hg.openjdk.java.net/jdk7/tl/langtools/rev/2ca5866a8dfb
10-12-2010

EVALUATION This is related to 6979683. The implementation of the new conversion rules (needed to support JSR 292), are showing an unexpected interaction with other rules; the problem lies in how cast conversion between array types is implemented; if javac sees a cast from S[] to T[], two cases apply: *) if S is a primitive type, then ensure that S == T *) otherwise ensure that S is castable to T In this case, S is not a primitive (type-variable X), so the second rule applies. The second rule causes recursion, which means that the visitor for type-variable is called; which means that X is castable to T iff upper(X) is castable to T, which is: upper(X) = Object castable to byte Here we are with a (now) 'legal' cast from Object to byte. The problem is that we should never have started recursion in the first place. The JLS are very clear about cast conversion and arrays; recursion should only happen when _both_ array element types are reference types, which clearly don't apply in this case.
09-12-2010