No "unreachable code" errors when an illegal return expression is the last in a lambda
This commit is contained in:
+5
-1
@@ -482,6 +482,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
|
|||||||
JetExpression returnedExpression = expression.getReturnedExpression();
|
JetExpression returnedExpression = expression.getReturnedExpression();
|
||||||
|
|
||||||
JetType expectedType = TypeUtils.NO_EXPECTED_TYPE;
|
JetType expectedType = TypeUtils.NO_EXPECTED_TYPE;
|
||||||
|
JetType resultType = KotlinBuiltIns.getInstance().getNothingType();
|
||||||
JetDeclaration parentDeclaration = PsiTreeUtil.getParentOfType(expression, JetDeclaration.class);
|
JetDeclaration parentDeclaration = PsiTreeUtil.getParentOfType(expression, JetDeclaration.class);
|
||||||
|
|
||||||
if (parentDeclaration instanceof JetParameter) {
|
if (parentDeclaration instanceof JetParameter) {
|
||||||
@@ -503,6 +504,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
|
|||||||
} while (containingFunction instanceof JetFunctionLiteral);
|
} while (containingFunction instanceof JetFunctionLiteral);
|
||||||
// Unqualified, in a function literal
|
// Unqualified, in a function literal
|
||||||
context.trace.report(RETURN_NOT_ALLOWED.on(expression));
|
context.trace.report(RETURN_NOT_ALLOWED.on(expression));
|
||||||
|
resultType = ErrorUtils.createErrorType("Return not allowed");
|
||||||
}
|
}
|
||||||
if (containingFunctionDescriptor != null) {
|
if (containingFunctionDescriptor != null) {
|
||||||
expectedType = DescriptorUtils.getFunctionExpectedReturnType(containingFunctionDescriptor, (JetElement) containingFunction);
|
expectedType = DescriptorUtils.getFunctionExpectedReturnType(containingFunctionDescriptor, (JetElement) containingFunction);
|
||||||
@@ -511,6 +513,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
|
|||||||
else {
|
else {
|
||||||
// Outside a function
|
// Outside a function
|
||||||
context.trace.report(RETURN_NOT_ALLOWED.on(expression));
|
context.trace.report(RETURN_NOT_ALLOWED.on(expression));
|
||||||
|
resultType = ErrorUtils.createErrorType("Return not allowed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (element != null) {
|
else if (element != null) {
|
||||||
@@ -520,6 +523,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
|
|||||||
if (functionDescriptor != containingFunctionDescriptor) {
|
if (functionDescriptor != containingFunctionDescriptor) {
|
||||||
// Qualified, non-local
|
// Qualified, non-local
|
||||||
context.trace.report(RETURN_NOT_ALLOWED.on(expression));
|
context.trace.report(RETURN_NOT_ALLOWED.on(expression));
|
||||||
|
resultType = ErrorUtils.createErrorType("Return not allowed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -534,7 +538,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
|
|||||||
context.trace.report(RETURN_TYPE_MISMATCH.on(expression, expectedType));
|
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
|
@Override
|
||||||
|
|||||||
+18
@@ -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")
|
@TestMetadata("compiler/testData/diagnostics/tests/functionLiterals")
|
||||||
|
@InnerTestClasses({FunctionLiterals.Return.class})
|
||||||
public static class FunctionLiterals extends AbstractDiagnosticsTestWithEagerResolve {
|
public static class FunctionLiterals extends AbstractDiagnosticsTestWithEagerResolve {
|
||||||
public void testAllFilesPresentInFunctionLiterals() throws Exception {
|
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);
|
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");
|
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")
|
@TestMetadata("compiler/testData/diagnostics/tests/generics")
|
||||||
@@ -5204,7 +5223,7 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
|
|||||||
suite.addTestSuite(Deparenthesize.class);
|
suite.addTestSuite(Deparenthesize.class);
|
||||||
suite.addTest(Enum.innerSuite());
|
suite.addTest(Enum.innerSuite());
|
||||||
suite.addTestSuite(Extensions.class);
|
suite.addTestSuite(Extensions.class);
|
||||||
suite.addTestSuite(FunctionLiterals.class);
|
suite.addTest(FunctionLiterals.innerSuite());
|
||||||
suite.addTestSuite(Generics.class);
|
suite.addTestSuite(Generics.class);
|
||||||
suite.addTest(IncompleteCode.innerSuite());
|
suite.addTest(IncompleteCode.innerSuite());
|
||||||
suite.addTest(Inference.innerSuite());
|
suite.addTest(Inference.innerSuite());
|
||||||
|
|||||||
Reference in New Issue
Block a user