[NI] Uncapture captured types in diagnostics

This commit is contained in:
Dmitriy Novozhilov
2019-04-25 18:20:45 +03:00
committed by Mikhail Zarechenskiy
parent e6deaf3315
commit f7091dd1e9
3 changed files with 48 additions and 5 deletions
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluat
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.typeUtil.unCapture
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
class DiagnosticReporterByTrackingStrategy(
@@ -212,24 +213,42 @@ class DiagnosticReporterByTrackingStrategy(
val constraintError = diagnostic as NewConstraintError
val position = constraintError.position.from
val argument = (position as? ArgumentConstraintPosition)?.argument
?: (position as? ReceiverConstraintPosition)?.argument
?: (position as? ReceiverConstraintPosition)?.argument
argument?.let {
val expression = it.psiExpression ?: return
val deparenthesized = KtPsiUtil.safeDeparenthesize(expression)
if (reportConstantTypeMismatch(constraintError, deparenthesized)) return
trace.report(Errors.TYPE_MISMATCH.on(deparenthesized, constraintError.upperKotlinType, constraintError.lowerKotlinType))
trace.report(
Errors.TYPE_MISMATCH.on(
deparenthesized,
constraintError.upperKotlinType.unCapture(),
constraintError.lowerKotlinType.unCapture()
)
)
}
(position as? ExpectedTypeConstraintPosition)?.let {
val call = it.topLevelCall.psiKotlinCall.psiCall.callElement.safeAs<KtExpression>()
reportIfNonNull(call) {
trace.report(Errors.TYPE_MISMATCH.on(it, constraintError.upperKotlinType, constraintError.lowerKotlinType))
trace.report(
Errors.TYPE_MISMATCH.on(
it,
constraintError.upperKotlinType.unCapture(),
constraintError.lowerKotlinType.unCapture()
)
)
}
}
(position as? ExplicitTypeParameterConstraintPosition)?.let {
val typeArgumentReference = (it.typeArgument as SimpleTypeArgumentImpl).typeReference
trace.report(UPPER_BOUND_VIOLATED.on(typeArgumentReference, constraintError.upperKotlinType, constraintError.lowerKotlinType))
trace.report(
UPPER_BOUND_VIOLATED.on(
typeArgumentReference,
constraintError.upperKotlinType,
constraintError.lowerKotlinType
)
)
}
}
@@ -3,7 +3,7 @@ package
public val test1: CStar /* = C<*> */
public val test2: CIn /* = C<in kotlin.Int> */
public val test3: COut /* = C<out kotlin.Int> */
public val test4: CT<kotlin.Any?> /* = C<kotlin.Any?> */
public val test4: [ERROR : Type for CT<*>()]
public val test5: CT<CT<*> /* = C<*> */> /* = C<CT<*> /* = C<*> */> */
public final class C</*0*/ T> {
@@ -26,7 +26,9 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.checker.NewCapturedType
import org.jetbrains.kotlin.types.checker.NewCapturedTypeConstructor
import org.jetbrains.kotlin.types.checker.NewTypeVariableConstructor
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import java.util.*
enum class TypeNullability {
@@ -251,4 +253,26 @@ fun KotlinType.expandIntersectionTypeIfNecessary(): Collection<KotlinType> {
} else {
types
}
}
fun KotlinType.unCapture(): KotlinType = unwrap().unCapture()
fun UnwrappedType.unCapture(): UnwrappedType = when (this) {
is AbbreviatedType -> unCapture()
is SimpleType -> unCapture()
is FlexibleType -> FlexibleTypeImpl(lowerBound.unCapture(), upperBound.unCapture())
}
fun SimpleType.unCapture(): SimpleType {
val newArguments = arguments.map { projection ->
projection.type.constructor.safeAs<NewCapturedTypeConstructor>()?.let {
it.projection
} ?: projection
}
return replace(newArguments)
}
fun AbbreviatedType.unCapture(): SimpleType {
val newType = expandedType.unCapture()
return AbbreviatedType(newType, abbreviation)
}