Bug 537299 - [compiler] incorrect type mismatch with wildcard generics
Summary: [compiler] incorrect type mismatch with wildcard generics
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.9   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks: 564617
  Show dependency tree
 
Reported: 2018-07-23 11:59 EDT by Rob Spoor CLA
Modified: 2022-07-02 01:07 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 Rob Spoor CLA 2018-07-23 11:59:05 EDT
I have a generic type (let's call it X). I have a method where I have a 
Collection<? extends X<? extends E>>. I have the following code to return a Stream<E> from this collection, using a method x that takes an X<E> and returns an E.

The following worked just fine in Oxygen and with javac:

    Stream<E> stream = collection.stream().map(m -> x(m));

Since Photon this fails with the following error: "Type mismatch: cannot convert from Stream<Object> to Stream<E>". I can fix this by letting method x take an X<? extends E>, but this does feel like a regression in the Eclipse compiler.
Comment 1 Stephan Herrmann CLA 2018-07-23 15:46:37 EDT
What is 'E', a class, an interface, a type variable?
If type variable, are there any type bounds in its declaration?
What does the type parameter of X look like? Bounded / unbounded?

Self-contained examples are so much easier to discuss, than having to guess various bits and pieces :)
Comment 2 Rob Spoor CLA 2018-07-24 07:59:25 EDT
A small demo:

import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Stream;

class X<E> {
    private E value;

    public X(E value) {
        this.value = value;
    }

    public E getValue() {
        return value;
    }
}

class Demo {
    public static void main(String... args) {
        Collection<X<Integer>> collection = new ArrayList<>();
        collection.add(new X<>(1));
        collection.add(new X<>(2));
        demo(collection);
    }

    private static <E> void demo(Collection<? extends X<? extends E>> collection) {
        Stream<E> stream = collection.stream().map(x -> map(x));
    }

    private static <E> E map(X<E> x) {
        return x == null ? null : x.getValue();
    }
}


I have to change the argument to the map method from X<E> to X<? extends E>, but only in Photon, not in Oxygen.

Note that a method reference, Demo::map, doesn't compile in either Photon or Oxygen, unless I use the wildcard in the map method.
Comment 3 Stephan Herrmann CLA 2019-04-02 11:19:57 EDT
Thanks for the example.

I see it being rejected since the fix for bug 530235 (4.8M6).

Please don't hold your breath waiting for a fix because:

- The fix in bug 530235 is in an area where JLS doesn't help, only private email from a javac developer did help to some degree.

- Personally, I believe the fix in bug 530235 is correct.

- Seeing wildcards in this current bug, I'm afraid we'd need to first solve bug 448793 and friends, but in that bug I'm (still) waiting for necessary advice from Oracle (spec author and/or compiler dev).

Another workaround:

    Stream<E> stream = collection.stream().map((X<? extends E> x) -> map(x));
Comment 4 Eclipse Genie CLA 2022-07-02 01:07:20 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.