From 5b987ccc1525cf60c81f94942e61ea9f8c923a42 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Wed, 6 Jun 2012 17:17:44 +0200 Subject: [PATCH] when calling function stored in local variable as unary operation, don't forget to push function instance (KT-1953) I'm sure that there are similar cases that still don't work; please let me know if you find any #KT-1953 fixed --- .../jet/codegen/ExpressionCodegen.java | 34 ++++++++++++------- .../testData/codegen/regressions/kt1953.kt | 9 +++++ .../codegen/regressions/kt1953_class.kt | 14 ++++++++ .../jet/codegen/ExtensionFunctionsTest.java | 10 ++++++ 4 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 compiler/testData/codegen/regressions/kt1953.kt create mode 100644 compiler/testData/codegen/regressions/kt1953_class.kt diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index d1168e9ae88..97f57be0065 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -1023,19 +1023,7 @@ public class ExpressionCodegen extends JetVisitor { int index = lookupLocal(descriptor); if (index >= 0) { - if(descriptor instanceof VariableDescriptor) { - Type sharedVarType = typeMapper.getSharedVarType(descriptor); - final JetType outType = ((VariableDescriptor) descriptor).getType(); - if(sharedVarType != null) { - return StackValue.shared(index, asmType(outType)); - } - else { - return StackValue.local(index, asmType(outType)); - } - } - else { - return StackValue.local(index, TYPE_OBJECT); - } + return stackValueForLocal(descriptor, index); } if (descriptor instanceof PropertyDescriptor) { @@ -1157,6 +1145,22 @@ public class ExpressionCodegen extends JetVisitor { return value; } + private StackValue stackValueForLocal(DeclarationDescriptor descriptor, int index) { + if(descriptor instanceof VariableDescriptor) { + Type sharedVarType = typeMapper.getSharedVarType(descriptor); + final JetType outType = ((VariableDescriptor) descriptor).getType(); + if(sharedVarType != null) { + return StackValue.shared(index, asmType(outType)); + } + else { + return StackValue.local(index, asmType(outType)); + } + } + else { + return StackValue.local(index, TYPE_OBJECT); + } + } + public int lookupLocal(DeclarationDescriptor descriptor) { return myFrameMap.getIndex(descriptor); } @@ -2198,6 +2202,10 @@ public class ExpressionCodegen extends JetVisitor { } private StackValue invokeOperation(JetOperationExpression expression, FunctionDescriptor op, CallableMethod callable) { + int functionLocalIndex = myFrameMap.getIndex(op); + if (functionLocalIndex >= 0) { + stackValueForLocal(op, functionLocalIndex).put(ClosureCodegen.getInternalClassName(op).getAsmType(), v); + } ResolvedCall resolvedCall = bindingContext.get(BindingContext.RESOLVED_CALL, expression.getOperationReference()); assert resolvedCall != null; genThisAndReceiverFromResolvedCall(StackValue.none(), resolvedCall, callable); diff --git a/compiler/testData/codegen/regressions/kt1953.kt b/compiler/testData/codegen/regressions/kt1953.kt new file mode 100644 index 00000000000..6403f7f8c11 --- /dev/null +++ b/compiler/testData/codegen/regressions/kt1953.kt @@ -0,0 +1,9 @@ +fun box(): String { + val sb = StringBuilder() + fun String.plus() { + sb.append(this) + } + + +"OK" + return sb.toString()!! +} diff --git a/compiler/testData/codegen/regressions/kt1953_class.kt b/compiler/testData/codegen/regressions/kt1953_class.kt new file mode 100644 index 00000000000..c28f181ef5f --- /dev/null +++ b/compiler/testData/codegen/regressions/kt1953_class.kt @@ -0,0 +1,14 @@ +class A { + private val sb: StringBuilder = StringBuilder() + + fun String.plus { + sb.append(this) + } + + fun foo(): String { + +"OK" + return sb.toString()!! + } +} + +fun box(): String = A().foo() diff --git a/compiler/tests/org/jetbrains/jet/codegen/ExtensionFunctionsTest.java b/compiler/tests/org/jetbrains/jet/codegen/ExtensionFunctionsTest.java index 268b7eeb884..6adccef9854 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/ExtensionFunctionsTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/ExtensionFunctionsTest.java @@ -96,4 +96,14 @@ public class ExtensionFunctionsTest extends CodegenTestCase { createEnvironmentWithMockJdkAndIdeaAnnotations(CompilerSpecialMode.BUILTINS); blackBoxFile("regressions/kt1290.kt"); } + + public void testKt1953() throws Exception { + createEnvironmentWithMockJdkAndIdeaAnnotations(CompilerSpecialMode.JDK_HEADERS); + blackBoxFile("regressions/kt1953.kt"); + } + + public void testKt1953Class() throws Exception { + createEnvironmentWithMockJdkAndIdeaAnnotations(CompilerSpecialMode.JDK_HEADERS); + blackBoxFile("regressions/kt1953_class.kt"); + } }