Skip to content

Commit c928ff1

Browse files
Incorrect overload resolution ends in type error. (#3515)
+ record when Poly*Method is part of a set of overloads + in that case don't propagate premature bounds inner -> outer Fixes #2941
1 parent f529052 commit c928ff1

File tree

4 files changed

+82
-9
lines changed

4 files changed

+82
-9
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,8 @@ private boolean addConstraintsToC_OneExpr(Expression expri, Set<ConstraintFormul
691691
MethodBinding innerMethod = invocation.binding();
692692
if (innerMethod == null)
693693
return true; // -> proceed with no new C set elements.
694+
if (innerMethod instanceof PolyParameterizedGenericMethodBinding poly && poly.hasOverloads)
695+
return true; // don't let ambiguous inner method influence outer inference
694696

695697
Expression[] arguments = invocation.arguments();
696698
TypeBinding[] argumentTypes = arguments == null ? Binding.NO_PARAMETERS : new TypeBinding[arguments.length];

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
public class PolyParameterizedGenericMethodBinding extends ParameterizedGenericMethodBinding { // confused citizen.
1717

18+
public boolean hasOverloads;
19+
1820
private final ParameterizedGenericMethodBinding wrappedBinding;
1921
public PolyParameterizedGenericMethodBinding(ParameterizedGenericMethodBinding applicableMethod) {
2022
super(applicableMethod.originalMethod, applicableMethod.typeArguments, applicableMethod.environment, applicableMethod.inferredWithUncheckedConversion, false, applicableMethod.targetType);

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2018 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -38,14 +38,19 @@ public ProblemMethodBinding(char[] selector, TypeBinding[] args, ReferenceBindin
3838
public ProblemMethodBinding(MethodBinding closestMatch, char[] selector, TypeBinding[] args, int problemReason) {
3939
this(selector, args, problemReason);
4040
this.closestMatch = closestMatch;
41-
if (closestMatch != null && problemReason != ProblemReasons.Ambiguous) {
42-
this.declaringClass = closestMatch.declaringClass;
43-
this.returnType = closestMatch.returnType;
44-
if (problemReason == ProblemReasons.InvocationTypeInferenceFailure || problemReason == ProblemReasons.ContradictoryNullAnnotations) {
45-
this.thrownExceptions = closestMatch.thrownExceptions;
46-
this.typeVariables = closestMatch.typeVariables;
47-
this.modifiers = closestMatch.modifiers;
48-
this.tagBits = closestMatch.tagBits;
41+
if (problemReason == ProblemReasons.Ambiguous) {
42+
if (closestMatch instanceof PolyParameterizedGenericMethodBinding poly)
43+
poly.hasOverloads = true;
44+
} else {
45+
if (closestMatch != null) {
46+
this.declaringClass = closestMatch.declaringClass;
47+
this.returnType = closestMatch.returnType;
48+
if (problemReason == ProblemReasons.InvocationTypeInferenceFailure || problemReason == ProblemReasons.ContradictoryNullAnnotations) {
49+
this.thrownExceptions = closestMatch.thrownExceptions;
50+
this.typeVariables = closestMatch.typeVariables;
51+
this.modifiers = closestMatch.modifiers;
52+
this.tagBits = closestMatch.tagBits;
53+
}
4954
}
5055
}
5156
}

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2722,4 +2722,68 @@ public void test482440b() {
27222722
"}\n"
27232723
});
27242724
}
2725+
public void testGH2941_a() {
2726+
runConformTest(
2727+
new String[] {
2728+
"BiStream.java",
2729+
"""
2730+
import static java.util.stream.Collectors.collectingAndThen;
2731+
2732+
import java.util.function.Function;
2733+
import java.util.stream.Collector;
2734+
import java.util.stream.Stream;
2735+
2736+
public abstract class BiStream<K, V> {
2737+
public static <E, K, V> Collector<E, ?, BiStream<K, V>> toBiStream(
2738+
Function<? super E, ? extends K> toKey, Function<? super E, ? extends V> toValue) {
2739+
return collectingAndThen(toStream(), stream -> from(stream));
2740+
}
2741+
2742+
private static <T> Collector<T, ?, Stream<T>> toStream() {
2743+
return null;
2744+
}
2745+
2746+
public static <T, K, V> BiStream<K, V> from(Iterable<T> elements) {
2747+
return null;
2748+
}
2749+
2750+
public static <T, K, V> BiStream<K, V> from(Stream<T> stream) {
2751+
return null;
2752+
}
2753+
}
2754+
"""
2755+
});
2756+
}
2757+
public void testGH2941_b() {
2758+
runConformTest(
2759+
new String[] {
2760+
"BiStream.java",
2761+
"""
2762+
import static java.util.stream.Collectors.collectingAndThen;
2763+
2764+
import java.util.function.Function;
2765+
import java.util.stream.Collector;
2766+
import java.util.stream.Stream;
2767+
2768+
public abstract class BiStream<K, V> {
2769+
public static <E, K, V> Collector<E, ?, BiStream<K, V>> toBiStream(
2770+
Function<? super E, ? extends K> toKey, Function<? super E, ? extends V> toValue) {
2771+
return collectingAndThen(toStream(), stream -> from(stream));
2772+
}
2773+
2774+
private static <T> Collector<T, ?, Stream<T>> toStream() {
2775+
return null;
2776+
}
2777+
2778+
public static <T, K, V> BiStream<K, V> from(Stream<T> stream) {
2779+
return null;
2780+
}
2781+
2782+
public static <T, K, V> BiStream<K, V> from(Iterable<T> elements) {
2783+
return null;
2784+
}
2785+
}
2786+
"""
2787+
});
2788+
}
27252789
}

0 commit comments

Comments
 (0)