Bug 117119 - [1.5][compiler] "Type mismatch" error on valid generics construct
Summary: [1.5][compiler] "Type mismatch" error on valid generics construct
Status: VERIFIED DUPLICATE of bug 258798
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: 3.5 M5   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-11-18 13:35 EST by Zorzella Mising name CLA
Modified: 2009-01-27 05:34 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Zorzella Mising name CLA 2005-11-18 13:35:24 EST
Javac correctly compiles the snippet below, but Eclipse shows as an error:

******************************

import java.util.Collection;

public class Fails {
  
  public static <E extends Enum<E>> void fails () {
    Class<? extends Enum> enumType = null;
    final Collection<E> test = allOf(enumType);
  }
  
  public static <E extends Enum<E>> Collection<E> allOf(final Class<E> enumType) {
    return null;
  }
}
Comment 1 Philipe Mulet CLA 2005-11-21 05:40:54 EST
Javac issues the following warnings:
X.java:7: warning: [unchecked] unchecked method invocation: <E>allOf(java.lang.Class<E>) in X is applied to (java.lang.Class<capture of ? ex
tends java.lang.Enum>)
    final Collection<E> test = allOf(enumType);
                                    ^
X.java:7: warning: [unchecked] unchecked conversion
found   : java.util.Collection
required: java.util.Collection<E>
    final Collection<E> test = allOf(enumType);
                                    ^
2 warnings
Comment 2 Philipe Mulet CLA 2005-11-21 05:42:11 EST
We issue:
----------
1. WARNING in X.java
 (at line 7)
	final Collection<E> test = allOf(enumType);
	                           ^^^^^^^^^^^^^^^
Type safety: Unchecked invocation allOf(Class<capture-of ? extends Enum>) of the generic method allOf(Class<E>) of type X
----------
2. ERROR in X.java
 (at line 7)
	final Collection<E> test = allOf(enumType);
	                           ^^^^^^^^^^^^^^^
Type mismatch: cannot convert from Collection<capture-of ? extends Enum> to Collection<E>
----------
Comment 3 Philipe Mulet CLA 2006-02-28 07:28:03 EST
This is a bug in javac, also see bug 129261 for more details.

Note that the following code exposes the issue more visually.
Both assignments to 'test' and 'test2' are equivalent in theory in term of types involved. However, javac only catches 'test2' assignment, because it is losing track of some capture through method invocation.

import java.util.Collection;

public class X {
  
  public static <E extends Enum<E>> void fails () {
    Class<? extends Enum> enumType = null;
    final Collection<E> test = allOf(enumType);

    Collection<? extends Enum> colType = null;
    final Collection<E> test2 = colType;
  }
  
  public static <E extends Enum<E>> Collection<E> allOf(final Class<E> enumType) {
    return null;
  }
}

*** This bug has been marked as a duplicate of 129261 ***
Comment 4 Philipe Mulet CLA 2006-02-28 07:29:31 EST
Added GenericTypeTest#test929
Comment 5 Philipe Mulet CLA 2009-01-05 10:55:53 EST
Reopening. I now believe we should have accepted this code.
JLS 15.12.2.6 mandates an erasure conversion in presence of unchecked conversion during applicability check.

Hence javac's behavior seem legite.
Comment 6 Philipe Mulet CLA 2009-01-05 10:58:22 EST
and actually, this bug wasn't a true dup of bug 129261
Comment 7 Philipe Mulet CLA 2009-01-08 16:07:55 EST
Given changes for bug 258798, we now pass this test.
Closing as dup

*** This bug has been marked as a duplicate of bug 258798 ***
Comment 8 Frederic Fusier CLA 2009-01-27 05:34:52 EST
Verified for 3.5M5 using build I20090127-0100.