From bee1762b0e4a90ebbe54cd4ea3c5a5bc95281105 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Fri, 10 Jun 2016 13:44:55 +0300 Subject: [PATCH] 'Nothing' as function return type and property type can't be abbreviated. --- .../src/org/jetbrains/kotlin/diagnostics/Errors.java | 2 ++ .../kotlin/diagnostics/rendering/DefaultErrorMessages.java | 2 ++ .../org/jetbrains/kotlin/resolve/DeclarationsChecker.kt | 7 ++++++- .../returnTypeNothingShouldBeSpecifiedExplicitly.kt | 5 +++++ .../returnTypeNothingShouldBeSpecifiedExplicitly.txt | 6 ++++++ .../kotlin/checkers/DiagnosticsTestGenerated.java | 6 ++++++ 6 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/tests/typealias/returnTypeNothingShouldBeSpecifiedExplicitly.kt create mode 100644 compiler/testData/diagnostics/tests/typealias/returnTypeNothingShouldBeSpecifiedExplicitly.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java index 87c8ff084c0..49b4bde62c2 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -766,6 +766,8 @@ public interface Errors { DiagnosticFactory0 IMPLICIT_NOTHING_RETURN_TYPE = DiagnosticFactory0.create(ERROR); DiagnosticFactory0 IMPLICIT_NOTHING_PROPERTY_TYPE = DiagnosticFactory0.create(ERROR); + DiagnosticFactory0 ABBREVIATED_NOTHING_RETURN_TYPE = DiagnosticFactory0.create(ERROR); + DiagnosticFactory0 ABBREVIATED_NOTHING_PROPERTY_TYPE = DiagnosticFactory0.create(ERROR); DiagnosticFactory1 IMPLICIT_INTERSECTION_TYPE = DiagnosticFactory1.create(ERROR); // Context tracking 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 fbfd679791c..d8416c2e6be 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -563,6 +563,8 @@ public class DefaultErrorMessages { 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(IMPLICIT_NOTHING_PROPERTY_TYPE, "'Nothing' property type needs to be specified explicitly"); + MAP.put(ABBREVIATED_NOTHING_RETURN_TYPE, "'Nothing' return type can't be specified with type alias"); + MAP.put(ABBREVIATED_NOTHING_PROPERTY_TYPE, "'Nothing' property type can't be specified with type alias"); MAP.put(IMPLICIT_INTERSECTION_TYPE, "Inferred type {0} is an intersection, please specify the required type explicitly", RENDER_TYPE); MAP.put(EXPECTED_CONDITION, "Expected condition of type Boolean"); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt index 6818f998399..43ccdb77335 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt @@ -718,8 +718,8 @@ class DeclarationsChecker( private fun checkImplicitCallableType(declaration: KtCallableDeclaration, descriptor: CallableDescriptor) { descriptor.returnType?.let { + val target = declaration.nameIdentifier ?: declaration if (declaration.typeReference == null) { - val target = declaration.nameIdentifier ?: declaration if (it.isNothing() && !declaration.hasModifier(KtTokens.OVERRIDE_KEYWORD)) { trace.report( (if (declaration is KtProperty) IMPLICIT_NOTHING_PROPERTY_TYPE else IMPLICIT_NOTHING_RETURN_TYPE).on(target) @@ -729,6 +729,11 @@ class DeclarationsChecker( trace.report(IMPLICIT_INTERSECTION_TYPE.on(target, it)) } } + else if (it.isNothing() && it is AbbreviatedType) { + trace.report( + (if (declaration is KtProperty) ABBREVIATED_NOTHING_PROPERTY_TYPE else ABBREVIATED_NOTHING_RETURN_TYPE).on(target) + ) + } } } diff --git a/compiler/testData/diagnostics/tests/typealias/returnTypeNothingShouldBeSpecifiedExplicitly.kt b/compiler/testData/diagnostics/tests/typealias/returnTypeNothingShouldBeSpecifiedExplicitly.kt new file mode 100644 index 00000000000..bcb9109876f --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/returnTypeNothingShouldBeSpecifiedExplicitly.kt @@ -0,0 +1,5 @@ +typealias N = Nothing + +fun testFun(): N = null!! +val testVal: N = null!! +val testValWithGetter: N get() = null!! \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/typealias/returnTypeNothingShouldBeSpecifiedExplicitly.txt b/compiler/testData/diagnostics/tests/typealias/returnTypeNothingShouldBeSpecifiedExplicitly.txt new file mode 100644 index 00000000000..421623ebbea --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/returnTypeNothingShouldBeSpecifiedExplicitly.txt @@ -0,0 +1,6 @@ +package + +public typealias N = kotlin.Nothing +public val testVal: N [= kotlin.Nothing] +public val testValWithGetter: N [= kotlin.Nothing] +public fun testFun(): N [= kotlin.Nothing] diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index da28c3b54df..73785271f9d 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -19605,6 +19605,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("returnTypeNothingShouldBeSpecifiedExplicitly.kt") + public void testReturnTypeNothingShouldBeSpecifiedExplicitly() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/returnTypeNothingShouldBeSpecifiedExplicitly.kt"); + doTest(fileName); + } + @TestMetadata("simpleTypeAlias.kt") public void testSimpleTypeAlias() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/simpleTypeAlias.kt");