Bug 165143

Summary: [1.5][compiler] Missing unchecked cast warning
Product: [Eclipse Project] JDT Reporter: Ed Merks <Ed.Merks>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: VERIFIED FIXED QA Contact:
Severity: major    
Priority: P3 CC: marcelop
Version: 3.3   
Target Milestone: 3.3 M4   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Combined fix for 165143 and 106451
none
Complementary patch none

Description Ed Merks CLA 2006-11-20 07:50:59 EST
I was trying to understand when the Eclipse compiler generates warnings about casting to an erased type.  In this example, only the lines with // lines produce warning messages:

package org.example;

import java.util.List;
import java.util.Map;

public class Warning 
{
  public static void main(String[] args)
  {
    Object object = null;
    
    List list = (List)object; // 
    
    foo((List)object);
    foo((List<?>)object);
    foo((List<Object>)object); // 
    foo((List<? extends Object>)object);
    
    foo((Map)object);
    foo((Map<?, ?>)object);
    foo((Map<Object, ?>)object);
    foo((Map<?, Object>)object);
    foo((Map<Object, Object>)object); // 
    foo((Map<? extends Object, Object>)object);
    foo((Map<? extends Object, ? extends Object>)object);
  }
  
  public static void foo(List<?> list)
  {
    System.out.println("###" + list);
  }

  public static void foo(Map<?, ?> map)
  {
    System.out.println("###" + map);
  }
}

But Javac produces more:

$ javac -Xlint:all org/example/Warning.java
org/example/Warning.java:16: warning: [unchecked] unchecked cast
found   : java.lang.Object
required: java.util.List<java.lang.Object>
    foo((List<Object>)object);
                      ^
org/example/Warning.java:21: warning: [unchecked] unchecked cast
found   : java.lang.Object
required: java.util.Map<java.lang.Object,?>
    foo((Map<Object, ?>)object);
                        ^
org/example/Warning.java:22: warning: [unchecked] unchecked cast
found   : java.lang.Object
required: java.util.Map<?,java.lang.Object>
    foo((Map<?, Object>)object);
                        ^
org/example/Warning.java:23: warning: [unchecked] unchecked cast
found   : java.lang.Object
required: java.util.Map<java.lang.Object,java.lang.Object>
    foo((Map<Object, Object>)object);
                             ^
org/example/Warning.java:24: warning: [unchecked] unchecked cast
found   : java.lang.Object
required: java.util.Map<? extends java.lang.Object,java.lang.Object>
    foo((Map<? extends Object, Object>)object);
                                       ^
5 warnings

It looks to me like Eclipse doesn't warn about the cast to the erasure if any one of the template arguments is a wildcard but Javac doesn't warn about them only if all of the template arguments are wildcards.  I think that the Javac behavior seems more consistent.

I've marked this as "major" because we are trying to eliminate all warnings in the EMF code and more importantly in the generated code EMF produces so I want to be sure that the current set of warnings are complete that we don't suddenly get new ones when oversights in where to produce warnings are fixed.
Comment 1 Philipe Mulet CLA 2006-11-22 09:23:57 EST
Tuned unchecked cast semantics. We were indeed missing some situations.
Also addressing bug 106451 at the same time, since both are related (though not the same bug).

Added GenericTypeTest#test1078-1082.
Comment 2 Philipe Mulet CLA 2006-11-22 09:25:06 EST
Created attachment 54333 [details]
Combined fix for 165143 and 106451
Comment 3 Philipe Mulet CLA 2006-11-22 09:26:33 EST
Released for 3.3M4.
Comment 4 Philipe Mulet CLA 2006-11-22 11:42:52 EST
Created attachment 54346 [details]
Complementary patch

Ensure #checkUnsafeCast is called even if compliance < 1.5
Comment 5 Philipe Mulet CLA 2006-11-27 11:26:37 EST
*** Bug 165909 has been marked as a duplicate of this bug. ***
Comment 6 Olivier Thomann CLA 2006-12-11 15:53:55 EST
Verified for 3.3M4 with I20061211-1119