redhat-developer / redhat-developer/vscode-java
Type parameter related false positive error messages.
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- TypeScript
- Sterne
- 2.3k
- Forks
- 546
- Ø Merge
- 20 Std. 1 Min.
- Gemergte PRs (30 T.)
- 11
Beschreibung
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
- Create an empty java project and open it with vscode.
- Create a package
demoand aApp.javafile in this package. - 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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduzieren Sie den Bericht im leeren Java-Projekt mit dem bereitgestellten Code aus demo/App.java und vergleichen Sie anschließend die Diagnosen von VS Code mit dem Verhalten des Java-Compilers. Verfolgen Sie die Auflösung der Namen von Typparametern im Zusammenhang mit AbstractBinaryExpr und überprüfen Sie, dass der switch-Ausdruck sowie seine ADD- und SUB-Fälle keine falsch positiven Fehler mehr erzeugen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- java, vscode
- Bereich
- compilers, devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100