Skip to content

Commit

Permalink
Incorrect overload resolution ends in type error. (#3515)
Browse files Browse the repository at this point in the history
+ record when Poly*Method is part of a set of overloads
  + in that case don't propagate premature bounds inner -> outer

Fixes #2941
  • Loading branch information
stephan-herrmann authored Jan 5, 2025
1 parent f529052 commit c928ff1
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,8 @@ private boolean addConstraintsToC_OneExpr(Expression expri, Set<ConstraintFormul
MethodBinding innerMethod = invocation.binding();
if (innerMethod == null)
return true; // -> proceed with no new C set elements.
if (innerMethod instanceof PolyParameterizedGenericMethodBinding poly && poly.hasOverloads)
return true; // don't let ambiguous inner method influence outer inference

Expression[] arguments = invocation.arguments();
TypeBinding[] argumentTypes = arguments == null ? Binding.NO_PARAMETERS : new TypeBinding[arguments.length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

public class PolyParameterizedGenericMethodBinding extends ParameterizedGenericMethodBinding { // confused citizen.

public boolean hasOverloads;

private final ParameterizedGenericMethodBinding wrappedBinding;
public PolyParameterizedGenericMethodBinding(ParameterizedGenericMethodBinding applicableMethod) {
super(applicableMethod.originalMethod, applicableMethod.typeArguments, applicableMethod.environment, applicableMethod.inferredWithUncheckedConversion, false, applicableMethod.targetType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2018 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -38,14 +38,19 @@ public ProblemMethodBinding(char[] selector, TypeBinding[] args, ReferenceBindin
public ProblemMethodBinding(MethodBinding closestMatch, char[] selector, TypeBinding[] args, int problemReason) {
this(selector, args, problemReason);
this.closestMatch = closestMatch;
if (closestMatch != null && problemReason != ProblemReasons.Ambiguous) {
this.declaringClass = closestMatch.declaringClass;
this.returnType = closestMatch.returnType;
if (problemReason == ProblemReasons.InvocationTypeInferenceFailure || problemReason == ProblemReasons.ContradictoryNullAnnotations) {
this.thrownExceptions = closestMatch.thrownExceptions;
this.typeVariables = closestMatch.typeVariables;
this.modifiers = closestMatch.modifiers;
this.tagBits = closestMatch.tagBits;
if (problemReason == ProblemReasons.Ambiguous) {
if (closestMatch instanceof PolyParameterizedGenericMethodBinding poly)
poly.hasOverloads = true;
} else {
if (closestMatch != null) {
this.declaringClass = closestMatch.declaringClass;
this.returnType = closestMatch.returnType;
if (problemReason == ProblemReasons.InvocationTypeInferenceFailure || problemReason == ProblemReasons.ContradictoryNullAnnotations) {
this.thrownExceptions = closestMatch.thrownExceptions;
this.typeVariables = closestMatch.typeVariables;
this.modifiers = closestMatch.modifiers;
this.tagBits = closestMatch.tagBits;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2722,4 +2722,68 @@ public void test482440b() {
"}\n"
});
}
public void testGH2941_a() {
runConformTest(
new String[] {
"BiStream.java",
"""
import static java.util.stream.Collectors.collectingAndThen;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Stream;
public abstract class BiStream<K, V> {
public static <E, K, V> Collector<E, ?, BiStream<K, V>> toBiStream(
Function<? super E, ? extends K> toKey, Function<? super E, ? extends V> toValue) {
return collectingAndThen(toStream(), stream -> from(stream));
}
private static <T> Collector<T, ?, Stream<T>> toStream() {
return null;
}
public static <T, K, V> BiStream<K, V> from(Iterable<T> elements) {
return null;
}
public static <T, K, V> BiStream<K, V> from(Stream<T> stream) {
return null;
}
}
"""
});
}
public void testGH2941_b() {
runConformTest(
new String[] {
"BiStream.java",
"""
import static java.util.stream.Collectors.collectingAndThen;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Stream;
public abstract class BiStream<K, V> {
public static <E, K, V> Collector<E, ?, BiStream<K, V>> toBiStream(
Function<? super E, ? extends K> toKey, Function<? super E, ? extends V> toValue) {
return collectingAndThen(toStream(), stream -> from(stream));
}
private static <T> Collector<T, ?, Stream<T>> toStream() {
return null;
}
public static <T, K, V> BiStream<K, V> from(Stream<T> stream) {
return null;
}
public static <T, K, V> BiStream<K, V> from(Iterable<T> elements) {
return null;
}
}
"""
});
}
}

0 comments on commit c928ff1

Please sign in to comment.