JDK-7041730 : Regression: compiler accepts invalid cast from int to Byte
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: unknown
  • Submitted: 2011-05-04
  • Updated: 2014-06-24
  • Resolved: 2011-06-01
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 b143Fixed
Related Reports
Relates :  
Relates :  
Description
This program should be rejected:

class Test {
Byte b = (Byte) 0; 
}

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
SUGGESTED FIX A webrev of this fix is available at the following URL: http://hg.openjdk.java.net/jdk7/tl/langtools/rev/95fc7fd39be2
12-05-2011

EVALUATION This is a plain javac bug - the casting conversion routine is calling (recursively) the boxing routine in the case there's a cast from S to T where either S or T is a primitive (but not both). The boxing conversion routine does the following (approx): *) if S is primitive, check that the boxed type of S is a subtype of T *) otherwise, check that the unboxed type of S is a subtype of T So, in this case we have: int -> Byte which means we have to prove: Integer <: Byte - false but, starting from JDK 7, javac also tries to swap the operands to the boxing routine, and does the following: Byte -> int which means: byte <: int which is true (at least in the javac world). In other words, the cast will succeed if either one of the below assignment will work: void isCastOk(int i, Byte b) { i = b; //ok b = i; //error } This needs to be fixed, in order to unleash broken (or even dangerous) conversion at compile-time.
04-05-2011