Fix false positive "Cannot capture" error reporting
There is no need to report an error in case of non-top-level capture conversion, just don't add relevant capture type in the system instead If system can be solved successfully without captured type, then it's just fine (see KT-13950) In case of contradiction TYPE_INFERENCE_PARAMETER_CONSTRAINT_ERROR is reported #KT-13950 Fixed
This commit is contained in:
@@ -341,16 +341,16 @@ object Renderers {
|
||||
}
|
||||
|
||||
val typeParameter = typeVariableWithCapturedConstraint.originalTypeParameter
|
||||
|
||||
val explanation: String
|
||||
val upperBound = TypeIntersector.getUpperBoundsAsType(typeParameter)
|
||||
if (!KotlinBuiltIns.isNullableAny(upperBound) && capturedTypeConstructor.typeProjection.projectionKind == Variance.IN_VARIANCE) {
|
||||
explanation = "Type parameter has an upper bound '" + result.typeRenderer.render(upperBound, RenderingContext.of(upperBound)) + "'" +
|
||||
" that cannot be satisfied capturing 'in' projection"
|
||||
}
|
||||
else {
|
||||
explanation = "Only top-level type projections can be captured"
|
||||
|
||||
assert(!KotlinBuiltIns.isNullableAny(upperBound) && capturedTypeConstructor.typeProjection.projectionKind == Variance.IN_VARIANCE) {
|
||||
"There is the only reason to report TYPE_INFERENCE_CANNOT_CAPTURE_TYPES"
|
||||
}
|
||||
|
||||
val explanation =
|
||||
"Type parameter has an upper bound '" + result.typeRenderer.render(upperBound, RenderingContext.of(upperBound)) + "'" +
|
||||
" that cannot be satisfied capturing 'in' projection"
|
||||
|
||||
result.text(newText().normal(
|
||||
"'" + typeParameter.name + "'" +
|
||||
" cannot capture " +
|
||||
@@ -454,4 +454,4 @@ object Renderers {
|
||||
}.asRenderer()
|
||||
}
|
||||
|
||||
fun DescriptorRenderer.asRenderer() = SmartDescriptorRenderer(this)
|
||||
fun DescriptorRenderer.asRenderer() = SmartDescriptorRenderer(this)
|
||||
|
||||
+1
-4
@@ -152,13 +152,10 @@ open class ConstraintSystemBuilderImpl(private val mode: Mode = ConstraintSystem
|
||||
}
|
||||
|
||||
override fun capture(type: KotlinType, typeProjection: TypeProjection): Boolean {
|
||||
if (isMyTypeVariable(typeProjection.type)) return false
|
||||
if (isMyTypeVariable(typeProjection.type) || depth > 0) return false
|
||||
val myTypeVariable = getMyTypeVariable(type)
|
||||
|
||||
if (myTypeVariable != null && constraintPosition.isParameter()) {
|
||||
if (depth > 0) {
|
||||
errors.add(CannotCapture(constraintPosition, myTypeVariable))
|
||||
}
|
||||
generateTypeParameterCaptureConstraint(myTypeVariable, typeProjection, newConstraintContext, type.isMarkedNullable)
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user