redhat-developer / vscode-java

Java Language Support for Visual Studio Code
Eclipse Public License 2.0
2.08k stars 438 forks source link

Extended generic type loses more specific type when using recursive type bound (works in IntelliJ) #3817

Open bennettdams opened 5 days ago

bennettdams commented 5 days ago

The inferred type when using recursive type bounds is missing a more specific wildcard when looking at the definition of a variable (e.g. via hovering it). This works in IntelliJ, but not in VS Code.

VS Code:

Fruit<?>

image

IntelliJ:

Fruit<? extends Fruit<?>>

image

Environment
Steps To Reproduce

Here is a made-up example with two classes:

Fruit.java

public class Fruit<T extends Fruit<T>> implements Comparable<T> {
    private final Integer size;

    public Fruit(Integer size) {
        this.size = size;
    }

    public Integer getSize() {
        return size;
    }

    @Override public int compareTo(T other) {
        return size.compareTo(other.getSize());     // Now getSize() is available.
    }
}

Apple.java

class Apple extends Fruit<Apple> {
    public Apple(Integer size) {
        super(size);
    }
}

Some random class/function to check:

public Fruit<? extends Fruit<?>> getFruit(Fruit<? extends Fruit<?>> someFruit) {
    // in real world code, assume some mapper/runtime check here
    // var someFruit = ...
    return new Fruit<>(1);
}

Somewhere else:

var fruit = getFruit(new Apple(1));
Current Result

Fruit<?> (in the definition window)

Expected Result

Fruit<? extends Fruit<?>> (in the definition window)

snjeza commented 5 days ago

This is an upstream jdt issue. I can reproduce it in the Eclipse master branch, but I can't reproduce it in the javac branch.