redhat-developer / redhat-developer/vscode-java

Type parameter related false positive error messages.

Đang mở
#3,816 1 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

upstream
Ngôn ngữ chính
TypeScript
Star
2.3k
Fork
546
Merge trung bình
20 giờ 1 phút
Pull request đã merge (30 ngày)
11

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Tái hiện báo cáo trong dự án Java trống bằng mã demo/App.java được cung cấp, sau đó so sánh các chẩn đoán của VS Code với hành vi của trình biên dịch Java. Theo dõi việc phân giải tên tham số kiểu liên quan đến AbstractBinaryExpr và xác minh rằng biểu thức switch cùng các trường hợp ADD và SUB của nó không còn tạo ra các lỗi dương tính giả.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
java, vscode
Lĩnh vực
compilers, devtools
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.