diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/InferenceContext18.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/InferenceContext18.java index 6c38cdbb40d..bb9166e3da7 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/InferenceContext18.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/InferenceContext18.java @@ -1529,12 +1529,6 @@ static List pickIvarsByRank(Set variableSe Map> collectDependencies(BoundSet bounds) { // Implements the definition of dependencies from JLS §18.4: Map> dependsOn = new LinkedHashMap<>(); - // "An inference variable α depends on the resolution of itself." - for (InferenceVariable iv : this.inferenceVariables) { - Set selfSet = new LinkedHashSet<>(); - selfSet.add(iv); - dependsOn.put(iv, selfSet); - } for (TypeBound typeBound : bounds.flatten()) { // "Given a bound of one of the following forms:" (ecj may represent some using :> rather than <:) // α = T @@ -1616,6 +1610,15 @@ Map> collectDependencies(BoundSet bound } } } while (hasChange); + // "An inference variable α depends on the resolution of itself." + // https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4846 + for (InferenceVariable iv : this.inferenceVariables) { + if (!dependsOn.containsKey(iv)) { + Set selfSet = new LinkedHashSet<>(); + selfSet.add(iv); + dependsOn.put(iv, selfSet); + } + } return dependsOn; } diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_1_8.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_1_8.java index a9ecff3b42a..12e340153a9 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_1_8.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_1_8.java @@ -11074,4 +11074,58 @@ public static T any() { """ }); } + + // https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4846 + public void testGH4846() { + if (this.complianceLevel < ClassFileConstants.JDK9) { + return; + } + runConformTest( + new String[] { + "Pairs.java", + """ + import java.util.Map; + import java.util.function.Function; + + public class Pairs { + + public void addMapEntries(Function> extractor) { + add(extractor.andThen(Map::entrySet), Map.Entry::getKey, Map.Entry::getValue); + } + + public void add(Function> elementsExtractor, PairExtractor pairExtractor) { + add(elementsExtractor, pairExtractor::getName, pairExtractor::getValue); + } + + public void add(Function> elementsExtractor, Function nameExtractor, + Function valueExtractor) { + } + + interface PairExtractor { + N getName(E element); + V getValue(E element); + static PairExtractor of(Function nameExtractor, Function valueExtractor) { + return new PairExtractor<>() { + + @Override + @SuppressWarnings("unchecked") + public N getName(T instance) { + return (N) nameExtractor.apply(instance); + } + + @Override + @SuppressWarnings("unchecked") + public V getValue(T instance) { + return (V) valueExtractor.apply(instance); + } + + }; + } + } + + } + """ + }); + } + }