Bug 313509 - [1.5][compiler] Unchecked generic type operation always wrong
Summary: [1.5][compiler] Unchecked generic type operation always wrong
Status: VERIFIED INVALID
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 RC4   Edit
Assignee: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-05-19 08:17 EDT by Marvin Fröhlich CLA
Modified: 2010-06-07 01:46 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marvin Fröhlich CLA 2010-05-19 08:17:08 EDT
Build Identifier: 20090920-1017

In the Java compiler settings I can enable "Unchecked generic type operation" to be treated as a warning. But if I do, the following is signaled as a warning.

#############
class C<X>
{
    public void m( Collection<?> p )
    {
        for ( Object o : p )
            dump( (X)o ); // <-- This is warned about!
    }
}
#############

Note, that I cannot simply change the method interface to "Collection<X>"!

The case to X is treated as a warning. And IMHO this is always wrong. Otherwise you'd have to treat every single cast (to a concrete type) as a warning or error, which nobody wants.

I don't know, if this compiler setting does something useful. But for me it just looks like a bug.

Reproducible: Always
Comment 1 Marvin Fröhlich CLA 2010-05-19 08:20:33 EDT
Additionally the quick fixes won't work. Moving the cast to a method or a local variable won't do anything. The same warning will be made there.
Comment 2 Srikanth Sankaran CLA 2010-06-02 07:39:52 EDT
Hello Marvin,

The code snippet below shows what the compiler is warning
about : 

public class C<X> {
    public void m(Object o) {
            X x = (X)o; // No exception here as this cast is "unchecked"
            String s = (String) o; // Exception here as this cast is checked
    }

	public static void main(String [] args) {
		C<String> c = new C<String>();
		c.m(new Object());
	}
}

Though both the casts in C#m() "appear" logically to be doing the
same thing i.e cast an Object to String, see that only the latter
cast throws a ClassCastException.

Type variables are not reified at run time (i.e their type information
is not completely available at runtime.) Hence neither the compiler
nor the VM can arrange to dynamically sanity check the validity of
type operations involving them. This is what the compiler is warning
about.

See that javac issues the same warning with the -Xlint:unchecked
command line argument.

You can selectively or otherwise suppress this warning as needed.
Comment 3 Satyam Kandula CLA 2010-06-07 01:46:49 EDT
Verified for 3.6RC4