[NI] Use alternative to intersection type in public declarations

The new inference uses inferred intersection types normally, unlike the old inference.
However, intersection types in public declarations are approximated to supertype, which
potentially may give a less presice type, then it would be with the OI.
For non-related T1, T2 the NI approximates {T1 & T2} to Any in public declarations,
and if the OI was inferring T1 instead of the intersection type, it may lead to
less precise declaration type and related errors.

The solution is to remember an alternative for an intersection type when present.
Before approximation the alternative replaces the intersection type.

^KT-36249 Fixed
This commit is contained in:
Pavel Kirpichenkov
2020-02-20 19:55:13 +03:00
parent 4038c758ca
commit dc42b20ae3
67 changed files with 2120 additions and 30 deletions
@@ -0,0 +1,44 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun test() {
<!DEBUG_INFO_EXPRESSION_TYPE("Inv<Bound1>")!>testInv()<!>
<!DEBUG_INFO_EXPRESSION_TYPE("In<Bound1>")!>testIn()<!>
<!DEBUG_INFO_EXPRESSION_TYPE("Out<Bound1>")!>testOut()<!>
<!DEBUG_INFO_EXPRESSION_TYPE("BiParam<Bound1, Inv<*>>")!>testStarProjection()<!>
<!DEBUG_INFO_EXPRESSION_TYPE("[ERROR : Error function type]")!>testErrorType()<!>
<!DEBUG_INFO_EXPRESSION_TYPE("Inv<in Bound1>")!>testInProjection()<!>
<!DEBUG_INFO_EXPRESSION_TYPE("Inv<out Bound1>")!>testOutProjection()<!>
<!DEBUG_INFO_EXPRESSION_TYPE("Inv<Inv<Inv<Bound1>>>")!>testDeeplyNested()<!>
}
interface Bound1
interface Bound2
object First : Bound1, Bound2
object Second : Bound1, Bound2
class Inv<T>(val prop: T)
class Out<out O>(val prop: O)
class In<in I>(arg: I)
class BiParam<F, S>(first: F, second: S)
fun <S : Bound1> intersect(vararg elements: S): S = TODO()
fun makeStarProjection(): Inv<*> = TODO()
fun <I> makeInProjection(arg: I): Inv<in I> = TODO()
fun <O> makeOutProjection(arg: O): Inv<out O> = TODO()
fun testInv() = Inv(intersect(First, Second))
fun testOut() = Out(intersect(First, Second))
fun testIn() = In(intersect(First, Second))
fun testInProjection() = makeInProjection(intersect(First, Second))
fun testOutProjection() = makeOutProjection(intersect(First, Second))
fun testDeeplyNested() = Inv(Inv(Inv(intersect(First, Second))))
fun testStarProjection() = BiParam(
intersect(First, Second),
makeStarProjection()
)
fun testErrorType() = BiParam(
intersect(First, Second),
<!UNRESOLVED_REFERENCE!>unresolved<!>
)