Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 415981

Summary: [1.5] Compiler rejects valid assignment involving generics
Product: [Eclipse Project] JDT Reporter: John Kozlov <orionllmain>
Component: CoreAssignee: Stephan Herrmann <stephan.herrmann>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: jarthana, lukas.eder, stephan.herrmann
Version: 4.3   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description John Kozlov CLA 2013-08-27 12:25:24 EDT
The following code compiles with Java 6 compiler, but fails in Eclipse:

public static <T> void f(List<? extends T> list) {
  Set<T> set = Collections.singleton(list.get(0));
}

The error is: "Type mismatch: cannot convert from Set<capture#1-of ? extends T> to Set<T> (Java Problem)"

This code should compile, because the identical code compiles in Eclipse:

public static <T> void f(List<? extends T> list) {
  T t = list.get(0);
  Set<T> set = Collections.singleton(t);
}
Comment 1 Stephan Herrmann CLA 2013-08-27 14:06:41 EDT
This is not a regression, the issue can be reproduced all the way back to ecj 3.2.2.

(In reply to comment #0)
> This code should compile, because the identical code compiles in Eclipse:

This is not a hard proof. Since Collections.singleton is a generic method
the compiler needs to apply type inference to find the correct instantiation.
If inference fails, it shouldn't surprise that making the intermediate type
explicit helps the compiler to recognize the program as type safe.
Comment 2 Stephan Herrmann CLA 2014-01-09 11:20:26 EST
The statement in bug 425203 comment 1 also holds for this bug.
Comment 3 Lukas Eder CLA 2014-11-17 03:00:01 EST
I'm looking through Eclipse bugs similar to this one. This one here, I can no longer reproduce with 4.5.0M3. Perhaps a duplicate?
Comment 4 Stephan Herrmann CLA 2014-11-18 05:56:33 EST
(In reply to Lukas Eder from comment #3)
> I'm looking through Eclipse bugs similar to this one. This one here, I can
> no longer reproduce with 4.5.0M3. Perhaps a duplicate?

Thanks for retesting.

In current HEAD I see:
- accept at -1.8
- reject at -1.7 with this error (same as comment 0):

1. ERROR in /tmp/Test.java (at line 4)
        Set<T> set = Collections.singleton(list.get(0));
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Type mismatch: cannot convert from Set<capture#1-of ? extends T> to Set<T>
----------
1 problem (1 error)


This means we're not likely to find a duplicate, as the change in behavior is just a result of entirely new type inference (spec & impl) in Java 8.

OTOH, at 1.7 and below we still deviate from javac behavior, but I personally wouldn't give a high priority to fixing this, considering also that in JLS7 (i.e., without target typing) our behaviour could well be the correct one. (The "identical code" in comment 0 is *not* identical from a type inference p.o.v).
Comment 5 Lukas Eder CLA 2014-11-18 06:09:55 EST
I see, yes I've only tested this with Java 8
Comment 6 Eclipse Genie CLA 2018-10-29 15:37:38 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 7 Stephan Herrmann CLA 2018-10-30 07:59:47 EDT
I don't plan to fix the legacy modes 1.7 or below.