[NI] Don't fail on captured type that contains type variable
This commit is contained in:
+17
-13
@@ -1,17 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2017 JetBrains s.r.o.
|
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||||
*
|
* that can be found in the license/LICENSE.txt file.
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.kotlin.resolve.calls.inference.components
|
package org.jetbrains.kotlin.resolve.calls.inference.components
|
||||||
@@ -83,10 +72,25 @@ abstract class TypeCheckerContextForConstraintSystem : TypeCheckerContext(errorT
|
|||||||
if (subType.anyBound(this::isMyTypeVariable)) {
|
if (subType.anyBound(this::isMyTypeVariable)) {
|
||||||
return simplifyUpperConstraint(subType, superType) && (answer ?: true)
|
return simplifyUpperConstraint(subType, superType) && (answer ?: true)
|
||||||
} else {
|
} else {
|
||||||
|
extractTypeVariableForSubtype(subType)?.let {
|
||||||
|
return simplifyUpperConstraint(it, superType) && (answer ?: true)
|
||||||
|
}
|
||||||
|
|
||||||
return simplifyConstraintForPossibleIntersectionSubType(subType, superType) ?: answer
|
return simplifyConstraintForPossibleIntersectionSubType(subType, superType) ?: answer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// extract type variable only from type like Captured(out T)
|
||||||
|
private fun extractTypeVariableForSubtype(type: UnwrappedType): UnwrappedType? {
|
||||||
|
if (type !is NewCapturedType) return null
|
||||||
|
|
||||||
|
val projection = type.constructor.projection
|
||||||
|
return if (projection.projectionKind == Variance.OUT_VARIANCE)
|
||||||
|
projection.type.takeIf { it is SimpleType && isMyTypeVariable(it) }?.unwrap()
|
||||||
|
else
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Foo <: T -- leave as is
|
* Foo <: T -- leave as is
|
||||||
*
|
*
|
||||||
|
|||||||
compiler/testData/diagnostics/tests/inference/capturedTypes/capturedTypeWithTypeVariableSubtyping.kt
Vendored
+22
@@ -0,0 +1,22 @@
|
|||||||
|
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||||
|
|
||||||
|
fun takeCovArray(a: Array<out Number>) {}
|
||||||
|
fun takeInArray(a: Array<in Number>) {}
|
||||||
|
|
||||||
|
fun <T> foo(): Array<out T> = TODO()
|
||||||
|
fun <T> bar(): Array<in T> = TODO()
|
||||||
|
|
||||||
|
fun <X> id(x: X): X = x
|
||||||
|
fun <Z> arrayInId(z: Array<in Z>): Array<in Z> = z
|
||||||
|
fun <Z> arrayOutId(z: Array<out Z>): Array<out Z> = z
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
takeCovArray(foo())
|
||||||
|
takeInArray(bar())
|
||||||
|
|
||||||
|
takeCovArray(id(foo()))
|
||||||
|
takeInArray(id(bar()))
|
||||||
|
|
||||||
|
takeCovArray(arrayOutId(foo()))
|
||||||
|
takeInArray(arrayInId(bar()))
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
package
|
||||||
|
|
||||||
|
public fun </*0*/ Z> arrayInId(/*0*/ z: kotlin.Array<in Z>): kotlin.Array<in Z>
|
||||||
|
public fun </*0*/ Z> arrayOutId(/*0*/ z: kotlin.Array<out Z>): kotlin.Array<out Z>
|
||||||
|
public fun </*0*/ T> bar(): kotlin.Array<in T>
|
||||||
|
public fun </*0*/ T> foo(): kotlin.Array<out T>
|
||||||
|
public fun </*0*/ X> id(/*0*/ x: X): X
|
||||||
|
public fun takeCovArray(/*0*/ a: kotlin.Array<out kotlin.Number>): kotlin.Unit
|
||||||
|
public fun takeInArray(/*0*/ a: kotlin.Array<in kotlin.Number>): kotlin.Unit
|
||||||
|
public fun test(): kotlin.Unit
|
||||||
@@ -18,7 +18,7 @@ fun test1(int: Int, any: Any) {
|
|||||||
|
|
||||||
val a2 : MyList<out Any> = getMyList(int)
|
val a2 : MyList<out Any> = getMyList(int)
|
||||||
|
|
||||||
val a3 : MyList<out Any> = <!NI;TYPE_MISMATCH!>getMyListToReadFrom(int)<!>
|
val a3 : MyList<out Any> = getMyListToReadFrom(int)
|
||||||
|
|
||||||
val a4 : MyList<in Int> = getMyList(any)
|
val a4 : MyList<in Int> = getMyList(any)
|
||||||
|
|
||||||
|
|||||||
@@ -18,5 +18,5 @@ fun <T> fout(<!UNUSED_PARAMETER!>expression<!> : T) : Out<<!REDUNDANT_PROJECTION
|
|||||||
|
|
||||||
fun fooout() : Out<Point> {
|
fun fooout() : Out<Point> {
|
||||||
val p = Point();
|
val p = Point();
|
||||||
return <!NI;TYPE_MISMATCH!>fout<Point>(p)<!>;
|
return fout<Point>(p);
|
||||||
}
|
}
|
||||||
@@ -10660,6 +10660,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("capturedTypeWithTypeVariableSubtyping.kt")
|
||||||
|
public void testCapturedTypeWithTypeVariableSubtyping() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/capturedTypes/capturedTypeWithTypeVariableSubtyping.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("expectedTypeMismatchWithInVariance.kt")
|
@TestMetadata("expectedTypeMismatchWithInVariance.kt")
|
||||||
public void testExpectedTypeMismatchWithInVariance() throws Exception {
|
public void testExpectedTypeMismatchWithInVariance() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/capturedTypes/expectedTypeMismatchWithInVariance.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/capturedTypes/expectedTypeMismatchWithInVariance.kt");
|
||||||
|
|||||||
Generated
+6
@@ -10660,6 +10660,12 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("capturedTypeWithTypeVariableSubtyping.kt")
|
||||||
|
public void testCapturedTypeWithTypeVariableSubtyping() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/capturedTypes/capturedTypeWithTypeVariableSubtyping.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("expectedTypeMismatchWithInVariance.kt")
|
@TestMetadata("expectedTypeMismatchWithInVariance.kt")
|
||||||
public void testExpectedTypeMismatchWithInVariance() throws Exception {
|
public void testExpectedTypeMismatchWithInVariance() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/capturedTypes/expectedTypeMismatchWithInVariance.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/capturedTypes/expectedTypeMismatchWithInVariance.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user