Fixed inference in a simple case.
Try number lower bounds before upper bounds when computing a value.
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
public inline fun <T: Any> iterate(initialValue: T, nextFunction: (T) -> T?): Iterator<T> =
|
||||||
|
throw Exception("$initialValue $nextFunction")
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
iterate(3) { n -> if (n > 0) n - 1 else null }
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package a
|
package a
|
||||||
|
|
||||||
|
class TypeOf<T>(t: T)
|
||||||
|
|
||||||
fun <T> id(t: T): T = t
|
fun <T> id(t: T): T = t
|
||||||
|
|
||||||
fun <T> either(t1: T, <!UNUSED_PARAMETER!>t2<!>: T): T = t1
|
fun <T> either(t1: T, <!UNUSED_PARAMETER!>t2<!>: T): T = t1
|
||||||
@@ -9,62 +11,68 @@ fun other(<!UNUSED_PARAMETER!>s<!>: String) {}
|
|||||||
fun <T> otherGeneric(<!UNUSED_PARAMETER!>l<!>: List<T>) {}
|
fun <T> otherGeneric(<!UNUSED_PARAMETER!>l<!>: List<T>) {}
|
||||||
|
|
||||||
fun test() {
|
fun test() {
|
||||||
val <!UNUSED_VARIABLE!>a<!>: Byte = id(1)
|
val a: Byte = id(1)
|
||||||
|
|
||||||
val <!UNUSED_VARIABLE!>b<!>: Byte = <!TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH!>id<!>(300)
|
val b: Byte = <!TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH!>id<!>(300)
|
||||||
|
|
||||||
val <!UNUSED_VARIABLE!>c<!>: Int = <!TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH!>id<!>(9223372036854775807)
|
val c: Int = <!TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH!>id<!>(9223372036854775807)
|
||||||
|
|
||||||
val d = id(22)
|
val d = id(22)
|
||||||
d: Int
|
d: Int
|
||||||
|
|
||||||
val e = id(9223372036854775807)
|
val e = id(9223372036854775807)
|
||||||
<!TYPE_MISMATCH!>e<!>: Int
|
TypeOf(e): TypeOf<Long>
|
||||||
e: Long
|
|
||||||
|
|
||||||
val <!UNUSED_VARIABLE!>f<!>: Byte = either(1, 2)
|
val f: Byte = either(1, 2)
|
||||||
|
|
||||||
val <!UNUSED_VARIABLE!>g<!>: Byte = <!TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH!>either<!>(1, 300)
|
val g: Byte = <!TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH!>either<!>(1, 300)
|
||||||
|
|
||||||
other(<!CONSTANT_EXPECTED_TYPE_MISMATCH!>11<!>)
|
other(<!CONSTANT_EXPECTED_TYPE_MISMATCH!>11<!>)
|
||||||
|
|
||||||
<!TYPE_INFERENCE_TYPE_CONSTRUCTOR_MISMATCH!>otherGeneric<!>(<!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>)
|
<!TYPE_INFERENCE_TYPE_CONSTRUCTOR_MISMATCH!>otherGeneric<!>(<!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>)
|
||||||
|
|
||||||
val r = either(1, "")
|
val r = either(1, "")
|
||||||
<!TYPE_MISMATCH!>r<!>: Int
|
TypeOf(r): TypeOf<Any>
|
||||||
<!TYPE_MISMATCH!>r<!>: String
|
|
||||||
r: Any
|
use(a, b, c, d, e, f, g, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun use(vararg a: Any?) = a
|
||||||
|
|
||||||
trait Inv<T>
|
trait Inv<T>
|
||||||
|
|
||||||
fun <T> exactBound(<!UNUSED_PARAMETER!>t<!>: T, <!UNUSED_PARAMETER!>l<!>: Inv<T>) {}
|
fun <T> exactBound(t: T, l: Inv<T>): T = throw Exception("$t $l")
|
||||||
|
|
||||||
fun testExactBound(invS: Inv<String>, invI: Inv<Int>) {
|
fun testExactBound(invS: Inv<String>, invI: Inv<Int>, invB: Inv<Byte>) {
|
||||||
<!TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS!>exactBound<!>(1, invS)
|
<!TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS!>exactBound<!>(1, invS)
|
||||||
exactBound(1, invI)
|
exactBound(1, invI)
|
||||||
|
|
||||||
|
val b = exactBound(1, invB)
|
||||||
|
TypeOf(b): TypeOf<Byte>
|
||||||
}
|
}
|
||||||
|
|
||||||
trait Cov<out T>
|
trait Cov<out T>
|
||||||
|
|
||||||
fun <T> lowerBound(t: T, <!UNUSED_PARAMETER!>l<!>: Cov<T>) = t
|
fun <T> lowerBound(t: T, l : Cov<T>): T = throw Exception("$t $l")
|
||||||
|
|
||||||
fun testLowerBound(cov: Cov<String>) {
|
fun testLowerBound(cov: Cov<String>, covN: Cov<Number>) {
|
||||||
val r = lowerBound(1, cov)
|
val r = lowerBound(1, cov)
|
||||||
r: Any
|
TypeOf(r): TypeOf<Any>
|
||||||
|
|
||||||
|
val n = lowerBound(1, covN)
|
||||||
|
TypeOf(n): TypeOf<Number>
|
||||||
}
|
}
|
||||||
|
|
||||||
trait Contr<in T>
|
trait Contr<in T>
|
||||||
|
|
||||||
fun <T> upperBound(t: T, <!UNUSED_PARAMETER!>l<!>: Contr<T>) = t
|
fun <T> upperBound(t: T, l: Contr<T>): T = throw Exception("$t $l")
|
||||||
|
|
||||||
fun testUpperBound(contrS: Contr<String>, contrB: Contr<Byte>, contrN: Contr<Number>) {
|
fun testUpperBound(contrS: Contr<String>, contrB: Contr<Byte>, contrN: Contr<Number>) {
|
||||||
<!TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS!>upperBound<!>(1, contrS)
|
<!TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS!>upperBound<!>(1, contrS)
|
||||||
|
|
||||||
val n = upperBound(1, contrN)
|
val n = upperBound(1, contrN)
|
||||||
n: Number
|
TypeOf(n): TypeOf<Int>
|
||||||
<!TYPE_MISMATCH!>n<!>: Int
|
|
||||||
val b = upperBound(1, contrB)
|
val b = upperBound(1, contrB)
|
||||||
b: Byte
|
TypeOf(b): TypeOf<Byte>
|
||||||
<!TYPE_MISMATCH!>b<!>: Int
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3291,6 +3291,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
|
|||||||
doTest("compiler/testData/diagnostics/tests/inference/possibleCycleOnConstraints.kt");
|
doTest("compiler/testData/diagnostics/tests/inference/possibleCycleOnConstraints.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("tryNumberLowerBoundsBeforeUpperBounds.kt")
|
||||||
|
public void testTryNumberLowerBoundsBeforeUpperBounds() throws Exception {
|
||||||
|
doTest("compiler/testData/diagnostics/tests/inference/tryNumberLowerBoundsBeforeUpperBounds.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("typeConstructorMismatch.kt")
|
@TestMetadata("typeConstructorMismatch.kt")
|
||||||
public void testTypeConstructorMismatch() throws Exception {
|
public void testTypeConstructorMismatch() throws Exception {
|
||||||
doTest("compiler/testData/diagnostics/tests/inference/typeConstructorMismatch.kt");
|
doTest("compiler/testData/diagnostics/tests/inference/typeConstructorMismatch.kt");
|
||||||
|
|||||||
+11
-9
@@ -185,19 +185,10 @@ public class TypeBoundsImpl implements TypeBounds {
|
|||||||
}
|
}
|
||||||
ContainerUtil.addIfNotNull(superTypeOfLowerBounds, values);
|
ContainerUtil.addIfNotNull(superTypeOfLowerBounds, values);
|
||||||
|
|
||||||
Set<JetType> upperBounds = filterBounds(bounds, BoundKind.UPPER_BOUND, values);
|
|
||||||
JetType intersectionOfUpperBounds = TypeUtils.intersect(JetTypeChecker.INSTANCE, upperBounds);
|
|
||||||
if (!upperBounds.isEmpty() && intersectionOfUpperBounds != null) {
|
|
||||||
if (tryPossibleAnswer(intersectionOfUpperBounds)) {
|
|
||||||
return Collections.singleton(intersectionOfUpperBounds);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//todo
|
//todo
|
||||||
//fun <T> foo(t: T, consumer: Consumer<T>): T
|
//fun <T> foo(t: T, consumer: Consumer<T>): T
|
||||||
//foo(1, c: Consumer<Any>) - infer Int, not Any here
|
//foo(1, c: Consumer<Any>) - infer Int, not Any here
|
||||||
|
|
||||||
values.addAll(filterBounds(bounds, BoundKind.UPPER_BOUND));
|
|
||||||
|
|
||||||
JetType superTypeOfNumberLowerBounds = TypeUtils.commonSupertypeForNumberTypes(numberLowerBounds);
|
JetType superTypeOfNumberLowerBounds = TypeUtils.commonSupertypeForNumberTypes(numberLowerBounds);
|
||||||
if (tryPossibleAnswer(superTypeOfNumberLowerBounds)) {
|
if (tryPossibleAnswer(superTypeOfNumberLowerBounds)) {
|
||||||
return Collections.singleton(superTypeOfNumberLowerBounds);
|
return Collections.singleton(superTypeOfNumberLowerBounds);
|
||||||
@@ -211,6 +202,17 @@ public class TypeBoundsImpl implements TypeBounds {
|
|||||||
return Collections.singleton(superTypeOfAllLowerBounds);
|
return Collections.singleton(superTypeOfAllLowerBounds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Set<JetType> upperBounds = filterBounds(bounds, BoundKind.UPPER_BOUND, values);
|
||||||
|
JetType intersectionOfUpperBounds = TypeUtils.intersect(JetTypeChecker.INSTANCE, upperBounds);
|
||||||
|
if (!upperBounds.isEmpty() && intersectionOfUpperBounds != null) {
|
||||||
|
if (tryPossibleAnswer(intersectionOfUpperBounds)) {
|
||||||
|
return Collections.singleton(intersectionOfUpperBounds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
values.addAll(filterBounds(bounds, BoundKind.UPPER_BOUND));
|
||||||
|
|
||||||
return values;
|
return values;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user