[FIR] Fix transform of subject of when expressions with subject variable

This commit is contained in:
Dmitriy Novozhilov
2020-03-13 14:17:41 +03:00
parent 820da6edaa
commit 37aa234bb7
6 changed files with 27 additions and 0 deletions
@@ -0,0 +1,3 @@
fun test(func: () -> String?) {
val x = func() ?: ""
}
@@ -0,0 +1,12 @@
FILE: invokeInWhenSubjectVariableInitializer.kt
public final fun test(func: R|() -> kotlin/String?|): R|kotlin/Unit| {
lval x: R|kotlin/String| = when (lval <elvis>: R|kotlin/String?| = R|<local>/func|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/String?|>|()) {
==($subj$, Null(null)) -> {
String()
}
else -> {
R|<local>/<elvis>|
}
}
}
@@ -203,6 +203,11 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest {
runTest("compiler/fir/resolve/testData/resolve/intersectionTypes.kt");
}
@TestMetadata("invokeInWhenSubjectVariableInitializer.kt")
public void testInvokeInWhenSubjectVariableInitializer() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/invokeInWhenSubjectVariableInitializer.kt");
}
@TestMetadata("invokeOfLambdaWithReceiver.kt")
public void testInvokeOfLambdaWithReceiver() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/invokeOfLambdaWithReceiver.kt");
@@ -203,6 +203,11 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos
runTest("compiler/fir/resolve/testData/resolve/intersectionTypes.kt");
}
@TestMetadata("invokeInWhenSubjectVariableInitializer.kt")
public void testInvokeInWhenSubjectVariableInitializer() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/invokeInWhenSubjectVariableInitializer.kt");
}
@TestMetadata("invokeOfLambdaWithReceiver.kt")
public void testInvokeOfLambdaWithReceiver() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/invokeOfLambdaWithReceiver.kt");
@@ -58,6 +58,7 @@ internal class FirWhenExpressionImpl(
override fun <D> transformSubject(transformer: FirTransformer<D>, data: D): FirWhenExpressionImpl {
if (subjectVariable != null) {
subjectVariable = subjectVariable?.transformSingle(transformer, data)
subject = subjectVariable?.initializer
} else {
subject = subject?.transformSingle(transformer, data)
}
@@ -266,6 +266,7 @@ fun SmartPrinter.printImplementation(implementation: Implementation) {
"""
|if (subjectVariable != null) {
| subjectVariable = subjectVariable?.transformSingle(transformer, data)
| subject = subjectVariable?.initializer
| } else {
| subject = subject?.transformSingle(transformer, data)
| }