redhat-developer / redhat-developer/vscode-java

Type parameter related false positive error messages.

オープン
#3,816 コメント 1 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

upstream
主要言語
TypeScript
スター
2.3k
フォーク
546
平均マージ
20時間 1分
マージ済み PR(30日)
11

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

提供された demo/App.java コードを使用して空の Java プロジェクトで報告内容を再現し、その後、VS Code の診断結果と Java コンパイラーの動作を比較します。AbstractBinaryExpr に関係する型パラメーター名の解決を追跡し、switch 式とその ADD および SUB ケースで誤検出エラーが発生しなくなっていることを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
java, vscode
領域
compilers, devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。