Bug 84345

Summary: [1.5][compiler] Widening reference is possible when boxing in a cast expression
Product: [Eclipse Project] JDT Reporter: Olivier Thomann <Olivier_Thomann>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3    
Version: 3.1   
Target Milestone: 3.1 M5   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Apply on HEAD none

Description Olivier Thomann CLA 2005-02-03 11:57:29 EST
In a cast expression, it is possible to cast a byte into Number.
byte b = 0;
Number n = (Number) b;

is legal. Right now we report an incompatible cast.

The test in Expression checks for == in case of boxing instead of
isCompatibleWith(...).
Comment 1 Olivier Thomann CLA 2005-02-03 11:59:01 EST
Created attachment 17659 [details]
Apply on HEAD

I am running all the tests now.
With this patch, we report the cast as unnecessary. Indeed, if you remove the
cast, it compiles perfectly fine.
Comment 2 Olivier Thomann CLA 2005-02-03 13:01:42 EST
With this patch, everything looks good for the cast expression, but there is a
side effect in the EqualExpression.
The following code doesn't report an error anymore:

public class X {
  public Object foo() {
    java.io.Serializable o = null;
    if (o == 0) return o;
    return this;
  }
}

The JLS3 don't require the types to be cast compatible anymore. They refer to
types that need to be convertible to numeric types. java.io.Serializable is not
convertible to a numeric types.
Check 5.1.8 section.
Comment 3 Olivier Thomann CLA 2005-02-03 14:28:43 EST
Forget what I said in comment 2. This was wrong. In case of reference type, the
casting conversion still apply. I need to investigate what should be the fix in
the cast check to handle the equality expressions.
Comment 4 Philipe Mulet CLA 2005-02-07 12:34:23 EST
I believe the spec is a bit unprecise. It does perform numeric comparison as
soon as any of the operand is a numeric type.
Adjusted the implementation along this idea.

Added AutoboxingTest#test089.
Fixed
Comment 5 Jerome Lanneluc CLA 2005-02-15 09:00:42 EST
Verified in I20050214