Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,8 @@ public BoundSet inferInvocationType(TypeBinding expectedType, InvocationSite inv
if (!addConstraintsToC(this.invocationArguments, c, method, this.inferenceKind, invocationSite))
return null;
// 5. bullet: determine B4 from C
List<Set<InferenceVariable>> components = this.currentBounds.computeConnectedComponents(this.inferenceVariables);
while (!c.isEmpty()) {
List<Set<InferenceVariable>> components = this.currentBounds.computeConnectedComponents(this.inferenceVariables);
// *
Set<ConstraintFormula> bottomSet = findBottomSet(c, allOutputVariables(c), components);
if (bottomSet.isEmpty()) {
Expand Down Expand Up @@ -460,6 +460,12 @@ public BoundSet inferInvocationType(TypeBinding expectedType, InvocationSite inv
if (!this.currentBounds.reduceOneConstraint(this, constraint))
return null;
}
for (ConstraintFormula constraint : bottomSet) {
// https://bugs.openjdk.org/browse/JDK-8052325
if (constraint instanceof ConstraintExpressionFormula expressionFormula && expressionFormula.left instanceof LambdaExpression lambda && lambda.argumentsTypeElided()) {
addLambdaConstraintsToC(lambda, c, method, expressionFormula.right);
}
}
}
// 6. bullet: solve
BoundSet solution = solve();
Expand Down Expand Up @@ -645,6 +651,31 @@ private boolean addConstraintsToC(Expression[] exprs, Set<ConstraintFormula> c,
return true;
}

private boolean addLambdaConstraintsToC(LambdaExpression lambda, Set<ConstraintFormula> c, MethodBinding method, TypeBinding substF)
throws InferenceFailureException
{
// https://bugs.openjdk.java.net/browse/JDK-8038747
BlockScope skope = lambda.enclosingScope;
if (substF.isFunctionalInterface(skope)) { // could be an inference variable.
ReferenceBinding t = (ReferenceBinding) substF;
ParameterizedTypeBinding withWildCards = InferenceContext18.parameterizedWithWildcard(t);
if (withWildCards != null) {
t = ConstraintExpressionFormula.findGroundTargetType(this, skope, lambda, withWildCards);
}
MethodBinding functionType;
if (t != null && (functionType = t.getSingleAbstractMethod(skope, true)) != null && (lambda = lambda.resolveExpressionExpecting(t, this.scope)) != null) {
TypeBinding r = functionType.returnType;
Expression[] resultExpressions = lambda.resultExpressions();
for (int i = 0, length = resultExpressions == null ? 0 : resultExpressions.length; i < length; i++) {
Expression resultExpression = resultExpressions[i];
if (!addConstraintsToC_OneExpr(resultExpression, c, r.original(), r, method))
return false;
}
}
}
return true;
}

private boolean addConstraintsToC_OneExpr(Expression expri, Set<ConstraintFormula> c, TypeBinding fsi, TypeBinding substF, MethodBinding method)
throws InferenceFailureException
{
Expand All @@ -659,27 +690,9 @@ private boolean addConstraintsToC_OneExpr(Expression expri, Set<ConstraintFormul
}
if (expri instanceof FunctionalExpression) {
c.add(new ConstraintExceptionFormula((FunctionalExpression) expri, substF));
if (expri instanceof LambdaExpression) {
// https://bugs.openjdk.java.net/browse/JDK-8038747
LambdaExpression lambda = (LambdaExpression) expri;
BlockScope skope = lambda.enclosingScope;
if (substF.isFunctionalInterface(skope)) { // could be an inference variable.
ReferenceBinding t = (ReferenceBinding) substF;
ParameterizedTypeBinding withWildCards = InferenceContext18.parameterizedWithWildcard(t);
if (withWildCards != null) {
t = ConstraintExpressionFormula.findGroundTargetType(this, skope, lambda, withWildCards);
}
MethodBinding functionType;
if (t != null && (functionType = t.getSingleAbstractMethod(skope, true)) != null && (lambda = lambda.resolveExpressionExpecting(t, this.scope)) != null) {
TypeBinding r = functionType.returnType;
Expression[] resultExpressions = lambda.resultExpressions();
for (int i = 0, length = resultExpressions == null ? 0 : resultExpressions.length; i < length; i++) {
Expression resultExpression = resultExpressions[i];
if (!addConstraintsToC_OneExpr(resultExpression, c, r.original(), r, method))
return false;
}
}
}
// https://bugs.openjdk.org/browse/JDK-8052325
if (expri instanceof LambdaExpression lambda && !lambda.argumentsTypeElided()) {
addLambdaConstraintsToC(lambda, c, method, substF);
}
} else if (expri instanceof Invocation && expri.isPolyExpression()) {

Expand Down Expand Up @@ -709,7 +722,6 @@ private boolean addConstraintsToC_OneExpr(Expression expri, Set<ConstraintFormul
if (!innerContext.computeB3(invocation, substF, shallowMethod))
return false;
if (innerContext.addConstraintsToC(arguments, c, innerMethod.genericMethod(), innerContext.inferenceKind, invocation)) {
this.currentBounds.addBounds(innerContext.currentBounds, this.environment);
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7058,5 +7058,23 @@ interface C<W> {
},
"");
}

public void testGH4557() {
runConformTest(new String[] {
"Test.java",
"""
import java.util.List;
public class Test {
public static void main(List<?> l) {
get(get(get(l)));
}
public static <T> List<?> get(List<T> l) {
return l;
}
}
"""
},
"");
}
}

Loading