From 68680f702b0f6f0578e12e06620744680f5e13af Mon Sep 17 00:00:00 2001 From: pTalanov Date: Sat, 5 May 2012 15:39:53 +0400 Subject: [PATCH] Fix a bug which caused incorrectly generated js when you call extension literal from extension function. --- .../k2js/test/semantics/MiscTest.java | 5 +++- .../reference/CallParametersResolver.java | 24 ++++++++++--------- ...ionLiteralCalledInsideExtensionFunction.kt | 18 ++++++++++++++ 3 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 js/js.translator/testFiles/expression/misc/cases/extensionLiteralCalledInsideExtensionFunction.kt diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java index e6b3a42c8d5..7fac6d8226b 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java @@ -27,7 +27,6 @@ import org.mozilla.javascript.JavaScriptException; */ public final class MiscTest extends AbstractExpressionTest { - public MiscTest() { super("misc/"); } @@ -122,4 +121,8 @@ public final class MiscTest extends AbstractExpressionTest { public void testElvis() throws Exception { checkFooBoxIsTrue("elvis.kt"); } + + public void testExtensionLiteralCalledInsideExtensionFunction() throws Exception { + checkFooBoxIsTrue("extensionLiteralCalledInsideExtensionFunction.kt"); + } } diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/reference/CallParametersResolver.java b/js/js.translator/src/org/jetbrains/k2js/translate/reference/CallParametersResolver.java index e49a9d82435..c107c3bdf77 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/reference/CallParametersResolver.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/reference/CallParametersResolver.java @@ -19,14 +19,16 @@ package org.jetbrains.k2js.translate.reference; import com.google.dart.compiler.backend.js.ast.JsExpression; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.descriptors.*; +import org.jetbrains.jet.lang.descriptors.CallableDescriptor; +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; +import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; import org.jetbrains.jet.lang.resolve.calls.ResolvedCall; import org.jetbrains.jet.lang.resolve.calls.ResolvedCallWithTrace; import org.jetbrains.jet.lang.resolve.calls.VariableAsFunctionResolvedCall; import org.jetbrains.k2js.translate.context.TranslationContext; import org.jetbrains.k2js.translate.utils.TranslationUtils; -import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.*; +import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.getExpectedReceiverDescriptor; /** * @author Pavel Talanov @@ -34,10 +36,10 @@ import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.*; public final class CallParametersResolver { public static CallParameters resolveCallParameters(@Nullable JsExpression qualifier, - @Nullable JsExpression callee, - @NotNull CallableDescriptor descriptor, - @NotNull ResolvedCall call, - @NotNull TranslationContext context) { + @Nullable JsExpression callee, + @NotNull CallableDescriptor descriptor, + @NotNull ResolvedCall call, + @NotNull TranslationContext context) { return (new CallParametersResolver(qualifier, callee, descriptor, call, context)).resolve(); } @@ -55,10 +57,10 @@ public final class CallParametersResolver { private final boolean isExtensionCall; public CallParametersResolver(@Nullable JsExpression qualifier, - @Nullable JsExpression callee, - @NotNull CallableDescriptor descriptor, - @NotNull ResolvedCall call, - @NotNull TranslationContext context) { + @Nullable JsExpression callee, + @NotNull CallableDescriptor descriptor, + @NotNull ResolvedCall call, + @NotNull TranslationContext context) { this.qualifier = qualifier; this.callee = callee; this.descriptor = descriptor; @@ -104,7 +106,7 @@ public final class CallParametersResolver { if (qualifier != null) { return qualifier; } - DeclarationDescriptor expectedReceiverDescriptor = getExpectedReceiverDescriptor(descriptor); + DeclarationDescriptor expectedReceiverDescriptor = getExpectedReceiverDescriptor(resolvedCall.getResultingDescriptor()); assert expectedReceiverDescriptor != null; return TranslationUtils.getThisObject(context, expectedReceiverDescriptor); } diff --git a/js/js.translator/testFiles/expression/misc/cases/extensionLiteralCalledInsideExtensionFunction.kt b/js/js.translator/testFiles/expression/misc/cases/extensionLiteralCalledInsideExtensionFunction.kt new file mode 100644 index 00000000000..fe46faf30df --- /dev/null +++ b/js/js.translator/testFiles/expression/misc/cases/extensionLiteralCalledInsideExtensionFunction.kt @@ -0,0 +1,18 @@ +package foo + +fun A.create(init : A.() -> Unit) : A { + init() + return this +} + +fun box() : Boolean { + val a = A().create { + c = 1 + t + } + return a.c == 4 +} + +class A() { + val t = 3 + var c = 2 +} \ No newline at end of file