Fix a bug with nested lambdas

This commit is contained in:
Clément Fournier committed 2021-03-08 16:11:15 +01:00
1 parent cf6f0f0aa8
commit a71ad27200
2 files changed
+51 -1

No files matched your search

@@ -454,7 +454,9 @@ final class PolyResolution {
}
return node instanceof ASTSwitchExpression && child.getIndexInParent() != 0 // not the condition
|| node instanceof ASTSwitchArrowBranch
|| node instanceof ASTConditionalExpression && child.getIndexInParent() != 0; // not the condition
|| node instanceof ASTConditionalExpression && child.getIndexInParent() != 0 // not the condition
// lambdas "forward the context" when you have nested lambdas, eg: `x -> y -> f(x, y)`
|| node instanceof ASTLambdaExpression && child.getIndexInParent() == 1; // the body expression
}
@@ -677,4 +677,52 @@ class NodeStream<T> {
}
}
parserTest("Lambda bug with nested lambdas") {
fun makeTest(insideOut: Boolean) {
val (acu, spy) = parser.parseWithTypeInferenceSpy(
"""
interface Function<U,V> {
V apply(U u);
}
class Scratch {
<T> void chainingWithLambda(Function<?, ? extends T> f) {
this.<Function<Scratch, String>>chainingWithLambda(x -> y -> y.contains(0));
}
}
""".trimIndent()
)
val (t_Function, t_Scratch) = acu.declaredTypeSignatures()
val (lambdaX, lambdaY) = acu.descendants(ASTLambdaExpression::class.java).crossFindBoundaries()
.toList()
spy.shouldBeOk {
val t_lambdaY = t_Function[t_Scratch, ts.STRING]
val t_lambdaX = t_Function[ts.OBJECT, t_lambdaY]
if (insideOut) {
lambdaY shouldHaveType t_lambdaY
lambdaX shouldHaveType t_lambdaX
} else {
lambdaX shouldHaveType t_lambdaX
lambdaY shouldHaveType t_lambdaY
}
}
}
doTest("Outside in") {
makeTest(false)
}
doTest("Inside out") {
makeTest(true)
}
}
})