Skip to content

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

Description

@lukeu

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<T> {
        boolean matches(Object actual);

        static <E> Matcher<java.util.Collection<? extends E>> empty() {
            return null;
        }
        static <E> Matcher<java.lang.Iterable<? extends E>> 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<String> strings, Matcher<? extends Iterable<? extends String>> 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<? extends Iterable<? extends String>>) in the type PathValidatorTest is not applicable for the arguments (List, PathValidatorTest.Matcher<Collection<? extends Object>>)

I would hazard a guess that issues arises because the Generic <E> 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

Metadata

Metadata

Labels

javacecj not compatible with javac

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions