From 64543e3f52aa15989b4c8c315caba2c47f6de25d Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Fri, 9 Oct 2015 17:42:36 +0300 Subject: [PATCH] Implicit Nothing return type is now deprecated --- .../src/org/jetbrains/kotlin/diagnostics/Errors.java | 2 ++ .../kotlin/diagnostics/rendering/DefaultErrorMessages.java | 1 + .../org/jetbrains/kotlin/resolve/DeclarationsChecker.java | 4 ++++ .../testData/codegen/box/functions/coerceVoidToArray.kt | 2 +- .../testData/codegen/box/functions/coerceVoidToObject.kt | 2 +- .../controlFlowAnalysis/deadCode/commasAndWhitespaces.kt | 2 +- .../controlFlowAnalysis/deadCode/commentsInDeadCode.kt | 2 +- .../controlFlowAnalysis/deadCode/deadCallInInvokeCall.kt | 2 +- .../controlFlowAnalysis/deadCode/deadCallInReceiver.kt | 2 +- .../deadCode/deadCodeFromDifferentSources.kt | 2 +- .../controlFlowAnalysis/deadCode/deadCodeInArrayAccess.kt | 2 +- .../controlFlowAnalysis/deadCode/deadCodeInAssignment.kt | 2 +- .../deadCode/deadCodeInBinaryExpressions.kt | 2 +- .../tests/controlFlowAnalysis/deadCode/deadCodeInCalls.kt | 2 +- .../controlFlowAnalysis/deadCode/deadCodeInDeadCode.kt | 2 +- .../tests/controlFlowAnalysis/deadCode/deadCodeInIf.kt | 2 +- .../deadCode/deadCodeInInnerExpressions.kt | 2 +- .../deadCode/deadCodeInLocalDeclarations.kt | 2 +- .../tests/controlFlowAnalysis/deadCode/deadCodeInLoops.kt | 2 +- .../tests/controlFlowAnalysis/deadCode/deadCodeInReturn.kt | 2 +- .../controlFlowAnalysis/deadCode/deadCodeInUnaryExpr.kt | 2 +- compiler/testData/diagnostics/tests/implicitNothing.kt | 7 +++++++ compiler/testData/diagnostics/tests/implicitNothing.txt | 6 ++++++ compiler/testData/diagnostics/tests/inference/kt6175.kt | 2 +- .../diagnostics/tests/smartCasts/inference/kt4009.kt | 2 +- .../kotlin/checkers/JetDiagnosticsTestGenerated.java | 6 ++++++ js/js.translator/testData/_commonFiles/asserts.kt | 2 +- 27 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/implicitNothing.kt create mode 100644 compiler/testData/diagnostics/tests/implicitNothing.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java index 673578edacf..596254a9e36 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -694,6 +694,8 @@ public interface Errors { DiagnosticFactory2 INCOMPATIBLE_TYPES = DiagnosticFactory2.create(ERROR); + DiagnosticFactory0 IMPLICIT_NOTHING_RETURN_TYPE = DiagnosticFactory0.create(WARNING); + // Context tracking DiagnosticFactory1 EXPRESSION_EXPECTED = DiagnosticFactory1.create(ERROR); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java index cdf6cc038a8..24f0b9b00f4 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -525,6 +525,7 @@ public class DefaultErrorMessages { RENDER_TYPE); MAP.put(TYPE_MISMATCH_IN_CONDITION, "Condition must be of type kotlin.Boolean, but is of type {0}", RENDER_TYPE); MAP.put(INCOMPATIBLE_TYPES, "Incompatible types: {0} and {1}", RENDER_TYPE, RENDER_TYPE); + MAP.put(IMPLICIT_NOTHING_RETURN_TYPE, "''Nothing'' return type needs to be specified explicitly"); MAP.put(EXPECTED_CONDITION, "Expected condition of type kotlin.Boolean"); MAP.put(CANNOT_CHECK_FOR_ERASED, "Cannot check for instance of erased type: {0}", RENDER_TYPE); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.java index d6bca77fb1a..4a746b72333 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.java @@ -30,6 +30,7 @@ import org.jetbrains.kotlin.lexer.JetTokens; import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.types.*; import org.jetbrains.kotlin.types.checker.JetTypeChecker; +import org.jetbrains.kotlin.types.typeUtil.TypeUtilsKt; import java.util.*; @@ -641,6 +642,9 @@ public class DeclarationsChecker { if (!function.hasBody() && !hasAbstractModifier) { trace.report(NON_MEMBER_FUNCTION_NO_BODY.on(function, functionDescriptor)); } + if (TypeUtilsKt.isNothing(functionDescriptor.getReturnType()) && !function.hasDeclaredReturnType()) { + trace.report(IMPLICIT_NOTHING_RETURN_TYPE.on(function.getNameIdentifier() != null ? function.getNameIdentifier() : function)); + } checkFunctionExposedType(function, functionDescriptor); } diff --git a/compiler/testData/codegen/box/functions/coerceVoidToArray.kt b/compiler/testData/codegen/box/functions/coerceVoidToArray.kt index 4bb9e7bbad5..695a3a50187 100644 --- a/compiler/testData/codegen/box/functions/coerceVoidToArray.kt +++ b/compiler/testData/codegen/box/functions/coerceVoidToArray.kt @@ -1,6 +1,6 @@ fun a(): IntArray? = null -fun b() = throw Exception() +fun b(): Nothing = throw Exception() fun foo(): IntArray = a() ?: b() diff --git a/compiler/testData/codegen/box/functions/coerceVoidToObject.kt b/compiler/testData/codegen/box/functions/coerceVoidToObject.kt index 94ac2c84b4f..660ea0d44ec 100644 --- a/compiler/testData/codegen/box/functions/coerceVoidToObject.kt +++ b/compiler/testData/codegen/box/functions/coerceVoidToObject.kt @@ -1,6 +1,6 @@ fun a(): String? = null -fun b() = throw Exception() +fun b(): Nothing = throw Exception() fun foo(): String = a() ?: b() diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/commasAndWhitespaces.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/commasAndWhitespaces.kt index a32de3a361c..b184b4cd25a 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/commasAndWhitespaces.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/commasAndWhitespaces.kt @@ -6,7 +6,7 @@ fun testCommasAndWhitespaces() { bar( 1 , todo() , "" ) } -fun todo() = throw Exception() +fun todo(): Nothing = throw Exception() diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/commentsInDeadCode.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/commentsInDeadCode.kt index fa253f1cd4a..315b6b2b8fc 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/commentsInDeadCode.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/commentsInDeadCode.kt @@ -15,7 +15,7 @@ fun test3() { bar(11, l@(todo()/*comment*/), "") } -fun todo() = throw Exception() +fun todo(): Nothing = throw Exception() fun bar(i: Int, s: String, a: Any) {} diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCallInInvokeCall.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCallInInvokeCall.kt index 3a761cbe7d2..09a84f38169 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCallInInvokeCall.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCallInInvokeCall.kt @@ -8,4 +8,4 @@ fun testInvokeWithLambda() { todo()(1){ 42 } } -fun todo() = throw Exception() \ No newline at end of file +fun todo(): Nothing = throw Exception() \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCallInReceiver.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCallInReceiver.kt index bce9404313e..cf63b3e8dc3 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCallInReceiver.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCallInReceiver.kt @@ -8,4 +8,4 @@ fun test12() { todo()?.bar(1) } -fun todo() = throw Exception() \ No newline at end of file +fun todo(): Nothing = throw Exception() \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeFromDifferentSources.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeFromDifferentSources.kt index 66c20c12352..51a05109f54 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeFromDifferentSources.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeFromDifferentSources.kt @@ -14,7 +14,7 @@ fun test3() { bar() } -fun throwNPE() = null!! +fun throwNPE(): Nothing = null!! class A { operator fun plus(a: A): Nothing = throw Exception() diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInArrayAccess.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInArrayAccess.kt index ceb3f3934b7..3a60c07f97d 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInArrayAccess.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInArrayAccess.kt @@ -37,4 +37,4 @@ fun testArrayPlusAssign(array: Array) { array[1] += todo() } -fun todo() = throw Exception() \ No newline at end of file +fun todo(): Nothing = throw Exception() \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInAssignment.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInAssignment.kt index 24236d66ba8..c6c7d3b928e 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInAssignment.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInAssignment.kt @@ -15,4 +15,4 @@ fun testPlusAssign() { } -fun todo() = throw Exception() \ No newline at end of file +fun todo(): Nothing = throw Exception() \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInBinaryExpressions.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInBinaryExpressions.kt index a52ce2c7d3e..a1054978e8d 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInBinaryExpressions.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInBinaryExpressions.kt @@ -35,5 +35,5 @@ fun returnInBinary2(): Boolean { (return true) || (return false) } -fun todo() = throw Exception() +fun todo(): Nothing = throw Exception() fun bar() {} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInCalls.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInCalls.kt index 6bc59d6867a..0cf5b67ad13 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInCalls.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInCalls.kt @@ -10,4 +10,4 @@ fun testArgumentInVariableAsFunctionCall(f: (Any) -> Unit) { f(todo()) } -fun todo() = throw Exception() \ No newline at end of file +fun todo(): Nothing = throw Exception() \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInDeadCode.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInDeadCode.kt index 53001d49508..f52aa90ebba 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInDeadCode.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInDeadCode.kt @@ -21,4 +21,4 @@ fun unreachable4(array: Array) { } fun bar(a: Any) {} -fun todo() = throw Exception() +fun todo(): Nothing = throw Exception() diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInIf.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInIf.kt index 70eb69309bd..484e82d9d8f 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInIf.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInIf.kt @@ -8,5 +8,5 @@ fun testIf1(b: Boolean) { bar() } -fun todo() = throw Exception() +fun todo(): Nothing = throw Exception() fun bar() {} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInInnerExpressions.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInInnerExpressions.kt index 977a63b4fc7..644fcf114fd 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInInnerExpressions.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInInnerExpressions.kt @@ -10,4 +10,4 @@ fun testCompound1() { (todo() * "")[1] } -fun todo() = throw Exception() \ No newline at end of file +fun todo(): Nothing = throw Exception() \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInLocalDeclarations.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInLocalDeclarations.kt index f6da886eb8e..2ef3ce8afcd 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInLocalDeclarations.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInLocalDeclarations.kt @@ -32,5 +32,5 @@ fun testFunctionDefaultArgument() { open class Foo(i: Int) {} -fun todo() = throw Exception() +fun todo(): Nothing = throw Exception() fun bar() {} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInLoops.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInLoops.kt index aca104fd837..ecb096a9ae5 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInLoops.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInLoops.kt @@ -17,5 +17,5 @@ fun testDoWhile() { bar() } -fun todo() = throw Exception() +fun todo(): Nothing = throw Exception() fun bar() {} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInReturn.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInReturn.kt index d8574db97a2..b932880fcc6 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInReturn.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInReturn.kt @@ -2,4 +2,4 @@ fun testReturn() { return todo() } -fun todo() = throw Exception() \ No newline at end of file +fun todo(): Nothing = throw Exception() \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInUnaryExpr.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInUnaryExpr.kt index 385de223e04..060c7a12c78 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInUnaryExpr.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeInUnaryExpr.kt @@ -12,4 +12,4 @@ fun testPostfixSpecial() { todo()!! } -fun todo() = throw Exception() \ No newline at end of file +fun todo(): Nothing = throw Exception() \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/implicitNothing.kt b/compiler/testData/diagnostics/tests/implicitNothing.kt new file mode 100644 index 00000000000..1ce9bda8b32 --- /dev/null +++ b/compiler/testData/diagnostics/tests/implicitNothing.kt @@ -0,0 +1,7 @@ +fun foo() = throw Exception() + +fun bar() = null!! + +fun baz() = bar() + +fun gav(): Any = null!! diff --git a/compiler/testData/diagnostics/tests/implicitNothing.txt b/compiler/testData/diagnostics/tests/implicitNothing.txt new file mode 100644 index 00000000000..4f04b063b85 --- /dev/null +++ b/compiler/testData/diagnostics/tests/implicitNothing.txt @@ -0,0 +1,6 @@ +package + +public fun bar(): kotlin.Nothing +public fun baz(): kotlin.Nothing +public fun foo(): kotlin.Nothing +public fun gav(): kotlin.Any diff --git a/compiler/testData/diagnostics/tests/inference/kt6175.kt b/compiler/testData/diagnostics/tests/inference/kt6175.kt index 9a52a3eb0e2..0f0ae20c175 100644 --- a/compiler/testData/diagnostics/tests/inference/kt6175.kt +++ b/compiler/testData/diagnostics/tests/inference/kt6175.kt @@ -45,4 +45,4 @@ fun test4() { } } -fun fail() = throw Exception() \ No newline at end of file +fun fail(): Nothing = throw Exception() \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/smartCasts/inference/kt4009.kt b/compiler/testData/diagnostics/tests/smartCasts/inference/kt4009.kt index 0be198177d7..f1688305539 100644 --- a/compiler/testData/diagnostics/tests/smartCasts/inference/kt4009.kt +++ b/compiler/testData/diagnostics/tests/smartCasts/inference/kt4009.kt @@ -19,4 +19,4 @@ fun foo1(e: PsiElement) { } //from library -fun println(any: Any?) = throw Exception("$any") \ No newline at end of file +fun println(any: Any?): Nothing = throw Exception("$any") \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index cc64d2581dc..7563573b39f 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -265,6 +265,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("implicitNothing.kt") + public void testImplicitNothing() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/implicitNothing.kt"); + doTest(fileName); + } + @TestMetadata("IncDec.kt") public void testIncDec() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/IncDec.kt"); diff --git a/js/js.translator/testData/_commonFiles/asserts.kt b/js/js.translator/testData/_commonFiles/asserts.kt index 1456a076ae0..d24ded89de5 100644 --- a/js/js.translator/testData/_commonFiles/asserts.kt +++ b/js/js.translator/testData/_commonFiles/asserts.kt @@ -2,7 +2,7 @@ package kotlin // This file should be excluded from tests using StdLib, as these methods conflict with corresponding methods from kotlin.test // see StdLibTestBase.removeAdHocAssertions -fun fail(message: String? = null) = throw Exception(message) +fun fail(message: String? = null): Nothing = throw Exception(message) fun assertEquals(expected: T, actual: T, message: String? = null) { if (expected != actual) {