Skip to content

Commit fdbf9d6

Browse files
committed
Type Inference issue in spring-boot project
1 parent dc910a4 commit fdbf9d6

2 files changed

Lines changed: 61 additions & 6 deletions

File tree

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/InferenceContext18.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,12 +1486,6 @@ public Set<InferenceVariable> getSmallestVariableSet(BoundSet bounds, Set<Infere
14861486
Map<InferenceVariable,Set<InferenceVariable>> collectDependencies(BoundSet bounds, boolean maySkipSuperBound, boolean[] hasSkippedSuperBound) {
14871487
// Implements the definition of dependencies from JLS §18.4:
14881488
Map<InferenceVariable,Set<InferenceVariable>> dependsOn = new LinkedHashMap<>();
1489-
// "An inference variable α depends on the resolution of itself."
1490-
for (InferenceVariable iv : this.inferenceVariables) {
1491-
Set<InferenceVariable> selfSet = new LinkedHashSet<>();
1492-
selfSet.add(iv);
1493-
dependsOn.put(iv, selfSet);
1494-
}
14951489
for (TypeBound typeBound : bounds.flatten()) {
14961490
// "Given a bound of one of the following forms:" (ecj may represent some using :> rather than <:)
14971491
// α = T
@@ -1580,6 +1574,15 @@ Map<InferenceVariable,Set<InferenceVariable>> collectDependencies(BoundSet bound
15801574
}
15811575
}
15821576
} while (hasChange);
1577+
// "An inference variable α depends on the resolution of itself."
1578+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4846
1579+
for (InferenceVariable iv : this.inferenceVariables) {
1580+
if (!dependsOn.containsKey(iv)) {
1581+
Set<InferenceVariable> selfSet = new LinkedHashSet<>();
1582+
selfSet.add(iv);
1583+
dependsOn.put(iv, selfSet);
1584+
}
1585+
}
15831586
return dependsOn;
15841587
}
15851588

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_1_8.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10964,4 +10964,56 @@ <X, Y extends One<X>> void foo2(Y y1) {
1096410964
"The method bar(One<Inner<?>>) in the type Bug is not applicable for the arguments (One<Inner<X>>)\n" +
1096510965
"----------\n");
1096610966
}
10967+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4846
10968+
public void testGH4846() {
10969+
if (this.complianceLevel < ClassFileConstants.JDK9) {
10970+
return;
10971+
}
10972+
runConformTest(
10973+
new String[] {
10974+
"Pairs.java",
10975+
"""
10976+
import java.util.Map;
10977+
import java.util.function.Function;
10978+
10979+
public class Pairs<T> {
10980+
10981+
public <V> void addMapEntries(Function<T, Map<String, V>> extractor) {
10982+
add(extractor.andThen(Map::entrySet), Map.Entry::getKey, Map.Entry::getValue);
10983+
}
10984+
10985+
public <E> void add(Function<T, Iterable<E>> elementsExtractor, PairExtractor<E> pairExtractor) {
10986+
add(elementsExtractor, pairExtractor::getName, pairExtractor::getValue);
10987+
}
10988+
10989+
public <E, V> void add(Function<T, Iterable<E>> elementsExtractor, Function<E, String> nameExtractor,
10990+
Function<E, V> valueExtractor) {
10991+
}
10992+
10993+
interface PairExtractor<E> {
10994+
<N> N getName(E element);
10995+
<V> V getValue(E element);
10996+
static <T> PairExtractor<T> of(Function<T, ?> nameExtractor, Function<T, ?> valueExtractor) {
10997+
return new PairExtractor<>() {
10998+
10999+
@Override
11000+
@SuppressWarnings("unchecked")
11001+
public <N> N getName(T instance) {
11002+
return (N) nameExtractor.apply(instance);
11003+
}
11004+
11005+
@Override
11006+
@SuppressWarnings("unchecked")
11007+
public <V> V getValue(T instance) {
11008+
return (V) valueExtractor.apply(instance);
11009+
}
11010+
11011+
};
11012+
}
11013+
}
11014+
11015+
}
11016+
"""
11017+
});
11018+
}
1096711019
}

0 commit comments

Comments
 (0)