Skip to content

Commit a3826aa

Browse files
Fix 3 layer nested method calls with the return type includes a wildcard
Adopt test & fix from PR 4565 Also-by: coehlrich <[email protected]>
1 parent ad3653e commit a3826aa

File tree

2 files changed

+53
-24
lines changed

2 files changed

+53
-24
lines changed

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

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,9 @@ public BoundSet inferInvocationType(TypeBinding expectedType, InvocationSite inv
430430
if (!addConstraintsToC(this.invocationArguments, c, method, this.inferenceKind, invocationSite))
431431
return null;
432432
// 5. bullet: determine B4 from C
433-
List<Set<InferenceVariable>> components;
434433
while (!c.isEmpty()) {
435434
Map<InferenceVariable,Set<InferenceVariable>> dependencies = collectDependencies(this.currentBounds, false, new boolean[1]);
436-
components = new ArrayList<>(dependencies.values());
435+
List<Set<InferenceVariable>> components = new ArrayList<>(dependencies.values());
437436
// *
438437
Set<ConstraintFormula> bottomSet = findBottomSet(c, allOutputVariables(c), components);
439438
if (bottomSet.isEmpty()) {
@@ -467,6 +466,12 @@ public BoundSet inferInvocationType(TypeBinding expectedType, InvocationSite inv
467466
if (!this.currentBounds.reduceOneConstraint(this, constraint))
468467
return null;
469468
}
469+
for (ConstraintFormula constraint : bottomSet) {
470+
// https://bugs.openjdk.org/browse/JDK-8052325
471+
if (constraint instanceof ConstraintExpressionFormula expressionFormula && expressionFormula.left instanceof LambdaExpression lambda && lambda.argumentsTypeElided()) {
472+
addLambdaConstraintsToC(lambda, c, method, expressionFormula.right);
473+
}
474+
}
470475
}
471476
// 6. bullet: solve
472477
BoundSet solution = solve();
@@ -652,6 +657,31 @@ private boolean addConstraintsToC(Expression[] exprs, Set<ConstraintFormula> c,
652657
return true;
653658
}
654659

660+
private boolean addLambdaConstraintsToC(LambdaExpression lambda, Set<ConstraintFormula> c, MethodBinding method, TypeBinding substF)
661+
throws InferenceFailureException
662+
{
663+
// https://bugs.openjdk.java.net/browse/JDK-8038747
664+
BlockScope skope = lambda.enclosingScope;
665+
if (substF.isFunctionalInterface(skope)) { // could be an inference variable.
666+
ReferenceBinding t = (ReferenceBinding) substF;
667+
ParameterizedTypeBinding withWildCards = InferenceContext18.parameterizedWithWildcard(t);
668+
if (withWildCards != null) {
669+
t = ConstraintExpressionFormula.findGroundTargetType(this, skope, lambda, withWildCards);
670+
}
671+
MethodBinding functionType;
672+
if (t != null && (functionType = t.getSingleAbstractMethod(skope, true)) != null && (lambda = lambda.resolveExpressionExpecting(t, this.scope)) != null) {
673+
TypeBinding r = functionType.returnType;
674+
Expression[] resultExpressions = lambda.resultExpressions();
675+
for (int i = 0, length = resultExpressions == null ? 0 : resultExpressions.length; i < length; i++) {
676+
Expression resultExpression = resultExpressions[i];
677+
if (!addConstraintsToC_OneExpr(resultExpression, c, r.original(), r, method))
678+
return false;
679+
}
680+
}
681+
}
682+
return true;
683+
}
684+
655685
private boolean addConstraintsToC_OneExpr(Expression expri, Set<ConstraintFormula> c, TypeBinding fsi, TypeBinding substF, MethodBinding method)
656686
throws InferenceFailureException
657687
{
@@ -666,27 +696,9 @@ private boolean addConstraintsToC_OneExpr(Expression expri, Set<ConstraintFormul
666696
}
667697
if (expri instanceof FunctionalExpression) {
668698
c.add(new ConstraintExceptionFormula((FunctionalExpression) expri, substF));
669-
if (expri instanceof LambdaExpression) {
670-
// https://bugs.openjdk.java.net/browse/JDK-8038747
671-
LambdaExpression lambda = (LambdaExpression) expri;
672-
BlockScope skope = lambda.enclosingScope;
673-
if (substF.isFunctionalInterface(skope)) { // could be an inference variable.
674-
ReferenceBinding t = (ReferenceBinding) substF;
675-
ParameterizedTypeBinding withWildCards = InferenceContext18.parameterizedWithWildcard(t);
676-
if (withWildCards != null) {
677-
t = ConstraintExpressionFormula.findGroundTargetType(this, skope, lambda, withWildCards);
678-
}
679-
MethodBinding functionType;
680-
if (t != null && (functionType = t.getSingleAbstractMethod(skope, true)) != null && (lambda = lambda.resolveExpressionExpecting(t, this.scope)) != null) {
681-
TypeBinding r = functionType.returnType;
682-
Expression[] resultExpressions = lambda.resultExpressions();
683-
for (int i = 0, length = resultExpressions == null ? 0 : resultExpressions.length; i < length; i++) {
684-
Expression resultExpression = resultExpressions[i];
685-
if (!addConstraintsToC_OneExpr(resultExpression, c, r.original(), r, method))
686-
return false;
687-
}
688-
}
689-
}
699+
// https://bugs.openjdk.org/browse/JDK-8052325
700+
if (expri instanceof LambdaExpression lambda && !lambda.argumentsTypeElided()) {
701+
addLambdaConstraintsToC(lambda, c, method, substF);
690702
}
691703
} else if (expri instanceof Invocation && expri.isPolyExpression()) {
692704

@@ -716,7 +728,6 @@ private boolean addConstraintsToC_OneExpr(Expression expri, Set<ConstraintFormul
716728
if (!innerContext.computeB3(invocation, substF, shallowMethod))
717729
return false;
718730
if (innerContext.addConstraintsToC(arguments, c, innerMethod.genericMethod(), innerContext.inferenceKind, invocation)) {
719-
this.currentBounds.addBounds(innerContext.currentBounds, this.environment, false);
720731
return true;
721732
}
722733
return false;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7058,5 +7058,23 @@ interface C<W> {
70587058
},
70597059
"");
70607060
}
7061+
7062+
public void testGH4557() {
7063+
runConformTest(new String[] {
7064+
"Test.java",
7065+
"""
7066+
import java.util.List;
7067+
public class Test {
7068+
public static void main(List<?> l) {
7069+
get(get(get(l)));
7070+
}
7071+
public static <T> List<?> get(List<T> l) {
7072+
return l;
7073+
}
7074+
}
7075+
"""
7076+
},
7077+
"");
7078+
}
70617079
}
70627080

0 commit comments

Comments
 (0)