Fix diagnostic inferred type calculation for null in some cases

#KT-36222 fixed
This commit is contained in:
Ilya Chernikov
2020-02-19 17:15:37 +01:00
parent 997debdd42
commit 2727507d18
7 changed files with 45 additions and 1 deletions
@@ -18079,6 +18079,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/regressions/kt35668.kt");
}
@TestMetadata("kt36222.kt")
public void testKt36222() throws Exception {
runTest("compiler/testData/diagnostics/tests/regressions/kt36222.kt");
}
@TestMetadata("kt3647.kt")
public void testKt3647() throws Exception {
runTest("compiler/testData/diagnostics/tests/regressions/kt3647.kt");
@@ -33,6 +33,8 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.module
import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.expressions.ControlStructureTypingUtils
import org.jetbrains.kotlin.types.typeUtil.isNullableNothing
import org.jetbrains.kotlin.types.typeUtil.makeNullable
import org.jetbrains.kotlin.utils.addToStdlib.cast
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@@ -333,12 +335,15 @@ class DiagnosticReporterByTrackingStrategy(
(position as? ExpectedTypeConstraintPosition)?.let {
val call = it.topLevelCall.psiKotlinCall.psiCall.callElement.safeAs<KtExpression>()
val inferredType =
if (!constraintError.lowerKotlinType.isNullableNothing()) constraintError.lowerKotlinType
else constraintError.upperKotlinType.makeNullable()
reportIfNonNull(call) {
trace.report(
Errors.TYPE_MISMATCH.on(
it,
constraintError.upperKotlinType,
constraintError.lowerKotlinType
inferredType
)
)
}
@@ -0,0 +1,9 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun foo(f: () -> String) {}
fun <K> select(x: K, y: K): K = x
fun test() {
foo { select("non-null", null) } // inferred String? but String is expected
}
@@ -0,0 +1,10 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun foo(f: () -> String) {}
fun <K> select(x: K, y: K): K = x
fun test() {
foo { <!TYPE_MISMATCH!>select("non-null", null)<!> } // inferred String? but String is expected
}
@@ -0,0 +1,5 @@
package
public fun foo(/*0*/ f: () -> kotlin.String): kotlin.Unit
public fun </*0*/ K> select(/*0*/ x: K, /*1*/ y: K): K
public fun test(): kotlin.Unit
@@ -18091,6 +18091,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali
runTest("compiler/testData/diagnostics/tests/regressions/kt35668.kt");
}
@TestMetadata("kt36222.kt")
public void testKt36222() throws Exception {
runTest("compiler/testData/diagnostics/tests/regressions/kt36222.kt");
}
@TestMetadata("kt3647.kt")
public void testKt3647() throws Exception {
runTest("compiler/testData/diagnostics/tests/regressions/kt3647.kt");
@@ -18081,6 +18081,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/regressions/kt35668.kt");
}
@TestMetadata("kt36222.kt")
public void testKt36222() throws Exception {
runTest("compiler/testData/diagnostics/tests/regressions/kt36222.kt");
}
@TestMetadata("kt3647.kt")
public void testKt3647() throws Exception {
runTest("compiler/testData/diagnostics/tests/regressions/kt3647.kt");