[NI] Add checking @NotNull parameters for candidates

This commit is contained in:
Dmitriy Novozhilov
2019-05-24 18:55:21 +03:00
parent f20ec3e0a6
commit 910177ab17
12 changed files with 46 additions and 11 deletions
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.model.ArgumentConstraintPosi
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintPosition
import org.jetbrains.kotlin.resolve.calls.inference.model.ReceiverConstraintPosition
import org.jetbrains.kotlin.resolve.calls.model.*
import org.jetbrains.kotlin.types.NotNullTypeVariable
import org.jetbrains.kotlin.types.UnwrappedType
import org.jetbrains.kotlin.types.checker.captureFromExpression
import org.jetbrains.kotlin.types.checker.hasSupertypeWithGivenTypeConstructor
@@ -81,6 +82,20 @@ private fun checkExpressionArgument(
val expectedNullableType = expectedType.makeNullableAsSpecified(true)
val position = if (isReceiver) ReceiverConstraintPosition(expressionArgument) else ArgumentConstraintPosition(expressionArgument)
// Used only for arguments with @NotNull annotation
if (expectedType is NotNullTypeVariable) {
var expectedTypeIsNull: Boolean = false
csBuilder.runTransaction {
addNotNullUpperConstraint(argumentType, position)
expectedTypeIsNull = hasContradiction
false
}
if (expectedTypeIsNull) {
diagnosticsHolder.addDiagnostic(ArgumentTypeMismatchDiagnostic(expectedType, argumentType, expressionArgument))
}
}
if (expressionArgument.isSafeCall) {
if (!csBuilder.addSubtypeConstraintIfCompatible(argumentType, expectedNullableType, position)) {
diagnosticsHolder.addDiagnosticIfNotNull(
@@ -40,6 +40,8 @@ interface ConstraintSystemOperation {
fun isPostponedTypeVariable(typeVariable: TypeVariableMarker): Boolean
fun getProperSuperTypeConstructors(type: KotlinTypeMarker): List<TypeConstructorMarker>
fun addNotNullUpperConstraint(type: KotlinTypeMarker, position: ConstraintPosition)
}
interface ConstraintSystemBuilder : ConstraintSystemOperation {
@@ -302,4 +302,8 @@ class NewConstraintSystemImpl(
return constraints.any { (it.kind == ConstraintKind.UPPER || it.kind == ConstraintKind.EQUALITY) && it.type.isUnit() }
}
override fun addNotNullUpperConstraint(type: KotlinTypeMarker, position: ConstraintPosition) {
addSubtypeConstraint(type, anyType(), position)
}
}