diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ControlStructureTypingVisitor.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ControlStructureTypingVisitor.java index 5f3c9d447db..31409cf1a38 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ControlStructureTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ControlStructureTypingVisitor.java @@ -522,18 +522,16 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor { SimpleFunctionDescriptor functionDescriptor = context.trace.get(FUNCTION, labelTargetElement); if (functionDescriptor != null) { expectedType = DescriptorUtils.getFunctionExpectedReturnType(functionDescriptor, labelTargetElement); - boolean inLambdaWithNoExplicitType = expectedType == TypeUtils.NO_EXPECTED_TYPE; - if (inLambdaWithNoExplicitType) { - // expectedType is NO_EXPECTED_TYPE iff the return type of the corresponding function descriptor is not computed yet - // our temporary policy is to prohibit returns in this case. It mostly applies to local returns in lambdas - context.trace.report(RETURN_NOT_ALLOWED_EXPLICIT_RETURN_TYPE_REQUIRED.on(expression)); - resultType = ErrorUtils.createErrorType(RETURN_NOT_ALLOWED_MESSAGE); - } - else if (functionDescriptor != containingFunctionDescriptor) { + if (functionDescriptor != containingFunctionDescriptor) { // Qualified, non-local context.trace.report(RETURN_NOT_ALLOWED.on(expression)); resultType = ErrorUtils.createErrorType(RETURN_NOT_ALLOWED_MESSAGE); } + else if (expectedType == TypeUtils.NO_EXPECTED_TYPE) { + // expectedType is NO_EXPECTED_TYPE iff the return type of the corresponding function descriptor is not computed yet + // our temporary policy is to prohibit returns in this case. It mostly applies to local returns in lambdas + context.trace.report(RETURN_NOT_ALLOWED_EXPLICIT_RETURN_TYPE_REQUIRED.on(expression)); + } } else { context.trace.report(NOT_A_RETURN_LABEL.on(expression, expression.getLabelName())); diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalAndNonLocalReturnInLambda.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalAndNonLocalReturnInLambda.kt index 2141d0cdbc4..e1983714617 100644 --- a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalAndNonLocalReturnInLambda.kt +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalAndNonLocalReturnInLambda.kt @@ -1,8 +1,9 @@ fun test2(a: Int) { - (run @f{ + val x = run @f{ if (a > 0) return return@f 1 - }): Int + } + x: Int } fun run(f: () -> T): T { return f() } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnExplicitLabelNoParens.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnExplicitLabelNoParens.kt index ff517b875f1..f771704f813 100644 --- a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnExplicitLabelNoParens.kt +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnExplicitLabelNoParens.kt @@ -1,5 +1,6 @@ fun test2() { - (run @f{return@f 1}): Int + val x = run @f{return@f 1} + x: Int } fun run(f: () -> T): T { return f() } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnExplicitLabelParens.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnExplicitLabelParens.kt index e9fbb39b72a..0f5c1340181 100644 --- a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnExplicitLabelParens.kt +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnExplicitLabelParens.kt @@ -1,10 +1,12 @@ fun test() { - run(@f{return@f 1}): Int + val x = run(@f{return@f 1}) + x: Int } fun test1() { - run(@{return@ 1}): Int + val x = run(@{return@ 1}) + x: Int } fun run(f: () -> T): T { return f() } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnInNestedFunction.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnInNestedFunction.kt index 20b4c2f540c..7f262734dc9 100644 --- a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnInNestedFunction.kt +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnInNestedFunction.kt @@ -1,11 +1,12 @@ fun test() { - (run @f{ + val x = run @f{ fun local(a: Int): String { if (a > 0) return "2" return@local "3" } return@f 1 - }): Int + } + x: Int } fun run(f: () -> T): T { return f() } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnInNestedLambda.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnInNestedLambda.kt index e435d501e08..ae5a9010b5c 100644 --- a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnInNestedLambda.kt +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnInNestedLambda.kt @@ -1,10 +1,11 @@ fun test() { - (run @f{ + val x = run @f{ run @ff { return@ff "2" } return@f 1 - }): Int + } + x: Int } fun run(f: () -> T): T { return f() } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnUnit.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnUnit.kt index ac427a12443..18edcdb3aa9 100644 --- a/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnUnit.kt +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/LocalReturnUnit.kt @@ -1,8 +1,9 @@ fun test(a: Int) { - (run @f{ + val x = run @f{ if (a > 0) return@f else return@f Unit.VALUE - }): Unit + } + x: Unit } fun run(f: () -> T): T { return f() } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/functionLiterals/return/MixedReturnsFromLambda.kt b/compiler/testData/diagnostics/tests/functionLiterals/return/MixedReturnsFromLambda.kt index c677309489a..1cc33e72fb3 100644 --- a/compiler/testData/diagnostics/tests/functionLiterals/return/MixedReturnsFromLambda.kt +++ b/compiler/testData/diagnostics/tests/functionLiterals/return/MixedReturnsFromLambda.kt @@ -4,10 +4,11 @@ trait C: A fun test(a: C, b: B) { - (run @f{ + val x = run @f{ if (a != b) return@f a b - }): A + } + x: A } fun run(f: () -> T): T { return f() } \ No newline at end of file