No "unreachable code" errors when an illegal return expression is the last in a lambda

This commit is contained in:
Andrey Breslav
2013-08-16 18:09:01 +04:00
parent 34df845778
commit 9f90fd0938
3 changed files with 43 additions and 2 deletions
@@ -482,6 +482,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
JetExpression returnedExpression = expression.getReturnedExpression();
JetType expectedType = TypeUtils.NO_EXPECTED_TYPE;
JetType resultType = KotlinBuiltIns.getInstance().getNothingType();
JetDeclaration parentDeclaration = PsiTreeUtil.getParentOfType(expression, JetDeclaration.class);
if (parentDeclaration instanceof JetParameter) {
@@ -503,6 +504,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
} while (containingFunction instanceof JetFunctionLiteral);
// Unqualified, in a function literal
context.trace.report(RETURN_NOT_ALLOWED.on(expression));
resultType = ErrorUtils.createErrorType("Return not allowed");
}
if (containingFunctionDescriptor != null) {
expectedType = DescriptorUtils.getFunctionExpectedReturnType(containingFunctionDescriptor, (JetElement) containingFunction);
@@ -511,6 +513,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
else {
// Outside a function
context.trace.report(RETURN_NOT_ALLOWED.on(expression));
resultType = ErrorUtils.createErrorType("Return not allowed");
}
}
else if (element != null) {
@@ -520,6 +523,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
if (functionDescriptor != containingFunctionDescriptor) {
// Qualified, non-local
context.trace.report(RETURN_NOT_ALLOWED.on(expression));
resultType = ErrorUtils.createErrorType("Return not allowed");
}
}
else {
@@ -534,7 +538,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
context.trace.report(RETURN_TYPE_MISMATCH.on(expression, expectedType));
}
}
return DataFlowUtils.checkType(KotlinBuiltIns.getInstance().getNothingType(), expression, context, context.dataFlowInfo);
return DataFlowUtils.checkType(resultType, expression, context, context.dataFlowInfo);
}
@Override
@@ -0,0 +1,18 @@
fun test() {
run {<!RETURN_NOT_ALLOWED!>return<!>}
run {}
}
fun test2() {
run {<!RETURN_NOT_ALLOWED!>return@test2<!>}
run {}
}
fun test3() {
fun test4() {
run {<!RETURN_NOT_ALLOWED!>return@test3<!>}
run {}
}
}
fun run<T>(f: () -> T): T { return f() }
@@ -2390,6 +2390,7 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
}
@TestMetadata("compiler/testData/diagnostics/tests/functionLiterals")
@InnerTestClasses({FunctionLiterals.Return.class})
public static class FunctionLiterals extends AbstractDiagnosticsTestWithEagerResolve {
public void testAllFilesPresentInFunctionLiterals() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/diagnostics/tests/functionLiterals"), Pattern.compile("^(.+)\\.kt$"), true);
@@ -2425,6 +2426,24 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
doTest("compiler/testData/diagnostics/tests/functionLiterals/unusedLiteral.kt");
}
@TestMetadata("compiler/testData/diagnostics/tests/functionLiterals/return")
public static class Return extends AbstractDiagnosticsTestWithEagerResolve {
public void testAllFilesPresentInReturn() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/diagnostics/tests/functionLiterals/return"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("ForbiddenNonLocalReturnNoType.kt")
public void testForbiddenNonLocalReturnNoType() throws Exception {
doTest("compiler/testData/diagnostics/tests/functionLiterals/return/ForbiddenNonLocalReturnNoType.kt");
}
}
public static Test innerSuite() {
TestSuite suite = new TestSuite("FunctionLiterals");
suite.addTestSuite(FunctionLiterals.class);
suite.addTestSuite(Return.class);
return suite;
}
}
@TestMetadata("compiler/testData/diagnostics/tests/generics")
@@ -5204,7 +5223,7 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
suite.addTestSuite(Deparenthesize.class);
suite.addTest(Enum.innerSuite());
suite.addTestSuite(Extensions.class);
suite.addTestSuite(FunctionLiterals.class);
suite.addTest(FunctionLiterals.innerSuite());
suite.addTestSuite(Generics.class);
suite.addTest(IncompleteCode.innerSuite());
suite.addTest(Inference.innerSuite());