Bug 287607 - [1.5][compiler] cast of inner of generic enclosing type are not reported as unsafe
Summary: [1.5][compiler] cast of inner of generic enclosing type are not reported as u...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.5   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: 3.6 M3   Edit
Assignee: Kent Johnson CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-08-25 15:44 EDT by Rémi Forax CLA
Modified: 2009-10-27 04:00 EDT (History)
2 users (show)

See Also:


Attachments
Proposed patch and testcase (5.73 KB, patch)
2009-10-02 15:33 EDT, Kent Johnson CLA
no flags Details | Diff
Proposed patch and testcase (3.18 KB, patch)
2009-10-02 16:00 EDT, Kent Johnson CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Rémi Forax CLA 2009-08-25 15:44:00 EDT
Build ID: I20090806-1400

Steps To Reproduce:
1. Eclipse doesn't report an unsafe cast with the following snippet 
   (from http://www.dzone.com/links/two_java_typesystem_holes.html)
   The cast in getOtherElement should generates an unsafe cast warning.

class Outer<E> {
  Inner inner;

  class Inner {
    E e;

    E getOtherElement(Object other) {
      if (!(other instanceof Outer.Inner)) {
        throw new IllegalArgumentException(String.valueOf(other));
      }

      Inner that = (Inner) other;
      return that.e;
    }
  }

  public static void main(String[] args) {
    Outer<String> s = new Outer<String>();
    s.inner = s.new Inner();
    s.inner.e = "hello";

    Outer<Integer> i = new Outer<Integer>();
    i.inner = i.new Inner();
    i.inner.e = 1234;

    String producesClassCast = s.inner.getOtherElement(i.inner);
  }
}

More information:
javac 1.6 doesn't raise this warning too, but javac 1.7 does.

Rémi
Comment 1 Kent Johnson CLA 2009-09-03 12:45:07 EDT
Is this really an unsafe cast ?

Does the instanceof check above the cast, not mean its 'safe' ?
Comment 2 Rémi Forax CLA 2009-09-03 19:29:21 EDT
(In reply to comment #1)
> Is this really an unsafe cast ?
> 
> Does the instanceof check above the cast, not mean its 'safe' ?

Hi Kent,
No, JLS says that a cast is safe if it's a reified type,
i.e a type that the VM knows.

Inner is equivalent to Outer<E>.Inner,
so it's not a reified type so an unsafe cast warning is required.

BTW, here instanceof is done on a raw type,
it should be written:
  if (!(other instanceof Outer<?>.Inner))

Because generics aren't reified in Java,
the VM will not check the type argument of E.

Rémi
Comment 3 Kent Johnson CLA 2009-10-02 15:33:32 EDT
Created attachment 148682 [details]
Proposed patch and testcase
Comment 4 Kent Johnson CLA 2009-10-02 15:42:30 EDT
Released in HEAD for 3.6M3
Comment 5 Kent Johnson CLA 2009-10-02 16:00:06 EDT
Created attachment 148685 [details]
Proposed patch and testcase

Actually prefer this change instead
Comment 6 Kent Johnson CLA 2009-10-02 16:00:30 EDT
Released latest patch in HEAD for 3.6M3
Comment 7 Srikanth Sankaran CLA 2009-10-27 04:00:38 EDT
Verified for 3.6M3 using build I20091025-2000