JDK-6558543 : Widening reference conversion followed by unboxing isn't allowed
  • Type: Bug
  • Component: specification
  • Sub-Component: language
  • Affected Version: 5.0
  • Priority: P5
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2007-05-17
  • Updated: 2016-09-20
  • Resolved: 2011-07-22
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 rcFixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Description
According to 5.2 (assignment conversions) the second shouldn't be allowed, as it would be a widening reference conversion followed by an unboxing conversion, which isn't among the allowed conversions. However, I believe that is a bug in the JLS. (One could infer that T must be Integer, as Integer is final, but the type rules don't take advantage of final). A widening reference conversion followed by unboxing isn't listed among the conversions allowed in the corrections in http://java.sun.com/docs/books/jls/jls-proposed-changes.html .  Perhaps Alex will get it fixed in the next round.

-Neal

On 5/1/07, ... wrote:
I'm curious about this case:
        
        class A<T extends Integer> {
                     T x = (T)37;    // javac gives error
                     int i = (int)x; // javac gives no error
        }
        
        javac gives an error on the first case but not the second.  The JLS says 
        in 5.5:
        
           A value of a primitive type can be cast to a reference type by
        boxing conversion (��5.1.7).
           A value of a reference type can be cast to a primitive type by
        unboxing conversion (��5.1.8).
        
        It's not clear whether that means one of those conversions plus
        something else.
        If I read it as only boxing or unboxing, the error on the first case
        makes sense.
        But then I would also expect an error on the second case. 
        
        Which way is right?  Is there a javac bug here on one of these cases?

Comments
EVALUATION The sense of casting conversion is to permit ONE of the list in 5.5. The conversions required here are: 1) Boxing optionally following by narrowing reference 2) Widening reference optionally followed by unboxing I agree that (2) should be allowed as it is semantically similar to the proposal in 6526446 to allow "boxing followed by widening reference". 6526446 should say: "- a widening reference conversion optionally followed by either an unboxing conversion or an unchecked conversion." And if you have that, you ought to also have: "- a narrowing reference conversion optionally followed by either an unboxing conversion or an unchecked conversion". As for (1): if you can box then widen (as per 6526446), then you ought to be able to box then narrow. The boxing aspect is orthogonal to the compile/run-time check on the class hierarchy. However, I don't plan to allow boxing-then-narrowing yet.
17-05-2007