Skip to content

Commit bc7c7d4

Browse files
committed
Fix nested inference
1 parent 545483d commit bc7c7d4

2 files changed

Lines changed: 57 additions & 22 deletions

File tree

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

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,12 @@ public BoundSet inferInvocationType(TypeBinding expectedType, InvocationSite inv
427427
return null;
428428
// 5. bullet: determine B4 from C
429429
List<Set<InferenceVariable>> components = this.currentBounds.computeConnectedComponents(this.inferenceVariables);
430+
int variableCount = this.inferenceVariables.length;
430431
while (!c.isEmpty()) {
432+
if (variableCount < this.inferenceVariables.length) {
433+
components = this.currentBounds.computeConnectedComponents(this.inferenceVariables);
434+
variableCount = this.inferenceVariables.length;
435+
}
431436
// *
432437
Set<ConstraintFormula> bottomSet = findBottomSet(c, allOutputVariables(c), components);
433438
if (bottomSet.isEmpty()) {
@@ -461,6 +466,12 @@ public BoundSet inferInvocationType(TypeBinding expectedType, InvocationSite inv
461466
if (!this.currentBounds.reduceOneConstraint(this, constraint))
462467
return null;
463468
}
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+
}
464475
}
465476
// 6. bullet: solve
466477
BoundSet solution = solve();
@@ -646,6 +657,31 @@ private boolean addConstraintsToC(Expression[] exprs, Set<ConstraintFormula> c,
646657
return true;
647658
}
648659

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, this)) != 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+
649685
private boolean addConstraintsToC_OneExpr(Expression expri, Set<ConstraintFormula> c, TypeBinding fsi, TypeBinding substF, MethodBinding method)
650686
throws InferenceFailureException
651687
{
@@ -660,27 +696,9 @@ private boolean addConstraintsToC_OneExpr(Expression expri, Set<ConstraintFormul
660696
}
661697
if (expri instanceof FunctionalExpression) {
662698
c.add(new ConstraintExceptionFormula((FunctionalExpression) expri, substF));
663-
if (expri instanceof LambdaExpression) {
664-
// https://bugs.openjdk.java.net/browse/JDK-8038747
665-
LambdaExpression lambda = (LambdaExpression) expri;
666-
BlockScope skope = lambda.enclosingScope;
667-
if (substF.isFunctionalInterface(skope)) { // could be an inference variable.
668-
ReferenceBinding t = (ReferenceBinding) substF;
669-
ParameterizedTypeBinding withWildCards = InferenceContext18.parameterizedWithWildcard(t);
670-
if (withWildCards != null) {
671-
t = ConstraintExpressionFormula.findGroundTargetType(this, skope, lambda, withWildCards);
672-
}
673-
MethodBinding functionType;
674-
if (t != null && (functionType = t.getSingleAbstractMethod(skope, true)) != null && (lambda = lambda.resolveExpressionExpecting(t, this.scope, this)) != null) {
675-
TypeBinding r = functionType.returnType;
676-
Expression[] resultExpressions = lambda.resultExpressions();
677-
for (int i = 0, length = resultExpressions == null ? 0 : resultExpressions.length; i < length; i++) {
678-
Expression resultExpression = resultExpressions[i];
679-
if (!addConstraintsToC_OneExpr(resultExpression, c, r.original(), r, method))
680-
return false;
681-
}
682-
}
683-
}
699+
// https://bugs.openjdk.org/browse/JDK-8052325
700+
if (expri instanceof LambdaExpression lambda && !lambda.argumentsTypeElided()) {
701+
addLambdaConstraintsToC(lambda, c, method, substF);
684702
}
685703
} else if (expri instanceof Invocation && expri.isPolyExpression()) {
686704

@@ -710,7 +728,6 @@ private boolean addConstraintsToC_OneExpr(Expression expri, Set<ConstraintFormul
710728
if (!innerContext.computeB3(invocation, substF, shallowMethod))
711729
return false;
712730
if (innerContext.addConstraintsToC(arguments, c, innerMethod.genericMethod(), innerContext.inferenceKind, invocation)) {
713-
this.currentBounds.addBounds(innerContext.currentBounds, this.environment);
714731
return true;
715732
}
716733
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)