Bug 156183 - [1.5][compiler] Generics discrepency with Sun compiler
Summary: [1.5][compiler] Generics discrepency with Sun compiler
Status: RESOLVED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: Macintosh Mac OS X - Carbon (unsup.)
: P3 normal (vote)
Target Milestone: 3.3 M2   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-09-05 07:20 EDT by Jason Frame CLA
Modified: 2006-09-07 16:12 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 Jason Frame CLA 2006-09-05 07:20:49 EDT
This code compiles with eclipse 3.2 & 3.3M1 without error (there is the expected warning about the variable set not be used). However it fails to compile with the sun compiler version 1.5.06.

When I compile the code with the sun compiler I get the following errors:

[checking GenericsTest]
GenericsTest.java:12: incompatible types
found   : java.util.Set<GenericsTest.B>
required: java.util.Set<GenericsTest.A>
        Set<A> set = g.newSet(g.new B());

I'm not sure who is correct here. The generics syntax looks correct to me.

import java.util.LinkedHashSet;
import java.util.Set;

public class GenericsTest {

    public class A {};

    public class B extends A {};

    public static void main(String[] args) {
        GenericsTest g = new GenericsTest();
        Set<A> set = g.newSet(g.new B());
    }

    public <T, V extends T> Set<T> newSet(V... objects) {
        Set<T> set = new LinkedHashSet<T>();
        for (T t : objects) {
            set.add(t);
        }
        return set;
    }
Comment 1 Philipe Mulet CLA 2006-09-05 07:48:45 EDT
I believe the Eclipse compiler is right, and javac wrong.
According to JLS 15.12.2.8, the return type Set<A> should be inferred from to bind the unconstrained variable T. 
Comment 2 Philipe Mulet CLA 2006-09-05 07:56:46 EDT
Interestingly enough, removing the bound on parameter V makes javac happy again.

i.e.
public class GenericsTest {

    public class A {};

    public class B extends A {};

    public static void main(String[] args) {
        GenericsTest g = new GenericsTest();
        Set<A> set = g.newSet(g.new B());
    }

    public <T, V> Set<T> newSet(V... objects) {
        Set<T> set = new LinkedHashSet<T>();
        //for (T t : objects) {
        //    set.add(t);
        //}
        return set;
    }
Comment 3 Philipe Mulet CLA 2006-09-05 07:57:29 EDT
Added GenericTypeTest#test1026-1028
Comment 4 Philipe Mulet CLA 2006-09-05 10:30:09 EDT
Marking as invalid (we do the right thing)
Thanks for the testcase
Comment 5 Philipe Mulet CLA 2006-09-05 10:30:29 EDT
closing
Comment 6 Philipe Mulet CLA 2006-09-07 16:12:58 EDT
also see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6468384