diff --git a/compiler/testData/codegen/box/functions/functionExpression/functionExpression.kt b/compiler/testData/codegen/box/functions/functionExpression/functionExpression.kt index d40bdd72916..bf0c522d25f 100644 --- a/compiler/testData/codegen/box/functions/functionExpression/functionExpression.kt +++ b/compiler/testData/codegen/box/functions/functionExpression/functionExpression.kt @@ -18,12 +18,12 @@ fun box() : String { if( 1.(fun Int.(i: Int) = i + this)(1) != 2) return "test 6 failed"; if( (fooT1()("mama")) != "mama") return "test 7 failed"; - val a = [A] fun Int.() = this + 1 // + val a = [A] fun Int.() = this + 1 if (1.a() != 2) return "test 8 failed" val b = ( fun Int.() = this + 1) - if (1.a() != 2) return "test 9 failed" + if (1.b() != 2) return "test 9 failed" val c = (@c fun Int.() = this + 1) - if (1.a() != 2) return "test 10 failed" + if (1.c() != 2) return "test 10 failed" val d = @d fun (): Int { return@d 4} if (d() != 4) return "test 11 failed" diff --git a/compiler/testData/codegen/box/functions/functionExpression/functionExpressionWithName.kt b/compiler/testData/codegen/box/functions/functionExpression/functionExpressionWithName.kt index 13d61b76b80..b736dc45b74 100644 --- a/compiler/testData/codegen/box/functions/functionExpression/functionExpressionWithName.kt +++ b/compiler/testData/codegen/box/functions/functionExpression/functionExpressionWithName.kt @@ -21,9 +21,9 @@ fun box() : String { val a = [A] fun Int.name() = this + 1 // name if (1.a() != 2) return "test 8 failed" val b = ( fun Int.name() = this + 1) - if (1.a() != 2) return "test 9 failed" + if (1.b() != 2) return "test 9 failed" val c = (@c fun Int.name() = this + 1) - if (1.a() != 2) return "test 10 failed" + if (1.c() != 2) return "test 10 failed" val d = fun name(): Int { return@name 4} if (d() != 4) return "test 11 failed" diff --git a/compiler/testData/codegen/box/functions/functionExpression/functionLiteralExpression.kt b/compiler/testData/codegen/box/functions/functionExpression/functionLiteralExpression.kt index 81f4695ff78..ec2a051bc9d 100644 --- a/compiler/testData/codegen/box/functions/functionExpression/functionLiteralExpression.kt +++ b/compiler/testData/codegen/box/functions/functionExpression/functionLiteralExpression.kt @@ -10,6 +10,8 @@ fun fooT1(t : T) = { t.toString() } fun fooT2(t: T) = { x:T -> t.toString() + x.toString() } +object t + fun box() : String { if( (10.foo1())() != "23910") return "foo1 fail" if( (10.foo2())(1) != 11 ) return "foo2 fail" @@ -18,7 +20,7 @@ fun box() : String { if( {1}() != 1) return "test 4 failed"; if( {x : Int -> x}(1) != 1) return "test 5 failed"; if( 1.(fun Int.(x: Int) = x + this)(1) != 2) return "test 6 failed"; - if( 1.(fun Int.() = this)() != 1) return "test 7 failed"; + if( t.(fun Any.() = this)() != t) return "test 7 failed"; if( (fooT1("mama"))() != "mama") return "test 8 failed"; if( (fooT2("mama"))("papa") != "mamapapa") return "test 9 failed"; return "OK" diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index c469c628063..b0aa3910f3f 100644 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -843,6 +843,11 @@ fun main(args: Array) { testClass(javaClass()) { model("codegen/box/objectIntrinsics", targetBackend = TargetBackend.JS) } + + testClass(javaClass()) { + model("codegen/box/functions/functionExpression", targetBackend = TargetBackend.JS) + } + } } diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/FunctionExpressionTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/FunctionExpressionTestGenerated.java new file mode 100644 index 00000000000..1451c938991 --- /dev/null +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/FunctionExpressionTestGenerated.java @@ -0,0 +1,61 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.js.test.semantics; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; +import org.jetbrains.kotlin.test.JetTestUtils; +import org.jetbrains.kotlin.test.TestMetadata; +import org.junit.runner.RunWith; + +import java.io.File; +import java.util.regex.Pattern; + +/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("compiler/testData/codegen/box/functions/functionExpression") +@TestDataPath("$PROJECT_ROOT") +@RunWith(JUnit3RunnerWithInners.class) +public class FunctionExpressionTestGenerated extends AbstractFunctionExpressionTest { + public void testAllFilesPresentInFunctionExpression() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/functions/functionExpression"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("functionExpression.kt") + public void testFunctionExpression() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/functions/functionExpression/functionExpression.kt"); + doTest(fileName); + } + + @TestMetadata("functionExpressionWithName.kt") + public void testFunctionExpressionWithName() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/functions/functionExpression/functionExpressionWithName.kt"); + doTest(fileName); + } + + @TestMetadata("functionExpressionWithThisReference.kt") + public void testFunctionExpressionWithThisReference() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/functions/functionExpression/functionExpressionWithThisReference.kt"); + doTest(fileName); + } + + @TestMetadata("functionLiteralExpression.kt") + public void testFunctionLiteralExpression() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/functions/functionExpression/functionLiteralExpression.kt"); + doTest(fileName); + } +} diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/abstarctClassesForGeneratedTests.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/abstarctClassesForGeneratedTests.kt index 2268b0c6b70..f21b739ef29 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/abstarctClassesForGeneratedTests.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/abstarctClassesForGeneratedTests.kt @@ -20,16 +20,20 @@ import org.jetbrains.kotlin.js.test.AbstractSingleFileTranslationWithDirectivesT import org.jetbrains.kotlin.js.test.MultipleModulesTranslationTest import org.jetbrains.kotlin.js.test.SingleFileTranslationTest -public abstract class AbstractBridgeTest : SingleFileTranslationTest("bridges/") { +private abstract class AbstractBlackBoxTest(d: String) : SingleFileTranslationTest(d) { override fun doTest(filename: String) = checkBlackBoxIsOkByPath(filename) } +public abstract class AbstractBridgeTest : AbstractBlackBoxTest("bridges/") + public abstract class AbstractCallableReferenceTest(main: String) : SingleFileTranslationTest("callableReference/" + main) public abstract class AbstractCompanionObjectTest : SingleFileTranslationTest("objectIntrinsics/") public abstract class AbstractDynamicTest : SingleFileTranslationTest("dynamic/") +public abstract class AbstractFunctionExpressionTest : AbstractBlackBoxTest("functionExpression/") + public abstract class AbstractInlineEvaluationOrderTest : AbstractSingleFileTranslationWithDirectivesTest("inlineEvaluationOrder/") public abstract class AbstractInlineJsStdlibTest : AbstractSingleFileTranslationWithDirectivesTest("inlineStdlib/") diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/ExpressionVisitor.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/ExpressionVisitor.java index 39de547dc47..16b24f8f32e 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/ExpressionVisitor.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/ExpressionVisitor.java @@ -417,7 +417,10 @@ public final class ExpressionVisitor extends TranslatorVisitor { MetadataPackage.setStaticRef(name, alias); } - return new JsVars(new JsVars.JsVar(name, alias)).source(expression); + boolean isExpression = BindingContextUtilPackage.isUsedAsExpression(expression, context.bindingContext()); + JsNode result = isExpression ? alias : JsAstUtils.newVar(name, alias); + + return result.source(expression); } @Override