eclipse-jdt / eclipse-jdt/eclipse.jdt.core
[Bug] False positive errors when a type parameter shadows a nested type name
- Dominant language
- Java
- Stars
- 237
- Forks
- 195
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 47
Description
Environment:
- ECJ: `Eclipse Compiler for Java(TM) v20250526-2018, 3.42.0` (latest on Maven Central)
- javac: 21.0.2
## Description
ECJ reports a set of false positive errors on valid code that compiles and runs correctly under `javac`. The root cause appears to be a **type-parameter name shadowing** issue: when a type parameter (`Op`) has the same name as a nested type (`BinaryExpr.Op`) declared in the same hierarchy, ECJ resolves the type parameter's usages to the wrong symbol. This in turn breaks the switch-exhaustiveness analysis of an enum switch, producing three spurious diagnostics.
Renaming the type parameter (e.g. `Op` -> `O`) makes the errors disappear, which points at the type-parameter resolution rather than the switch analysis itself.
## Steps to Reproduce
1. Create the following `demo/App.java`:
```java
package demo;
interface BinaryExpr {
interface Op { }
Op getOperator();
}
abstract class AbstractBinaryExpr implements BinaryExpr {
private final Op op;
public AbstractBinaryExpr(Op op) { this.op = op; }
@Override
public Op getOperator() {
return op;
}
}
final class ArithExpr extends AbstractBinaryExpr {
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 -> "-";
};
}
}
```
2. Compile with ECJ (batch compiler):
```
java -jar ecj-3.42.0.jar -source 21 -target 21 -d out demo/App.java
```
3. Issue reproduced
```
1. ERROR in .../demo/App.java (at line 36)
return switch(e.getOperator()) {
^^^^^^^^^^^^^^^
A switch expression should have a default case
2. ERROR in .../demo/App.java (at line 37)
case ADD -> "+";
^^^
ADD cannot be resolved to a variable
3. ERROR in .../demo/App.java (at line 38)
case SUB -> "-";
^^^
SUB cannot be resolved to a variable
```
However, the code compiles cleanly with no errors, and it runs correctly (prints `+`):
```
$ javac -d out demo/App.java # no output, exit 0
$ java -cp out demo.App
+
```
Affected: https://github.com/redhat-developer/vscode-java/issues/3816
Contributor guide
Assessment
This issue has not been assessed yet.