From 629cdfc8221c245053d531f4629556d6e36cfc7a Mon Sep 17 00:00:00 2001 From: Alex Tkachman Date: Wed, 4 Jan 2012 12:54:10 +0200 Subject: [PATCH] KT-865 extension function as unary operation --- .../jet/codegen/ExpressionCodegen.java | 25 ++++++++++--------- .../testData/codegen/regressions/kt865.jet | 19 ++++++++++++++ .../jet/codegen/ExtensionFunctionsTest.java | 5 ++++ templatelib/test/TemplateCoreTest.kt | 9 ------- 4 files changed, 37 insertions(+), 21 deletions(-) create mode 100644 compiler/testData/codegen/regressions/kt865.jet diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index af17585cdef..cb4a2600153 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -1545,14 +1545,7 @@ public class ExpressionCodegen extends JetVisitor { Arrays.asList(expression.getLeft(), expression.getRight()), receiver); } else { - ResolvedCall resolvedCall = bindingContext.get(BindingContext.RESOLVED_CALL, expression.getOperationReference()); - assert resolvedCall != null; - CallableMethod callableMethod = (CallableMethod) callable; - genThisAndReceiverFromResolvedCall(StackValue.none(), resolvedCall, callableMethod); - pushTypeArguments(resolvedCall); - pushMethodArguments(resolvedCall, callableMethod.getValueParameterTypes()); - callableMethod.invoke(v); - return returnValueAsStackValue((FunctionDescriptor) op, callableMethod.getSignature().getAsmMethod().getReturnType()); + return invokeOperation(expression, (FunctionDescriptor)op, (CallableMethod) callable); } } } @@ -1928,13 +1921,21 @@ public class ExpressionCodegen extends JetVisitor { Arrays.asList(expression.getBaseExpression()), receiver); } else { - CallableMethod callableMethod = (CallableMethod) callable; - genToJVMStack(expression.getBaseExpression()); - callableMethod.invoke(v); - return returnValueAsStackValue((FunctionDescriptor) op, callableMethod.getSignature().getAsmMethod().getReturnType()); + return invokeOperation(expression, (FunctionDescriptor) op, (CallableMethod) callable); } } + private StackValue invokeOperation(JetOperationExpression expression, FunctionDescriptor op, CallableMethod callable) { + ResolvedCall resolvedCall = bindingContext.get(BindingContext.RESOLVED_CALL, expression.getOperationReference()); + assert resolvedCall != null; + CallableMethod callableMethod = (CallableMethod) callable; + genThisAndReceiverFromResolvedCall(StackValue.none(), resolvedCall, callableMethod); + pushTypeArguments(resolvedCall); + pushMethodArguments(resolvedCall, callableMethod.getValueParameterTypes()); + callableMethod.invoke(v); + return returnValueAsStackValue((FunctionDescriptor) op, callableMethod.getSignature().getAsmMethod().getReturnType()); + } + @Override public StackValue visitPostfixExpression(JetPostfixExpression expression, StackValue receiver) { DeclarationDescriptor op = bindingContext.get(BindingContext.REFERENCE_TARGET, expression.getOperationReference()); diff --git a/compiler/testData/codegen/regressions/kt865.jet b/compiler/testData/codegen/regressions/kt865.jet new file mode 100644 index 00000000000..c47829aa612 --- /dev/null +++ b/compiler/testData/codegen/regressions/kt865.jet @@ -0,0 +1,19 @@ +import java.util.* + +class Template() { + val collected = LinkedList() + + fun String.plus() { + collected.add(this@plus) + } + + fun test() { + + "239" + } +} + +fun box() : String { + val u = Template() + u.test() + return if(u.collected.size() == 1 && u.collected.get(0) == "239") "OK" else "fail" +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/codegen/ExtensionFunctionsTest.java b/compiler/tests/org/jetbrains/jet/codegen/ExtensionFunctionsTest.java index e26bd1b88ae..c03aebc041c 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/ExtensionFunctionsTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/ExtensionFunctionsTest.java @@ -42,4 +42,9 @@ public class ExtensionFunctionsTest extends CodegenTestCase { public void testKt475() throws Exception { blackBoxFile("regressions/kt475.jet"); } + + public void testKt865() throws Exception { + createEnvironmentWithFullJdk(); + blackBoxFile("regressions/kt865.jet"); + } } diff --git a/templatelib/test/TemplateCoreTest.kt b/templatelib/test/TemplateCoreTest.kt index 4a118a94f45..44633b477ac 100644 --- a/templatelib/test/TemplateCoreTest.kt +++ b/templatelib/test/TemplateCoreTest.kt @@ -13,16 +13,11 @@ class EmailTemplate(var name: String = "James", var time: Date = Date()) : TextT } } -/** - TODO compile error - http://youtrack.jetbrains.net/issue/KT-865 - class MoreDryTemplate(var name: String = "James", var time: Date = Date()) : TextTemplate() { override fun render() { +"Hey there $name and how are you? Today is $time. Kotlin rocks" } } -*/ class TemplateCoreTest() : TestSupport() { fun testDefaultValues() { @@ -41,9 +36,6 @@ class TemplateCoreTest() : TestSupport() { } } - /* - TODO compile error - fun testMoreDryTemplate() { val text = MoreDryTemplate("Alex").renderToText() assert { @@ -51,5 +43,4 @@ class TemplateCoreTest() : TestSupport() { text.startsWith("Hey there Alex") } } - */ } \ No newline at end of file