KT-6244 Overloads with no default parameters should take over ones with defaults

#KT-6244 Fixed
This commit is contained in:
Stanislav Erokhin
2015-08-31 22:07:37 +03:00
parent 5ded315cbb
commit d015f713cb
14 changed files with 99 additions and 16 deletions
@@ -158,7 +158,7 @@ public class OverloadingConflictResolver {
if (fIsVararg && !gIsVararg) return false;
if (!fIsVararg && !gIsVararg) {
if (fSize != gSize) return false;
if (fSize > gSize) return false;
for (int i = 0; i < fSize; i++) {
ValueParameterDescriptor fParam = fParams.get(i);
@@ -24,6 +24,7 @@ public enum ResolutionStatus {
UNKNOWN_STATUS,
UNSAFE_CALL_ERROR,
OTHER_ERROR,
ARGUMENTS_MAPPING_ERROR,
// '1.foo()' shouldn't be resolved to 'fun String.foo()'
// candidates with such error are treated specially
// (are mentioned in 'unresolved' error, if there are no other options)
@@ -31,7 +32,6 @@ public enum ResolutionStatus {
// 'a.foo()' shouldn't be resolved to package level non-extension 'fun foo()'
// candidates with such error are thrown away completely
RECEIVER_PRESENCE_ERROR,
ARGUMENTS_MAPPING_ERROR,
INCOMPLETE_TYPE_INFERENCE,
SUCCESS(true);
@@ -7,4 +7,4 @@ fun foo(s: String) {}
val x1 = ofType<() -> Unit>(::foo)
val x2 = ofType<(String) -> Unit>(::foo)
val x3 = ofType<(Int) -> Unit>(::<!NONE_APPLICABLE!>foo<!>)
val x3 = ofType<(Int) -> Unit>(<!TYPE_MISMATCH!>::foo<!>)
@@ -3,7 +3,7 @@
fun foo() {}
fun foo(s: String) {}
val x1 = ::<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!>
val x1 = ::foo
val x2: () -> Unit = ::foo
val x3: (String) -> Unit = ::foo
val x4: (Int) -> Unit = ::<!NONE_APPLICABLE!>foo<!>
val x4: (Int) -> Unit = <!TYPE_MISMATCH!>::foo<!>
@@ -1,6 +1,6 @@
package
internal val x1: [ERROR : Type for ::foo]
internal val x1: kotlin.reflect.KFunction0<kotlin.Unit>
internal val x2: () -> kotlin.Unit
internal val x3: (kotlin.String) -> kotlin.Unit
internal val x4: (kotlin.Int) -> kotlin.Unit
@@ -6,5 +6,5 @@ fun foo(s: String) {}
fun bar(f: () -> Unit) = 1
fun bar(f: (String) -> Unit) = 2
val x1 = ::<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!> as () -> Unit
val x2 = bar(::<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!> as (String) -> Unit)
val x1 = ::foo <!USELESS_CAST!>as () -> Unit<!>
val x2 = bar(<!UNCHECKED_CAST!>::foo as (String) -> Unit<!>)
@@ -7,7 +7,7 @@ class Delegate {
return 1
}
fun propertyDelegated(p: PropertyMetadata) {}
fun propertyDelegated(p: PropertyMetadata, i: Int = 1) {}
fun propertyDelegated(p: PropertyMetadata, s: String = "") {}
}
}
@@ -7,7 +7,7 @@ internal final class Delegate {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
internal final fun get(/*0*/ t: kotlin.Any?, /*1*/ p: kotlin.PropertyMetadata): kotlin.Int
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
internal final fun propertyDelegated(/*0*/ p: kotlin.PropertyMetadata): kotlin.Unit
internal final fun propertyDelegated(/*0*/ p: kotlin.PropertyMetadata, /*1*/ i: kotlin.Int = ...): kotlin.Unit
internal final fun propertyDelegated(/*0*/ p: kotlin.PropertyMetadata, /*1*/ s: kotlin.String = ...): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -29,6 +29,6 @@ import p.*
fun test(b: B?) {
if (b is C) {
b?.<!OVERLOAD_RESOLUTION_AMBIGUITY!>foo<!>(1, "")
<!DEBUG_INFO_SMARTCAST!>b<!><!UNNECESSARY_SAFE_CALL!>?.<!>foo(1, "")
}
}
@@ -0,0 +1,59 @@
// !CHECK_TYPE
// !DIAGNOSTICS: -UNUSED_EXPRESSION -UNUSED_PARAMETER -UNUSED_VARIABLE
fun simple() = 1
fun simple(a: Int = 3) = ""
fun twoDefault(a: Int = 2) = 1
fun twoDefault(a: Any = 2, b: String = "") = ""
fun <T> withGeneric(a: T) = 1
fun <T> withGeneric(a: T, b: Int = 4) = ""
fun <T> discriminateGeneric(a: T) = 1
fun discriminateGeneric(a: Int, b: String = "") = ""
fun <T: CharSequence> withDefaultGeneric(t: T, d: T? = null) = 1
fun <T: Any> withDefaultGeneric(t: T, d: T? = null, a: Int = 1) = ""
fun wrongTwoDefault(a: Any = 2) = 1
fun wrongTwoDefault(a: Int = 2, b: String = "") = ""
fun <T: Any> wrongWithDefaultGeneric(t: T, d: T? = null) = 1
fun <T: CharSequence> wrongWithDefaultGeneric(t: T, d: T? = null, a: Int = 1) = ""
fun wrong(a: Int = 1) {}
fun wrong(a: String = "", b: Int = 1) {}
fun test() {
val a = simple()
a checkType { _<Int>() }
val b = simple(1)
b checkType { _<String>() }
val c = twoDefault()
c checkType { _<Int>() }
val d = twoDefault(1)
d checkType { _<Int>() }
val e = twoDefault(1, "")
e checkType { _<String>() }
val f = withGeneric(3)
f checkType { _<Int>() }
val g = discriminateGeneric(1)
g checkType { _<String>() }
val h = withDefaultGeneric("")
h checkType { _<Int>() }
<!OVERLOAD_RESOLUTION_AMBIGUITY!>wrongTwoDefault<!>(1)
<!CANNOT_COMPLETE_RESOLVE!>wrongWithDefaultGeneric<!>("")
<!UNREACHABLE_CODE!><!OVERLOAD_RESOLUTION_AMBIGUITY!>wrong<!>(<!>null!!<!UNREACHABLE_CODE!>)<!>
}
@@ -0,0 +1,19 @@
package
internal fun </*0*/ T> discriminateGeneric(/*0*/ a: T): kotlin.Int
internal fun discriminateGeneric(/*0*/ a: kotlin.Int, /*1*/ b: kotlin.String = ...): kotlin.String
internal fun simple(): kotlin.Int
internal fun simple(/*0*/ a: kotlin.Int = ...): kotlin.String
internal fun test(): kotlin.Unit
internal fun twoDefault(/*0*/ a: kotlin.Any = ..., /*1*/ b: kotlin.String = ...): kotlin.String
internal fun twoDefault(/*0*/ a: kotlin.Int = ...): kotlin.Int
internal fun </*0*/ T : kotlin.CharSequence> withDefaultGeneric(/*0*/ t: T, /*1*/ d: T? = ...): kotlin.Int
internal fun </*0*/ T : kotlin.Any> withDefaultGeneric(/*0*/ t: T, /*1*/ d: T? = ..., /*2*/ a: kotlin.Int = ...): kotlin.String
internal fun </*0*/ T> withGeneric(/*0*/ a: T): kotlin.Int
internal fun </*0*/ T> withGeneric(/*0*/ a: T, /*1*/ b: kotlin.Int = ...): kotlin.String
internal fun wrong(/*0*/ a: kotlin.Int = ...): kotlin.Unit
internal fun wrong(/*0*/ a: kotlin.String = ..., /*1*/ b: kotlin.Int = ...): kotlin.Unit
internal fun wrongTwoDefault(/*0*/ a: kotlin.Any = ...): kotlin.Int
internal fun wrongTwoDefault(/*0*/ a: kotlin.Int = ..., /*1*/ b: kotlin.String = ...): kotlin.String
internal fun </*0*/ T : kotlin.Any> wrongWithDefaultGeneric(/*0*/ t: T, /*1*/ d: T? = ...): kotlin.Int
internal fun </*0*/ T : kotlin.CharSequence> wrongWithDefaultGeneric(/*0*/ t: T, /*1*/ d: T? = ..., /*2*/ a: kotlin.Int = ...): kotlin.String
@@ -14,8 +14,8 @@ open class B1 {
}
class A1 : B1 {
<!EXPLICIT_DELEGATION_CALL_REQUIRED!>constructor()<!>
constructor(x: Int) : <!OVERLOAD_RESOLUTION_AMBIGUITY!>super<!>()
constructor()
constructor(x: Int) : super()
}
// --------------------------
@@ -39,4 +39,4 @@ open class B3 {
class A3 : B3 {
<!EXPLICIT_DELEGATION_CALL_REQUIRED!>constructor()<!>
constructor(x: Int) : <!INVISIBLE_MEMBER!>super<!>()
}
}
@@ -9807,6 +9807,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("defaultParameters.kt")
public void testDefaultParameters() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/overload/defaultParameters.kt");
doTest(fileName);
}
@TestMetadata("EmptyArgumentListInLambda.kt")
public void testEmptyArgumentListInLambda() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/overload/EmptyArgumentListInLambda.kt");
@@ -1,4 +1,3 @@
// ERROR: Overload resolution ambiguity: public constructor C(arg1: kotlin.Int, arg2: kotlin.Int) defined in C kotlin.jvm.jvmOverloads public constructor C(arg1: kotlin.Int, arg2: kotlin.Int = ..., arg3: kotlin.Int = ...) defined in C
class C jvmOverloads constructor(arg1: Int, arg2: Int = 0, arg3: Int = 0) {
private val field: Int