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
@@ -1,7 +1,7 @@
open class A {
open fun foo() = "FAIL"
fun bar() = if (this is C) <!DEBUG_INFO_IMPLICIT_RECEIVER_SMARTCAST!>foo<!>() else "FAIL"
fun bar() = if (this is C) foo() else "FAIL"
}
open class B : A()
@@ -0,0 +1,13 @@
interface A {
fun <T, E> foo(): E
}
interface B {
fun <Q, W> foo(): Q
}
fun test(c: Any) {
if (c is B && c is A) {
c.<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!><String, Int>()
}
}
@@ -0,0 +1,17 @@
package
public fun test(/*0*/ c: kotlin.Any): kotlin.Unit
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(): Q
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,15 @@
// !CHECK_TYPE
interface A {
fun foo(): CharSequence
}
interface B {
fun foo(): String?
}
fun test(c: Any) {
if (c is B && c is A) {
c.<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!>()
}
}
@@ -0,0 +1,17 @@
package
public fun test(/*0*/ c: kotlin.Any): kotlin.Unit
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,52 @@
// !CHECK_TYPE
// FILE: A.java
public interface A {
String foo();
}
// FILE: main.kt
interface B {
fun foo(): String?
}
interface C {
fun foo(): String
}
fun foo(x: Any?) {
if (x is A && x is B) {
<!DEBUG_INFO_SMARTCAST!>x<!>.foo().checkType { <!TYPE_MISMATCH!>_<!><String>() }
<!DEBUG_INFO_SMARTCAST!>x<!>.foo().checkType { _<String?>() }
}
if (x is B && x is A) {
<!DEBUG_INFO_SMARTCAST!>x<!>.foo().checkType { <!TYPE_MISMATCH!>_<!><String>() }
<!DEBUG_INFO_SMARTCAST!>x<!>.foo().checkType { _<String?>() }
}
if (x is A && x is C) {
<!DEBUG_INFO_SMARTCAST!>x<!>.foo().checkType { _<String>() }
<!DEBUG_INFO_SMARTCAST!>x<!>.foo().checkType { <!TYPE_MISMATCH!>_<!><String?>() }
}
if (x is C && x is A) {
<!DEBUG_INFO_SMARTCAST!>x<!>.foo().checkType { _<String>() }
<!DEBUG_INFO_SMARTCAST!>x<!>.foo().checkType { <!TYPE_MISMATCH!>_<!><String?>() }
}
if (x is A && x is B && x is C) {
<!DEBUG_INFO_SMARTCAST!>x<!>.foo().checkType { _<String>() }
<!DEBUG_INFO_SMARTCAST!>x<!>.foo().checkType { <!TYPE_MISMATCH!>_<!><String?>() }
}
if (x is B && x is A && x is C) {
<!DEBUG_INFO_SMARTCAST!>x<!>.foo().checkType { _<String>() }
<!DEBUG_INFO_SMARTCAST!>x<!>.foo().checkType { <!TYPE_MISMATCH!>_<!><String?>() }
}
if (x is B && x is C && x is A) {
<!DEBUG_INFO_SMARTCAST!>x<!>.foo().checkType { _<String>() }
<!DEBUG_INFO_SMARTCAST!>x<!>.foo().checkType { <!TYPE_MISMATCH!>_<!><String?>() }
}
}
@@ -0,0 +1,25 @@
package
public /*synthesized*/ fun A(/*0*/ function: () -> kotlin.String!): A
public fun foo(/*0*/ x: kotlin.Any?): kotlin.Unit
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,19 @@
// !CHECK_TYPE
interface Common {
fun foo(): CharSequence?
}
interface A : Common {
override fun foo(): CharSequence
}
interface B : Common {
override fun foo(): String
}
fun test(c: Common) {
if (c is B && c is A) {
<!DEBUG_INFO_SMARTCAST!>c<!>.foo().checkType { _<String>() }
}
}
@@ -0,0 +1,24 @@
package
public fun test(/*0*/ c: Common): kotlin.Unit
public interface A : Common {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract override /*1*/ 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 : Common {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract override /*1*/ 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 Common {
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
}
@@ -0,0 +1,15 @@
// !CHECK_TYPE
interface A {
fun foo(): CharSequence?
}
interface B {
fun foo(): String
}
fun test(c: Any) {
if (c is B && c is A) {
<!DEBUG_INFO_SMARTCAST!>c<!>.foo().checkType { _<String>() }
}
}
@@ -0,0 +1,17 @@
package
public fun test(/*0*/ c: kotlin.Any): kotlin.Unit
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,21 @@
// !CHECK_TYPE
interface A {
val foo: Any?
}
interface C: A {
override val foo: String?
}
interface B: A {
override var foo: String
}
fun test(a: A) {
if (a is B && a is C) {
<!DEBUG_INFO_SMARTCAST!>a<!>.foo = ""
<!DEBUG_INFO_SMARTCAST!>a<!>.foo = <!NULL_FOR_NONNULL_TYPE!>null<!>
<!DEBUG_INFO_SMARTCAST!>a<!>.foo.checkType { _<String>() }
}
}
@@ -0,0 +1,24 @@
package
public fun test(/*0*/ a: A): kotlin.Unit
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,20 @@
// !CHECK_TYPE
interface A {
val foo: Any?
}
interface C: A {
override val foo: String
}
interface B: A {
override var foo: String?
}
fun test(a: A) {
if (a is B && a is C) {
a.<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!> = ""
a.<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!> = null
a.<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!>
}
}
@@ -0,0 +1,24 @@
package
public fun test(/*0*/ a: A): kotlin.Unit
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,16 @@
// !CHECK_TYPE
interface A {
fun foo(): CharSequence?
}
interface B : A {
override fun foo(): String
}
fun test(a: A) {
if (a is B) {
<!DEBUG_INFO_SMARTCAST!>a<!>.foo()
<!DEBUG_INFO_SMARTCAST!>a<!>.foo().checkType { _<String>() }
}
}
@@ -0,0 +1,17 @@
package
public fun test(/*0*/ a: A): kotlin.Unit
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 : A {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract override /*1*/ 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,12 @@
interface A {
fun foo()
}
interface C: A
interface B: A
fun test(c: C) {
if (c is B) {
c.foo() // OVERLOAD_RESOLUTION_AMBIGUITY: B.foo() and C.foo()
}
}
@@ -0,0 +1,24 @@
package
public fun test(/*0*/ c: C): kotlin.Unit
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,37 @@
// !CHECK_TYPE
interface A {
fun foo(): CharSequence?
fun baz(x: Any) {}
}
interface B {
fun foo(): String
fun baz(x: Int): String =""
fun baz(x: Int, y: Int) {}
fun foobar(): CharSequence?
}
interface C {
fun foo(): String
fun baz(x: Int): String =""
fun baz(x: Int, y: Int) {}
fun foobar(): String
}
var x: A = null!!
fun test() {
x.foo().checkType { _<CharSequence?>() }
if (x is B && x is C) {
x.foo().checkType { _<CharSequence?>() }
x.baz("")
x.baz(1).checkType { _<Unit>() }
<!SMARTCAST_IMPOSSIBLE!>x<!>.baz(1, 2)
<!SMARTCAST_IMPOSSIBLE!>x<!>.foobar().checkType { _<String>() }
}
}
@@ -0,0 +1,32 @@
package
public var x: A
public fun test(): kotlin.Unit
public interface A {
public open fun baz(/*0*/ x: kotlin.Any): kotlin.Unit
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 fun baz(/*0*/ x: kotlin.Int): kotlin.String
public open fun baz(/*0*/ x: kotlin.Int, /*1*/ y: kotlin.Int): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo(): kotlin.String
public abstract fun foobar(): kotlin.CharSequence?
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 fun baz(/*0*/ x: kotlin.Int): kotlin.String
public open fun baz(/*0*/ x: kotlin.Int, /*1*/ y: kotlin.Int): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo(): kotlin.String
public abstract fun foobar(): 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,15 @@
// !CHECK_TYPE
interface A {
fun <T, E> foo(): E
}
interface B {
fun <Q, W> foo(): W
}
fun test(c: Any) {
if (c is B && c is A) {
<!DEBUG_INFO_SMARTCAST!>c<!>.foo<String, Int>().checkType { _<Int>() }
}
}
@@ -0,0 +1,17 @@
package
public fun test(/*0*/ c: kotlin.Any): kotlin.Unit
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
}
@@ -0,0 +1,15 @@
// !CHECK_TYPE
interface A {
fun <T, E> foo(): E
}
interface B : A {
override fun <Q, W> foo(): W
}
fun test(a: A) {
if (a is B) {
a.foo<String, Int>().checkType { _<Int>() }
}
}
@@ -0,0 +1,17 @@
package
public fun test(/*0*/ a: A): kotlin.Unit
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 : A {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract override /*1*/ 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
}
+2 -2
View File
@@ -4,5 +4,5 @@ fun test(c : Class<*>) {
val sc = <!UNCHECKED_CAST!>c as Class<String><!>
// No ambiguous overload
c.getAnnotations();
sc.getAnnotations();
}
sc.getAnnotations();
}
@@ -12,7 +12,7 @@ fun foo(): Int {
override fun run() = Unit
}
// Unnecessary but not important smart cast
<!DEBUG_INFO_SMARTCAST!>k<!>.run()
k.run()
return <!DEBUG_INFO_SMARTCAST!>c<!> + d
}
else return -1
@@ -14,10 +14,10 @@ fun foo(): Boolean {
var v: A
v = B()
// No smart cast needed, but not a problem if ever
if (<!DEBUG_INFO_SMARTCAST!>v<!>.ok()) {
if (v.ok()) {
v = C()
}
// No smart cast needed, and no smart cast possible!
// We cannot choose between B and C
return v.ok()
}
}