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:
Denis Zharkov
2016-09-21 11:57:28 +03:00
parent 4431c6ad02
commit 6663054f4d
10 changed files with 67 additions and 23 deletions
@@ -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)
@@ -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
}
@@ -3,7 +3,7 @@
fun <T> foo(array: Array<Array<T>>): Array<Array<T>> = array
fun test(array: Array<Array<out Int>>) {
<!TYPE_INFERENCE_CANNOT_CAPTURE_TYPES!>foo<!>(array)
<!TYPE_INFERENCE_PARAMETER_CONSTRAINT_ERROR!>foo<!>(array)
val f: Array<out Array<out Int>> = <!TYPE_INFERENCE_CANNOT_CAPTURE_TYPES!>foo<!>(array)
}
val f: Array<out Array<out Int>> = <!TYPE_INFERENCE_PARAMETER_CONSTRAINT_ERROR!>foo<!>(<!TYPE_MISMATCH!>array<!>)
}
@@ -0,0 +1,18 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
class A<T>
class B<T>
fun <E> foo(b: B<in A<E>>) {}
fun <E> baz(b: B<out A<E>>) {}
// See KT-13950
fun bar(b: B<in A<out Number>>, bOut: B<out A<out Number>>, bOut2: B<out A<Number>>) {
foo(b)
foo<Number>(b)
<!TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS!>baz<!>(bOut)
baz<Number>(<!TYPE_MISMATCH!>bOut<!>)
baz(bOut2)
baz<Number>(bOut2)
}
@@ -0,0 +1,19 @@
package
public fun bar(/*0*/ b: B<in A<out kotlin.Number>>, /*1*/ bOut: B<out A<out kotlin.Number>>, /*2*/ bOut2: B<out A<kotlin.Number>>): kotlin.Unit
public fun </*0*/ E> baz(/*0*/ b: B<out A<E>>): kotlin.Unit
public fun </*0*/ E> foo(/*0*/ b: B<in A<E>>): kotlin.Unit
public final class A</*0*/ T> {
public constructor A</*0*/ T>()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class B</*0*/ T> {
public constructor B</*0*/ T>()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -4,6 +4,7 @@ class B<E>
fun <T> B<T>.foo(f: (T) -> Unit) {}
fun <T> B<T>.bar(f: (T, T) -> Unit, g: (T, T) -> Unit) {}
fun <T> B<T>.baz(f: (B<T>) -> Unit) {}
fun number(x: Number) {}
fun Number.foobar() {}
@@ -34,4 +35,8 @@ fun test(b: B<out Number>) {
b.foo(::number)
b.foo(Number::foobar)
b.baz {
b -> b checkType { _<B<out Number>>() }
}
}
@@ -3,6 +3,7 @@ package
public fun number(/*0*/ x: kotlin.Number): kotlin.Unit
public fun test(/*0*/ b: B<out kotlin.Number>): kotlin.Unit
public fun </*0*/ T> B<T>.bar(/*0*/ f: (T, T) -> kotlin.Unit, /*1*/ g: (T, T) -> kotlin.Unit): kotlin.Unit
public fun </*0*/ T> B<T>.baz(/*0*/ f: (B<T>) -> kotlin.Unit): kotlin.Unit
public fun </*0*/ T> B<T>.foo(/*0*/ f: (T) -> kotlin.Unit): kotlin.Unit
public fun kotlin.Number.foobar(): kotlin.Unit
@@ -9389,6 +9389,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("noCaptureTypeErrorForNonTopLevel.kt")
public void testNoCaptureTypeErrorForNonTopLevel() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/capturedTypes/noCaptureTypeErrorForNonTopLevel.kt");
doTest(fileName);
}
@TestMetadata("notApproximateWhenCopyDescriptors.kt")
public void testNotApproximateWhenCopyDescriptors() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/capturedTypes/notApproximateWhenCopyDescriptors.kt");
@@ -1,10 +1,8 @@
// !DIAGNOSTICS_NUMBER: 1
// !DIAGNOSTICS: TYPE_INFERENCE_CANNOT_CAPTURE_TYPES
class My<R, T>
fun <R : Any> bar(a: Array<R>): Array<R?> = null!!
fun <R, T> foo(my: My<Array<R>, T>): My<Array<R>, T> = my
fun test11(my: My<Array<out Int>, out Int>) {
foo(my)
}
fun test1(a: Array<in Int>) {
val r: Array<in Int?> = bar(a)
}
@@ -1,2 +1,2 @@
<!-- typeInferenceCannotCaptureTypes1 -->
Type inference failed: 'R' cannot capture 'out Int'. Only top-level type projections can be captured
Type inference failed: 'R' cannot capture 'in Int'. Type parameter has an upper bound 'Any' that cannot be satisfied capturing 'in' projection