diff --git a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java index 9560d51ab1b..931be3f2f6b 100644 --- a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java @@ -24220,6 +24220,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte runTest("compiler/testData/diagnostics/tests/typeParameters/implicitNothingInReturnPosition.kt"); } + @TestMetadata("implicitNothingOfJavaCallAgainstNotNothingExpectedType.kt") + public void testImplicitNothingOfJavaCallAgainstNotNothingExpectedType() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/implicitNothingOfJavaCallAgainstNotNothingExpectedType.kt"); + } + @TestMetadata("implicitNothingOnDelegates.kt") public void testImplicitNothingOnDelegates() throws Exception { runTest("compiler/testData/diagnostics/tests/typeParameters/implicitNothingOnDelegates.kt"); 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 535ca857115..9a07cd5b68e 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 @@ -97,7 +97,9 @@ object ImplicitNothingAsTypeParameterCallChecker : CallChecker { val resolvedCallAtom = resolvedAtom.getResolvedCallAtom(context.trace.bindingContext) ?: continue val candidateDescriptor = resolvedCallAtom.candidateDescriptor - val isReturnTypeOwnTypeParameter = candidateDescriptor.typeParameters.any { it.defaultType == candidateDescriptor.returnType } + val isReturnTypeOwnTypeParameter = candidateDescriptor.typeParameters.any { + it.typeConstructor == candidateDescriptor.returnType?.constructor + } val isSpecialCall = candidateDescriptor.name in SPECIAL_FUNCTION_NAMES val hasExplicitTypeArguments = resolvedCallAtom.atom.psiKotlinCall.typeArguments.isNotEmpty() // not required diff --git a/compiler/testData/diagnostics/tests/typeParameters/implicitNothingOfJavaCallAgainstNotNothingExpectedType.fir.kt b/compiler/testData/diagnostics/tests/typeParameters/implicitNothingOfJavaCallAgainstNotNothingExpectedType.fir.kt new file mode 100644 index 00000000000..5fa4bff03fc --- /dev/null +++ b/compiler/testData/diagnostics/tests/typeParameters/implicitNothingOfJavaCallAgainstNotNothingExpectedType.fir.kt @@ -0,0 +1,40 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNCHECKED_CAST -UNUSED_EXPRESSION -UNREACHABLE_CODE + +// FILE: abc/Bar.java + +package abc; + +public class Bar { + public static T bar() { + return null; + } +} + +// FILE: main.kt + +import abc.Bar + +fun ifProblem(b: Boolean): String? { + return run { + if (b) { Bar.bar() } else null + } +} + +fun whenProblem(b: Boolean): String? { + return run { + when { + b -> Bar.bar() + else -> null + } + } +} + +fun tryProblem(): String? { + return run { + try { + Bar.bar() + } catch (e: Exception) { + null + } + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/typeParameters/implicitNothingOfJavaCallAgainstNotNothingExpectedType.kt b/compiler/testData/diagnostics/tests/typeParameters/implicitNothingOfJavaCallAgainstNotNothingExpectedType.kt new file mode 100644 index 00000000000..a3dadaf7615 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typeParameters/implicitNothingOfJavaCallAgainstNotNothingExpectedType.kt @@ -0,0 +1,40 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNCHECKED_CAST -UNUSED_EXPRESSION -UNREACHABLE_CODE + +// FILE: abc/Bar.java + +package abc; + +public class Bar { + public static T bar() { + return null; + } +} + +// FILE: main.kt + +import abc.Bar + +fun ifProblem(b: Boolean): String? { + return run { + if (b) { Bar.bar() } else null + } +} + +fun whenProblem(b: Boolean): String? { + return run { + when { + b -> Bar.bar() + else -> null + } + } +} + +fun tryProblem(): String? { + return run { + try { + Bar.bar() + } catch (e: Exception) { + null + } + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/typeParameters/implicitNothingOfJavaCallAgainstNotNothingExpectedType.txt b/compiler/testData/diagnostics/tests/typeParameters/implicitNothingOfJavaCallAgainstNotNothingExpectedType.txt new file mode 100644 index 00000000000..b64b89ab225 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typeParameters/implicitNothingOfJavaCallAgainstNotNothingExpectedType.txt @@ -0,0 +1,18 @@ +package + +public fun ifProblem(/*0*/ b: kotlin.Boolean): kotlin.String? +public fun tryProblem(): kotlin.String? +public fun whenProblem(/*0*/ b: kotlin.Boolean): kotlin.String? + +package abc { + + public open class Bar { + public constructor Bar() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + // Static members + public open fun bar(): T! + } +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 51cc0f60769..c2156196a37 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -24302,6 +24302,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali runTest("compiler/testData/diagnostics/tests/typeParameters/implicitNothingInReturnPosition.kt"); } + @TestMetadata("implicitNothingOfJavaCallAgainstNotNothingExpectedType.kt") + public void testImplicitNothingOfJavaCallAgainstNotNothingExpectedType() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/implicitNothingOfJavaCallAgainstNotNothingExpectedType.kt"); + } + @TestMetadata("implicitNothingOnDelegates.kt") public void testImplicitNothingOnDelegates() throws Exception { runTest("compiler/testData/diagnostics/tests/typeParameters/implicitNothingOnDelegates.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 37729482f3d..652c6bdcb6b 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -24222,6 +24222,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/typeParameters/implicitNothingInReturnPosition.kt"); } + @TestMetadata("implicitNothingOfJavaCallAgainstNotNothingExpectedType.kt") + public void testImplicitNothingOfJavaCallAgainstNotNothingExpectedType() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/implicitNothingOfJavaCallAgainstNotNothingExpectedType.kt"); + } + @TestMetadata("implicitNothingOnDelegates.kt") public void testImplicitNothingOnDelegates() throws Exception { runTest("compiler/testData/diagnostics/tests/typeParameters/implicitNothingOnDelegates.kt");