redhat-developer / redhat-developer/vscode-java

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

Open
#3,817 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

upstream
Dominant language
TypeScript
Stars
2.3k
Forks
546
Avg merge
20h 1m
Merged PRs (30d)
11

Description

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
  • Operating System: macOS Sonoma 14
  • JDK version: 21.0.2
  • Visual Studio Code version: 1.94.2
  • Java extension version: v0.29.0
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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the hover or definition result with the Fruit.java and Apple.java examples, using the reported VS Code and Java extension setup. Compare the inferred type with the expected recursive wildcard form. Done means the definition window reports Fruit<? extends Fruit<?>> rather than Fruit<?> for the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, vscode
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.