eclipse-jdt / eclipse-jdt/eclipse.jdt.core
ECJ fails to report lambda capture of non-effectively final variable in dead code
- Dominant language
- Java
- Stars
- 237
- Forks
- 195
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 47
Description
## ECJ Version:
```bash
Eclipse Compiler for Java(TM) v20250814-1944, 3.43.0, Copyright IBM Corp 2000, 2020. All rights reserved.
```
## Javac Version:
```bash
javac 21.0.8
```
## Description:
When compiling the following Java code, ECJ does not report a compilation error for a lambda expression that captures a loop variable i which is not effectively final, even though such usage is explicitly prohibited. Instead, ECJ only issues a warning about "Dead code" due to the if (false) condition.
In contrast, javac correctly rejects the code with an error.
## Test program:
```java
public class Test {
public static void main(String[] strArr) {
if (false) {
StructContainer[] structArray = new StructContainer[5];
for (int i = 0; i < structArray.length; i++) {
structArray[i] = new StructContainer(() -> i * 10);
}
}else {
}
}
}
class StructContainer {
private final java.util.function.IntSupplier valueSupplier;
public StructContainer(java.util.function.IntSupplier supplier) {
this.valueSupplier = supplier;
}
public int getValue() {
return valueSupplier.getAsInt();
}
}
```
Using javac it will have an error:
```bash
javac Test.java
Test.java:8: error: local variables referenced from a lambda expression must be final or effectively final
structArray[i] = new StructContainer(() -> i * 10);
^
1 error
```
However, ecj only have a warning:
```bash
java -jar ./ecj-4.36M3.jar Test.java
1. WARNING in Test.java (at line 5)
if (false) {
StructContainer[] structArray = new StructContainer[5];
for (int i = 0; i < structArray.length; i++) {
structArray[i] = new StructContainer(() -> i * 10);
}
}else {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Dead code
----------
1 problem (1 warning)
```
Contributor guide
Assessment
This issue has not been assessed yet.