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

Compile error with Generic wildcards in 4.37 M3 that was not present in M2

Open
#4,332 13 comments 0 reactions 1 assignee Claimed by @stephan-herrmann View on GitHub
javac
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

In Eclipse 4.37 M3, JDT started giving a compile error in some unit test code that was not present in M2 and is not reported by `javac`.

FYI @stephan-herrmann as the PR in this timeframe that seems the most related is #4251 (tweaking some prior work I see was done on generics recently)

I've extracted the following minimal reproducer "simulating" some Hamcrest matcher code, but without dependencies:

```
class MyUnitTest {

interface Matcher {
boolean matches(Object actual);

static Matcher> empty() {
return null;
}
static Matcher> contains(E... itemMatcher) {
return null;
}
}

void testOne() {
validate(List.of("one"), Matcher.empty());
}

void testTwo() {
validate(List.of("two"), Matcher.contains("two"));
}

private static void validate(
List strings, Matcher> matcher) {
// do some validation
}
}
```

The error appears on the call to local helper function `validate` within `testOne`, and states:

> The method validate(List, PathValidatorTest.Matcher>) in the type PathValidatorTest is not applicable for the arguments (List, PathValidatorTest.Matcher>)

I would hazard a guess that issues arises because the Generic `` in method `empty` is only inferred to `Object`, whereas before it was determined to be `String`. My reasoning is simply that if I change the method signature of `validate` by substituting `{Iterable => Collection}` then:

* An error is now given on the call to `validate` within `testTwo` (as I would expect), and
* The error in `testOne` goes away and the tooltip when hovering over its call to `validate` show the inferred type of E as `String`.

------

Specific JDT versions reported in Help > About > Installation details > Installation History:
* "Good": Eclipse Java Development Tools 3.20.300.v20250724-1800
* "Bad": Eclipse Java Development Tools 3.20.300.v20250814-1944

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.