[NI] Add check for non-null argument type in arguments check

#KT-31461 Fixed
This commit is contained in:
Dmitriy Novozhilov
2019-05-20 11:47:36 +03:00
parent b323298b0e
commit b4c8c79931
11 changed files with 53 additions and 10 deletions
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.types.UnwrappedType
import org.jetbrains.kotlin.types.checker.captureFromExpression
import org.jetbrains.kotlin.types.checker.hasSupertypeWithGivenTypeConstructor
import org.jetbrains.kotlin.types.lowerIfFlexible
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
import org.jetbrains.kotlin.types.typeUtil.supertypes
import org.jetbrains.kotlin.types.upperIfFlexible
@@ -66,6 +67,14 @@ private fun checkExpressionArgument(
return UnstableSmartCast(expressionArgument, unstableType)
}
}
if (argumentType.isMarkedNullable) {
if (csBuilder.addSubtypeConstraintIfCompatible(argumentType, actualExpectedType, position)) return null
if (csBuilder.addSubtypeConstraintIfCompatible(argumentType.makeNotNullable(), actualExpectedType, position)) {
return ArgumentTypeMismatchDiagnostic(actualExpectedType, argumentType, expressionArgument)
}
}
csBuilder.addSubtypeConstraint(argumentType, actualExpectedType, position)
return null
}
@@ -185,4 +185,14 @@ class NonApplicableCallForBuilderInferenceDiagnostic(val kotlinCall: KotlinCall)
override fun report(reporter: DiagnosticReporter) {
reporter.onCall(this)
}
}
class ArgumentTypeMismatchDiagnostic(
val expectedType: UnwrappedType,
val actualType: UnwrappedType,
val expressionArgument: ExpressionKotlinCallArgument
) : KotlinCallDiagnostic(MAY_THROW_RUNTIME_ERROR) {
override fun report(reporter: DiagnosticReporter) {
reporter.onCallArgument(expressionArgument, this)
}
}