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

ECJ fails to infer generic constructor arguments with target typing, while javac succeed

Open
#4,355 0 comments 0 reactions 0 assignees View on GitHub
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 following java code, ECJ and javac exhibit divergent behavior in handling a generic constructor invocation involving `Enum.valueOf`.

## Test program:
```java
public class Test {
}

enum Color {
RED, GREEN, BLUE
}

class EnumWrapper> {
private T value;

public EnumWrapper(T value) {
this.value = value;
}

public T getValue() {
return value;
}
}

class RecursiveTemplate> {

public void process(EnumWrapper wrapper, int depth) {
if (depth <= 0)
return;
EnumWrapper nextWrapper = new EnumWrapper<>(T.valueOf(wrapper.getValue().getClass(), wrapper.getValue().name()));
process(nextWrapper, depth - 1);
}
}
```

Javac compiles without any error:
```bash
javac Test.java
```
```bash
Note: Test.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
```

However, ecj rejects it with an error:
```bash
java -jar ecj-4.37M3.jar -21 Test.java
```
```bash
----------
----------
1. ERROR in Test.java (at line 25)
EnumWrapper nextWrapper = new EnumWrapper<>(T.valueOf(wrapper.getValue().getClass(), wrapper.getValue().name()));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Cannot infer type arguments for EnumWrapper<>
----------
1 problem (1 error)
```

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.