From f052bc341c0d603d21f316f0fa9086424b235a4d Mon Sep 17 00:00:00 2001 From: Victor Petukhov Date: Tue, 3 Nov 2020 13:03:09 +0300 Subject: [PATCH] Don't report warning about implicitly inferred Nothing for call arguments if there is non-Nothing return type ^KT-43108 Fixed --- .../FirOldFrontendDiagnosticsTestWithStdlibGenerated.java | 5 +++++ .../kotlin/diagnostics/rendering/DefaultErrorMessages.java | 4 ++-- .../checkers/ImplicitNothingAsTypeParameterCallChecker.kt | 6 ++++++ ...ontSpreadWarningToNotReturningNothingSubResolvedAtoms.kt | 6 ++++++ ...ntSpreadWarningToNotReturningNothingSubResolvedAtoms.txt | 3 +++ .../kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java | 5 +++++ .../javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java | 5 +++++ 7 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/diagnostics/testsWithStdLib/inference/nothingType/dontSpreadWarningToNotReturningNothingSubResolvedAtoms.kt create mode 100644 compiler/testData/diagnostics/testsWithStdLib/inference/nothingType/dontSpreadWarningToNotReturningNothingSubResolvedAtoms.txt diff --git a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestWithStdlibGenerated.java b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestWithStdlibGenerated.java index ab24fdbca0a..1387a44a333 100644 --- a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestWithStdlibGenerated.java +++ b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestWithStdlibGenerated.java @@ -3357,6 +3357,11 @@ public class FirOldFrontendDiagnosticsTestWithStdlibGenerated extends AbstractFi public void testDontInferToNullableNothingInDelegates() throws Exception { runTest("compiler/testData/diagnostics/testsWithStdLib/inference/nothingType/dontInferToNullableNothingInDelegates.kt"); } + + @TestMetadata("dontSpreadWarningToNotReturningNothingSubResolvedAtoms.kt") + public void testDontSpreadWarningToNotReturningNothingSubResolvedAtoms() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/inference/nothingType/dontSpreadWarningToNotReturningNothingSubResolvedAtoms.kt"); + } } } 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 980b0b6a83a..67dcbee7f88 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -1031,8 +1031,8 @@ public class DefaultErrorMessages { MAP.put(ILLEGAL_SUSPEND_PROPERTY_ACCESS, "Suspend property ''{0}'' should be accessed only from a coroutine or suspend function", NAME); MAP.put(ILLEGAL_RESTRICTED_SUSPENDING_FUNCTION_CALL, "Restricted suspending functions can only invoke member or extension suspending functions on their restricted coroutine scope"); MAP.put(NON_MODIFIER_FORM_FOR_BUILT_IN_SUSPEND, "''suspend'' function can only be called in a form of modifier of a lambda: suspend { ... }"); - MAP.put(IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION, "Returning type parameter has been inferred to Nothing implicitly. Please, specify type arguments explicitly to hide this warning. Nothing can produce an exception at runtime."); - MAP.put(IMPLICIT_NOTHING_TYPE_ARGUMENT_AGAINST_NOT_NOTHING_EXPECTED_TYPE, "Returning type parameter has been inferred to Nothing implicitly because Nothing is more specific than specified expected type. Please specify type arguments explicitly in accordance with expected type to hide this warning. Nothing can produce an exception at runtime."); + MAP.put(IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION, "Returning type parameter has been inferred to Nothing implicitly. Please specify type arguments explicitly to hide this warning. Nothing can produce an exception at runtime."); + MAP.put(IMPLICIT_NOTHING_TYPE_ARGUMENT_AGAINST_NOT_NOTHING_EXPECTED_TYPE, "Returning type parameter has been inferred to Nothing implicitly because Nothing is more specific than specified expected type. Please specify type arguments explicitly in accordance with expected type to hide this warning. Nothing can produce an exception at runtime. See KT-36776 for more details."); MAP.put(RETURN_FOR_BUILT_IN_SUSPEND, "Using implicit label for this lambda is prohibited"); MAP.put(MODIFIER_FORM_FOR_NON_BUILT_IN_SUSPEND, "Calls having a form of ''suspend {}'' are deprecated because ''suspend'' in the context will have a meaning of a modifier. Add empty argument list to the call: ''suspend() { ... }''"); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/ImplicitNothingAsTypeParameterCallChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/ImplicitNothingAsTypeParameterCallChecker.kt index 9a07cd5b68e..c1072a92f71 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/ImplicitNothingAsTypeParameterCallChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/ImplicitNothingAsTypeParameterCallChecker.kt @@ -14,6 +14,7 @@ import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.calls.SPECIAL_FUNCTION_NAMES import org.jetbrains.kotlin.resolve.calls.callUtil.getParameterForArgument import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall +import org.jetbrains.kotlin.resolve.calls.components.stableType import org.jetbrains.kotlin.resolve.calls.model.* import org.jetbrains.kotlin.resolve.calls.tower.NewResolvedCallImpl import org.jetbrains.kotlin.resolve.calls.tower.psiExpression @@ -96,6 +97,11 @@ object ImplicitNothingAsTypeParameterCallChecker : CallChecker { } val resolvedCallAtom = resolvedAtom.getResolvedCallAtom(context.trace.bindingContext) ?: continue + val atom = resolvedAtom.atom + + if (atom is SimpleKotlinCallArgument && !atom.receiver.stableType.isNothingOrNullableNothing()) + continue + val candidateDescriptor = resolvedCallAtom.candidateDescriptor val isReturnTypeOwnTypeParameter = candidateDescriptor.typeParameters.any { it.typeConstructor == candidateDescriptor.returnType?.constructor diff --git a/compiler/testData/diagnostics/testsWithStdLib/inference/nothingType/dontSpreadWarningToNotReturningNothingSubResolvedAtoms.kt b/compiler/testData/diagnostics/testsWithStdLib/inference/nothingType/dontSpreadWarningToNotReturningNothingSubResolvedAtoms.kt new file mode 100644 index 00000000000..22d9a361f23 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/inference/nothingType/dontSpreadWarningToNotReturningNothingSubResolvedAtoms.kt @@ -0,0 +1,6 @@ +// FIR_IDENTICAL +fun get(map: Map, key: String?): Int? { + return map[key]?.let { x -> + return x + } +} diff --git a/compiler/testData/diagnostics/testsWithStdLib/inference/nothingType/dontSpreadWarningToNotReturningNothingSubResolvedAtoms.txt b/compiler/testData/diagnostics/testsWithStdLib/inference/nothingType/dontSpreadWarningToNotReturningNothingSubResolvedAtoms.txt new file mode 100644 index 00000000000..5ddd134db2b --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/inference/nothingType/dontSpreadWarningToNotReturningNothingSubResolvedAtoms.txt @@ -0,0 +1,3 @@ +package + +public fun get(/*0*/ map: kotlin.collections.Map, /*1*/ key: kotlin.String?): kotlin.Int? diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java index cb721803ced..b512c76d8af 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java @@ -3507,6 +3507,11 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW public void testDontInferToNullableNothingInDelegates() throws Exception { runTest("compiler/testData/diagnostics/testsWithStdLib/inference/nothingType/dontInferToNullableNothingInDelegates.kt"); } + + @TestMetadata("dontSpreadWarningToNotReturningNothingSubResolvedAtoms.kt") + public void testDontSpreadWarningToNotReturningNothingSubResolvedAtoms() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/inference/nothingType/dontSpreadWarningToNotReturningNothingSubResolvedAtoms.kt"); + } } } diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java index 46035bf425a..fff8026db0e 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java @@ -3507,6 +3507,11 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno public void testDontInferToNullableNothingInDelegates() throws Exception { runTest("compiler/testData/diagnostics/testsWithStdLib/inference/nothingType/dontInferToNullableNothingInDelegates.kt"); } + + @TestMetadata("dontSpreadWarningToNotReturningNothingSubResolvedAtoms.kt") + public void testDontSpreadWarningToNotReturningNothingSubResolvedAtoms() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/inference/nothingType/dontSpreadWarningToNotReturningNothingSubResolvedAtoms.kt"); + } } }