diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.java index 03b44d12e07..2dd9da1bdeb 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.java @@ -34,6 +34,7 @@ import org.jetbrains.kotlin.load.kotlin.PackageClassUtils; import org.jetbrains.kotlin.name.ClassId; import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.renderer.DescriptorRenderer; +import org.jetbrains.kotlin.resolve.BindingContext; import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils; import org.jetbrains.kotlin.resolve.DescriptorUtils; import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall; @@ -272,16 +273,27 @@ public class InlineCodegen extends CallGenerator { final CallableMemberDescriptor descriptor = codegen.getContext().getContextDescriptor(); - final boolean isLambda = isFunctionExpression(descriptor) || isFunctionLiteral(descriptor); - @Override public boolean isMyLabel(@NotNull String name) { if (InlineCodegenUtil.ROOT_LABEL.equals(name)) { - return !isLambda; + return !isFunctionLiteral(descriptor); } - else { - return descriptor.getName().asString().equals(name); + + //check function name if exists + if (descriptor.getName().asString().equals(name)) { + return true; } + + //check functional expression labels + if (isFunctionExpression(descriptor)) { + PsiElement element = DescriptorToSourceUtils.descriptorToDeclaration(descriptor); + if (element != null && element.getParent() instanceof JetLabeledExpression) { + String labelName = ((JetLabeledExpression) element.getParent()).getLabelName(); + return name.equals(labelName); + } + } + + return false; } }; List infos = MethodInliner.processReturns(adapter, labelOwner, true, null); diff --git a/compiler/testData/codegen/boxInline/nameless/extension.1.kt b/compiler/testData/codegen/boxInline/functionExpression/extension.1.kt similarity index 100% rename from compiler/testData/codegen/boxInline/nameless/extension.1.kt rename to compiler/testData/codegen/boxInline/functionExpression/extension.1.kt diff --git a/compiler/testData/codegen/boxInline/nameless/extension.2.kt b/compiler/testData/codegen/boxInline/functionExpression/extension.2.kt similarity index 100% rename from compiler/testData/codegen/boxInline/nameless/extension.2.kt rename to compiler/testData/codegen/boxInline/functionExpression/extension.2.kt diff --git a/compiler/testData/codegen/boxInline/nonLocalReturns/returnFromFunctionExpr.1.kt b/compiler/testData/codegen/boxInline/nonLocalReturns/returnFromFunctionExpr.1.kt new file mode 100644 index 00000000000..4518ac23c79 --- /dev/null +++ b/compiler/testData/codegen/boxInline/nonLocalReturns/returnFromFunctionExpr.1.kt @@ -0,0 +1,23 @@ +//NO_CHECK_LAMBDA_INLINING +fun test(): String = fun (): String { + foo { return "OK" } + return "fail" +} () + +fun test2(): String = (@l fun (): String { + foo { return@l "OK" } + return "fail" +}) () + +fun test3(): String = (@l fun bar(): String { + foo { return@bar "OK" } + return "fail" +}) () + +fun box(): String { + if (test() != "OK") return "fail 1: ${test()}" + + if (test2() != "OK") return "fail 2: ${test2()}" + + return test3() +} \ No newline at end of file diff --git a/compiler/testData/codegen/boxInline/nonLocalReturns/returnFromFunctionExpr.2.kt b/compiler/testData/codegen/boxInline/nonLocalReturns/returnFromFunctionExpr.2.kt new file mode 100644 index 00000000000..111e1a6eed5 --- /dev/null +++ b/compiler/testData/codegen/boxInline/nonLocalReturns/returnFromFunctionExpr.2.kt @@ -0,0 +1,3 @@ +inline fun foo(f: () -> Unit) { + f() +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java index 220e5743a72..73b5a6d19e8 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java @@ -287,6 +287,21 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo } } + @TestMetadata("compiler/testData/codegen/boxInline/functionExpression") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class FunctionExpression extends AbstractBlackBoxInlineCodegenTest { + public void testAllFilesPresentInFunctionExpression() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/functionExpression"), Pattern.compile("^(.+)\\.1.kt$"), true); + } + + @TestMetadata("extension.1.kt") + public void testExtension() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/functionExpression/extension.1.kt"); + doTestMultiFileWithInlineCheck(fileName); + } + } + @TestMetadata("compiler/testData/codegen/boxInline/lambdaClassClash") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -362,21 +377,6 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo } } - @TestMetadata("compiler/testData/codegen/boxInline/nameless") - @TestDataPath("$PROJECT_ROOT") - @RunWith(JUnit3RunnerWithInners.class) - public static class Nameless extends AbstractBlackBoxInlineCodegenTest { - public void testAllFilesPresentInNameless() throws Exception { - JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/nameless"), Pattern.compile("^(.+)\\.1.kt$"), true); - } - - @TestMetadata("extension.1.kt") - public void testExtension() throws Exception { - String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/nameless/extension.1.kt"); - doTestMultiFileWithInlineCheck(fileName); - } - } - @TestMetadata("compiler/testData/codegen/boxInline/noInline") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -460,6 +460,12 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo doTestMultiFileWithInlineCheck(fileName); } + @TestMetadata("returnFromFunctionExpr.1.kt") + public void testReturnFromFunctionExpr() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/nonLocalReturns/returnFromFunctionExpr.1.kt"); + doTestMultiFileWithInlineCheck(fileName); + } + @TestMetadata("simple.1.kt") public void testSimple() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/nonLocalReturns/simple.1.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java index 7bcbabd15d6..3316a5eaea9 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java @@ -287,6 +287,21 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi } } + @TestMetadata("compiler/testData/codegen/boxInline/functionExpression") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class FunctionExpression extends AbstractCompileKotlinAgainstInlineKotlinTest { + public void testAllFilesPresentInFunctionExpression() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/functionExpression"), Pattern.compile("^(.+)\\.1.kt$"), true); + } + + @TestMetadata("extension.1.kt") + public void testExtension() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/functionExpression/extension.1.kt"); + doBoxTestWithInlineCheck(fileName); + } + } + @TestMetadata("compiler/testData/codegen/boxInline/lambdaClassClash") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -362,21 +377,6 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi } } - @TestMetadata("compiler/testData/codegen/boxInline/nameless") - @TestDataPath("$PROJECT_ROOT") - @RunWith(JUnit3RunnerWithInners.class) - public static class Nameless extends AbstractCompileKotlinAgainstInlineKotlinTest { - public void testAllFilesPresentInNameless() throws Exception { - JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/nameless"), Pattern.compile("^(.+)\\.1.kt$"), true); - } - - @TestMetadata("extension.1.kt") - public void testExtension() throws Exception { - String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/nameless/extension.1.kt"); - doBoxTestWithInlineCheck(fileName); - } - } - @TestMetadata("compiler/testData/codegen/boxInline/noInline") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -460,6 +460,12 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi doBoxTestWithInlineCheck(fileName); } + @TestMetadata("returnFromFunctionExpr.1.kt") + public void testReturnFromFunctionExpr() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/nonLocalReturns/returnFromFunctionExpr.1.kt"); + doBoxTestWithInlineCheck(fileName); + } + @TestMetadata("simple.1.kt") public void testSimple() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/nonLocalReturns/simple.1.kt");