Bug 78139 - [1.5][compiler] spurious type mismatch problems with generics.
Summary: [1.5][compiler] spurious type mismatch problems with generics.
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows 2000
: P3 normal (vote)
Target Milestone: 3.1 M4   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-11-09 00:56 EST by R Lenard CLA
Modified: 2005-06-01 04:19 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description R Lenard CLA 2004-11-09 00:56:43 EST
% cat Test.java
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;

public class Test
{
    public static <T> List<T> createEmptyL() {
        return new ArrayList<T>();
    }
    public static <T> Collection<T> createEmptyC() {
        return new ArrayList<T>();
    }
    public static <T> Iterable<T> createEmptyI() {
        return new ArrayList<T>();
    }
    public static void main(String[] args) {
        final List<String> lL = createEmptyL();
        final Collection<String> cL = (Collection<String>)createEmptyL();
        final Iterable<String> iL = (Iterable<String>)createEmptyL();
        final Collection<String> cC = createEmptyC();
        final Iterable<String> iC = (Iterable<String>)createEmptyC();
        final Iterable<String> iI = createEmptyI();
        
        final Collection<String> cL2 = createEmptyL();
        final Iterable<String> iC2 = createEmptyC();
    }
}

Eclipse gives spurious errors for lines 24 & 25.
Severity	Description	Resource	In Folder	Location
	Creation Time
2	Type mismatch: cannot convert from List<T> to Collection<String>
	Test.java	Fooey/src	line 24	November 9, 2004 4:38:49 PM
2	Type mismatch: cannot convert from Collection<T> to Iterable<String>
	Test.java	Fooey/src	line 25	November 9, 2004 4:38:49 PM
1	Type safety: The cast from List<T> to Collection<String> will not check 
conformance of type arguments at runtime	Test.java	Fooey/src
	line 18	November 9, 2004 4:38:49 PM
1	Type safety: The cast from List<T> to Iterable<String> will not check 
conformance of type arguments at runtime	Test.java	Fooey/src
	line 19	November 9, 2004 4:38:49 PM
1	Type safety: The cast from Collection<T> to Iterable<String> will not 
check conformance of type arguments at runtime	Test.java	Fooey/src
	line 21	November 9, 2004 4:38:49 PM

Javac also gets confused and gives spurious errors for lines 18,19,21.
c:/Eclipse/Fooey/src/Test.java:18: inconvertible types
found   : java.util.List<java.lang.Object>
required: java.util.Collection<java.lang.String>
        final Collection<String> cL = (Collection<String>)createEmptyL();
                                                                      ^
c:/Eclipse/Fooey/src/Test.java:19: inconvertible types
found   : java.util.List<java.lang.Object>
required: java.lang.Iterable<java.lang.String>
        final Iterable<String> iL = (Iterable<String>)createEmptyL();
                                                                  ^
c:/Eclipse/Fooey/src/Test.java:21: inconvertible types
found   : java.util.Collection<java.lang.Object>
required: java.lang.Iterable<java.lang.String>
        final Iterable<String> iC = (Iterable<String>)createEmptyC();
                                                                  ^
3 errors
Comment 1 Philipe Mulet CLA 2004-11-09 06:08:53 EST
Inference support did not consider downcast scenarii when collecting possible
substitutes.

Once fixed, only following warnings are issued:

----------
1. WARNING in d:\src\Test.java (at line 18)
	final Collection<String> cL = (Collection<String>)createEmptyL();
	                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Unnecessary cast from List<String> to Collection<String>
----------
----------
2. WARNING in d:\src\Test.java (at line 19)
	final Iterable<String> iL = (Iterable<String>)createEmptyL();
	                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Unnecessary cast from List<String> to Iterable<String>
----------
----------
3. WARNING in d:\src\Test.java (at line 21)
	final Iterable<String> iC = (Iterable<String>)createEmptyC();
	                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Unnecessary cast from Collection<String> to Iterable<String>
----------
Comment 2 Philipe Mulet CLA 2004-11-09 06:09:26 EST
Added regression test: GenericTypeTest#test396.
Fixed
Comment 3 Olivier Thomann CLA 2004-12-14 15:54:21 EST
Verified in 200412140800
Comment 4 Philipe Mulet CLA 2005-02-07 09:28:13 EST
Actually, new implementation of the spec 15.12.2.7 behave differently, and find
now errors on 3 of the cases: cL, iL, iC.

Note that javac also detects the same errors:
X.java:22: inconvertible types
found   : java.util.List<java.lang.Object>
required: java.util.Collection<java.lang.String>
        final Collection<String> cL = (Collection<String>)emptyList(); // 2
                                                                   ^
X.java:25: inconvertible types
found   : java.util.List<java.lang.Object>
required: java.lang.Iterable<java.lang.String>
        final Iterable<String> iL = (Iterable<String>)emptyList(); // 3
                                                               ^
X.java:31: inconvertible types
found   : java.util.Collection<java.lang.Object>
required: java.lang.Iterable<java.lang.String>
        final Iterable<String> iC = (Iterable<String>)emptyCollection(); // 5
                                                                     ^
3 errors
Comment 5 Philipe Mulet CLA 2005-02-07 09:36:31 EST
Ignore previous post. We compile this scenario with no error, even with new
inference code in place.
Comment 6 Philipe Mulet CLA 2005-06-01 04:19:05 EDT
Looks like we should reject cases (2), (3) & (5) based on the fact that return
type inference is not meant to perform through casting conversion. Only
assignment conversion is to be considered.
Also see bug 97800