redhat-developer / redhat-developer/vscode-java

Type parameter related false positive error messages.

Open
#3,816 1 comment 1 reaction 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

I encountered some error messages on my code that can be correctly compiled and run, meaning that these errors could be false positives. After simplifying the code, I found that these false positives could be caused by incorrect name shadowing for type parameters.

Environment
  • Operating System: Linux
  • JDK version: 21
  • Visual Studio Code version: 1.94.2
  • Java extension version: 1.35.1
Steps To Reproduce
  1. Create an empty java project and open it with vscode.
  2. Create a package demo and a App.java file in this package.
  3. Paste the code below into App.java:
package demo;

interface BinaryExpr {
    interface Op { }

    Op getOperator();
}

abstract class AbstractBinaryExpr<Op extends BinaryExpr.Op> implements BinaryExpr {
    private final Op op;

    public AbstractBinaryExpr(Op op) { this.op = op; }

    @Override
    public Op getOperator() {
        return op;
    }
}

final class ArithExpr extends AbstractBinaryExpr<ArithExpr.Op> {
    enum Op implements BinaryExpr.Op {
        ADD, SUB
    }

    public ArithExpr(Op op) { super(op); }
}

public class App {
    
    public static void main(String[] args) {
        var e = new ArithExpr(ArithExpr.Op.ADD);
        System.out.println(toStr(e));
    }

    public static String toStr(ArithExpr e) {
        return switch(e.getOperator()) {
            case ADD -> "+";
            case SUB -> "-";
        };
    }
}
Current Result

You will see the following three error messages:

// ...
public static String toStr(ArithExpr e) {
    return switch(e.getOperator()) { // Error 1: A switch expression should have a default case
        case ADD -> "+"; // Error 2: ADD cannot be resolved to a variable
        case SUB -> "-"; // Error 3: SUB cannot be resolved to a variable
    };
}
Expected Result

No error messages are expected because java compiler can handle it correctly.

Additional Informations

If you rename the type parameter Op and its following occurrances in class AbstractBinaryExpr into O, the error messages will disappear. This hints that the return type Op of AbstractBinaryExpr.getOperator() may be resolved to the Op in the superclass BinaryExpr rather than the type parameter.

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

Reproduce the report in the empty Java project using the supplied demo/App.java code, then compare VS Code's diagnostics with the Java compiler's behavior. Trace the type-parameter name resolution involved in AbstractBinaryExpr and verify that the switch expression and its ADD and SUB cases no longer produce false-positive errors.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.