eclipse-jdt / eclipse-jdt/eclipse.jdt.core

Unused local elimination may cause side effects to vanish

Open
#4,030 8 comments 0 reactions 1 assignee Claimed by @srikanth-sankaran View on GitHub
bug compiler corner case
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

Environment:
ECJ Version:

```
Eclipse Compiler for Java(TM) v20250514-1000, 3.42.0, Copyright IBM Corp 2000, 2020. All rights reserved.
```
Java Version:

```
java 21.0.7 2025-04-15 LTS
Java(TM) SE Runtime Environment (build 21.0.7+8-LTS-245)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.7+8-LTS-245, mixed mode, sharing)
```
Javac Version:

```
javac 21.0.7
```
Description:
The following test program exhibits different runtime behaviors when compiled with different compilers (ECJ and javac):

```java
public class Test {
public static void main(String[] args) {
int result = (Integer) null;
}
}
```

When compiling this code with javac and running it, the program throws a `java.lang.NullPointerException`.

However, when compiling with ECJ and running the program, no exception is thrown. This is incorrect behavior as it is trying to unbox a null Integer value to a primitive int.

Another case that shows the problem, this time missing ArithmeticException:

```
class Test {
static int val = 0;

public static void main(String[] argv) {
val = 50;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
byte b = (byte) i;
int result = b * j;
result <<= 2;
result /= j;
}
}
}
}
```

Steps to Reproduce:
1. Compile the test program using javac, and run it. The program throws a NullPointerException:
```bash
javac Test.java
java Test
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "null" is null
at Test.main(Test.java:3)
```

2. Compile the test program using ECJ, and run it. The program executes successfully without throwing any exception:
```bash
java -jar ecj-4.36M3.jar --release 21 -d . Test.java
java Test
```

Expected behavior: The program compiled with ECJ should throw a NullPointerException.
Actual behavior: The program compiled with ECJ executes successfully without throwing any exception.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.