redhat-developer / redhat-developer/vscode-java
Type parameter related false positive error messages.
还没有人认领这个 Issue。
- 主要语言
- TypeScript
- 星标
- 2.3k
- 派生
- 547
- 平均合并
- 20 小时 1 分钟
- 30 天内合并 PR
- 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
- 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.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
使用提供的 demo/App.java 代码在空的 Java 项目中复现该报告,然后将 VS Code 的诊断结果与 Java 编译器的行为进行比较。跟踪 AbstractBinaryExpr 中涉及的类型参数名称解析,并验证 switch 表达式及其 ADD 和 SUB case 不再产生误报错误。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- java, vscode
- 领域
- compilers, devtools
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100