Fix overload resolution ambiguity for types intersection

There are two different forms of types intestion:
1. Type parameters with multiple bounds
2. Smart casts

The problem was that when member scope of type intersection contained
effective duplicates and that lead to overload resolution ambiguity in
strange cases like `x.hashCode()`

For first type we do effectively the same thing as when building member
scope for class extending several interfaces: group all descriptors by
both-way-overridability relation and then choose most-specific in each
group.

For smart casts we do basically the same thing but with special
treatments:
1. From all descriptors that _equal_ to most specific we choose
   the one that works without smartcast if possible (i.e. we choose first from candidates list)
2. If smart-cast value seems to be unstable we use only member scope
   of receiver type + all descriptors from smart cast possible types
   that has incompatible signature. If we'd include all of them and
   choose one as more specific, and it would lead to false
   SMART_CAST_IMPOSIBLE (see test unstableSmartCast.kt)

 #KT-3996 Fixed
 #KT-10315 Fixed
This commit is contained in:
Denis Zharkov
2015-12-15 12:55:40 +03:00
parent 8d0c3281cd
commit b4bb92d136
75 changed files with 1226 additions and 105 deletions
@@ -0,0 +1,13 @@
// !CHECK_TYPE
interface A {
fun foo(): CharSequence
}
interface B {
fun foo(): String?
}
fun <T> test(x: T) where T : B, T : A {
x.<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!>()
}
@@ -0,0 +1,17 @@
package
public fun </*0*/ T : B> test(/*0*/ x: T): kotlin.Unit where T : A
public interface A {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo(): kotlin.CharSequence
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface B {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo(): kotlin.String?
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,19 @@
// !CHECK_TYPE
// FILE: A.java
public interface A {
String foo();
}
// FILE: main.kt
interface B {
fun foo(): String?
}
interface C {
fun foo(): String
}
fun <T> test(x: T) where T : B, T : A, T : C {
x.foo().checkType { _<String>() }
}
@@ -0,0 +1,25 @@
package
public /*synthesized*/ fun A(/*0*/ function: () -> kotlin.String!): A
public fun </*0*/ T : B> test(/*0*/ x: T): kotlin.Unit where T : A, T : C
public interface A {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo(): kotlin.String!
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface B {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo(): kotlin.String?
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface C {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,13 @@
// !CHECK_TYPE
interface A {
fun foo(): CharSequence?
}
interface B {
fun foo(): String
}
fun <T> test(x: T) where T : B, T : A {
x.foo().checkType { _<String>() }
}
@@ -0,0 +1,17 @@
package
public fun </*0*/ T : B> test(/*0*/ x: T): kotlin.Unit where T : A
public interface A {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo(): kotlin.CharSequence?
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface B {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,19 @@
// !CHECK_TYPE
interface A {
val foo: Any?
}
interface C: A {
override val foo: String?
}
interface B: A {
override var foo: String
}
fun <T> test(a: T) where T : B, T : C {
a.foo = ""
a.foo = <!NULL_FOR_NONNULL_TYPE!>null<!>
a.foo.checkType { _<String>() }
}
@@ -0,0 +1,24 @@
package
public fun </*0*/ T : B> test(/*0*/ a: T): kotlin.Unit where T : C
public interface A {
public abstract val foo: kotlin.Any?
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 interface B : A {
public abstract override /*1*/ var foo: kotlin.String
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 interface C : A {
public abstract override /*1*/ val foo: kotlin.String?
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
}
@@ -0,0 +1,19 @@
// !CHECK_TYPE
interface A {
val foo: Any?
}
interface C: A {
override val foo: String
}
interface B: A {
override var foo: String?
}
fun <T> test(a: T) where T : B, T : C {
a.<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!> = ""
a.<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!> = null
a.<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!>.<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>checkType<!> { <!UNRESOLVED_REFERENCE!>_<!><String>() }
}
@@ -0,0 +1,24 @@
package
public fun </*0*/ T : B> test(/*0*/ a: T): kotlin.Unit where T : C
public interface A {
public abstract val foo: kotlin.Any?
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 interface B : A {
public abstract override /*1*/ var foo: kotlin.String?
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 interface C : A {
public abstract override /*1*/ val foo: kotlin.String
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
}
@@ -0,0 +1,13 @@
interface A {
fun foo()
}
interface C: A
interface B: A
fun <T> test(x: T) where T : C?, T : B? {
x?.foo()
if (x != null) {
<!DEBUG_INFO_SMARTCAST!>x<!>.foo()
}
}
@@ -0,0 +1,24 @@
package
public fun </*0*/ T : C?> test(/*0*/ x: T): kotlin.Unit where T : B?
public interface A {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface B : A {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract override /*1*/ /*fake_override*/ fun foo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface C : A {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract override /*1*/ /*fake_override*/ fun foo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,13 @@
// !CHECK_TYPE
interface A {
fun <T, E> foo(): E
}
interface B {
fun <Q, W> foo(): W
}
fun <T> test(x: T) where T : B, T : A {
x.foo<String, Int>().checkType { _<Int>() }
}
@@ -0,0 +1,17 @@
package
public fun </*0*/ T : B> test(/*0*/ x: T): kotlin.Unit where T : A
public interface A {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun </*0*/ T, /*1*/ E> foo(): E
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface B {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun </*0*/ Q, /*1*/ W> foo(): W
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -27,7 +27,7 @@ fun <T : CharSequence?> foo(x: T) {
if (x is String) {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
<!DEBUG_INFO_SMARTCAST!>x<!><!UNNECESSARY_SAFE_CALL!>?.<!>length
x<!UNNECESSARY_SAFE_CALL!>?.<!>length
<!DEBUG_INFO_SMARTCAST!>x<!>.bar1()
x.bar2()