Implement proper expression generation for the last script line

fixes problem with result fields
#KT-33127 fixed
This commit is contained in:
Ilya Chernikov
2019-08-06 06:34:34 +02:00
parent ec54246b3a
commit cddabf140b
2 changed files with 22 additions and 2 deletions
@@ -484,7 +484,9 @@ public abstract class MemberCodegen<T extends KtPureElement/* TODO: & KtDeclarat
protected void generateInitializers(@NotNull Function0<ExpressionCodegen> createCodegen) {
NotNullLazyValue<ExpressionCodegen> codegen = LockBasedStorageManager.NO_LOCKS.createLazyValue(createCodegen);
for (KtDeclaration declaration : ((KtDeclarationContainer) element).getDeclarations()) {
List<KtDeclaration> declarations = ((KtDeclarationContainer) element).getDeclarations();
for (int i = 0; i < declarations.size(); i++) {
KtDeclaration declaration = declarations.get(i);
if (declaration instanceof KtProperty) {
if (shouldInitializeProperty((KtProperty) declaration)) {
generateNopSeparatorIfNeeded(codegen);
@@ -501,7 +503,11 @@ public abstract class MemberCodegen<T extends KtPureElement/* TODO: & KtDeclarat
generateNopSeparatorIfNeeded(codegen);
ExpressionCodegen expressionCodegen = codegen.invoke();
expressionCodegen.gen(body, Type.VOID_TYPE);
Type bodyExpressionType = Type.VOID_TYPE;
if (i == declarations.size() - 1 && this instanceof ScriptCodegen) {
bodyExpressionType = expressionCodegen.expressionType(body);
}
expressionCodegen.gen(body, bodyExpressionType);
expressionCodegen.markLineNumber(declaration, true);
nopSeparatorNeeded = true;
}
@@ -55,6 +55,20 @@ class ReplTest : TestCase() {
)
}
@Test
fun testEvalWithIfResult() {
chechEvaluateInRepl(
simpleScriptompilationConfiguration,
simpleScriptEvaluationConfiguration,
sequenceOf(
"val x = 5",
"x + 6",
"if (x < 10) res1 * 2 else x"
),
sequenceOf(null, 11, 22)
)
}
@Test
fun testImplicitReceiver() {
val receiver = TestReceiver()