From d015f713cbf14fdbf76ed917e3b92a631e956f85 Mon Sep 17 00:00:00 2001 From: Stanislav Erokhin Date: Mon, 31 Aug 2015 22:07:37 +0300 Subject: [PATCH] KT-6244 Overloads with no default parameters should take over ones with defaults #KT-6244 Fixed --- .../results/OverloadingConflictResolver.java | 2 +- .../calls/results/ResolutionStatus.java | 2 +- .../resolve/byGenericArgType.kt | 2 +- .../callableReference/resolve/byValType.kt | 4 +- .../callableReference/resolve/byValType.txt | 2 +- .../tests/callableReference/resolve/withAs.kt | 4 +- .../propertyDelegatedAmbiguity.kt | 4 +- .../propertyDelegatedAmbiguity.txt | 2 +- .../differentNumberOfParams.kt | 2 +- .../tests/overload/defaultParameters.kt | 59 +++++++++++++++++++ .../tests/overload/defaultParameters.txt | 19 ++++++ .../errorsOnEmptyDelegationCall.kt | 6 +- .../checkers/JetDiagnosticsTestGenerated.java | 6 ++ .../constructors/parameterModification.kt | 1 - 14 files changed, 99 insertions(+), 16 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/overload/defaultParameters.kt create mode 100644 compiler/testData/diagnostics/tests/overload/defaultParameters.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/OverloadingConflictResolver.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/OverloadingConflictResolver.java index 4059d7179fd..e7687b2da5f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/OverloadingConflictResolver.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/OverloadingConflictResolver.java @@ -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); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/ResolutionStatus.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/ResolutionStatus.java index b4c5ec3b981..ad0568fb3b4 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/ResolutionStatus.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/ResolutionStatus.java @@ -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); diff --git a/compiler/testData/diagnostics/tests/callableReference/resolve/byGenericArgType.kt b/compiler/testData/diagnostics/tests/callableReference/resolve/byGenericArgType.kt index 9c17a48e44b..022437e419e 100644 --- a/compiler/testData/diagnostics/tests/callableReference/resolve/byGenericArgType.kt +++ b/compiler/testData/diagnostics/tests/callableReference/resolve/byGenericArgType.kt @@ -7,4 +7,4 @@ fun foo(s: String) {} val x1 = ofType<() -> Unit>(::foo) val x2 = ofType<(String) -> Unit>(::foo) -val x3 = ofType<(Int) -> Unit>(::foo) \ No newline at end of file +val x3 = ofType<(Int) -> Unit>(::foo) \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/callableReference/resolve/byValType.kt b/compiler/testData/diagnostics/tests/callableReference/resolve/byValType.kt index 4aa6dac8397..c9e5829da61 100644 --- a/compiler/testData/diagnostics/tests/callableReference/resolve/byValType.kt +++ b/compiler/testData/diagnostics/tests/callableReference/resolve/byValType.kt @@ -3,7 +3,7 @@ fun foo() {} fun foo(s: String) {} -val x1 = ::foo +val x1 = ::foo val x2: () -> Unit = ::foo val x3: (String) -> Unit = ::foo -val x4: (Int) -> Unit = ::foo \ No newline at end of file +val x4: (Int) -> Unit = ::foo \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/callableReference/resolve/byValType.txt b/compiler/testData/diagnostics/tests/callableReference/resolve/byValType.txt index 2ddd646a152..1e1fb5490ae 100644 --- a/compiler/testData/diagnostics/tests/callableReference/resolve/byValType.txt +++ b/compiler/testData/diagnostics/tests/callableReference/resolve/byValType.txt @@ -1,6 +1,6 @@ package -internal val x1: [ERROR : Type for ::foo] +internal val x1: kotlin.reflect.KFunction0 internal val x2: () -> kotlin.Unit internal val x3: (kotlin.String) -> kotlin.Unit internal val x4: (kotlin.Int) -> kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/callableReference/resolve/withAs.kt b/compiler/testData/diagnostics/tests/callableReference/resolve/withAs.kt index ff34f30b9ea..cafaae15a3f 100644 --- a/compiler/testData/diagnostics/tests/callableReference/resolve/withAs.kt +++ b/compiler/testData/diagnostics/tests/callableReference/resolve/withAs.kt @@ -6,5 +6,5 @@ fun foo(s: String) {} fun bar(f: () -> Unit) = 1 fun bar(f: (String) -> Unit) = 2 -val x1 = ::foo as () -> Unit -val x2 = bar(::foo as (String) -> Unit) \ No newline at end of file +val x1 = ::foo as () -> Unit +val x2 = bar(::foo as (String) -> Unit) \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/propertyDelegatedAmbiguity.kt b/compiler/testData/diagnostics/tests/delegatedProperty/propertyDelegatedAmbiguity.kt index 9c4bb73ae49..a14adb28f24 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/propertyDelegatedAmbiguity.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/propertyDelegatedAmbiguity.kt @@ -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 = "") {} -} +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/propertyDelegatedAmbiguity.txt b/compiler/testData/diagnostics/tests/delegatedProperty/propertyDelegatedAmbiguity.txt index 9b407046b11..253a97eb3e6 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/propertyDelegatedAmbiguity.txt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/propertyDelegatedAmbiguity.txt @@ -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 } diff --git a/compiler/testData/diagnostics/tests/multimodule/duplicateMethod/differentNumberOfParams.kt b/compiler/testData/diagnostics/tests/multimodule/duplicateMethod/differentNumberOfParams.kt index 10ed61a7b4a..630250fd3bb 100644 --- a/compiler/testData/diagnostics/tests/multimodule/duplicateMethod/differentNumberOfParams.kt +++ b/compiler/testData/diagnostics/tests/multimodule/duplicateMethod/differentNumberOfParams.kt @@ -29,6 +29,6 @@ import p.* fun test(b: B?) { if (b is C) { - b?.foo(1, "") + b?.foo(1, "") } } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/overload/defaultParameters.kt b/compiler/testData/diagnostics/tests/overload/defaultParameters.kt new file mode 100644 index 00000000000..2fead22681b --- /dev/null +++ b/compiler/testData/diagnostics/tests/overload/defaultParameters.kt @@ -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 withGeneric(a: T) = 1 +fun withGeneric(a: T, b: Int = 4) = "" + +fun discriminateGeneric(a: T) = 1 +fun discriminateGeneric(a: Int, b: String = "") = "" + +fun withDefaultGeneric(t: T, d: T? = null) = 1 +fun withDefaultGeneric(t: T, d: T? = null, a: Int = 1) = "" + +fun wrongTwoDefault(a: Any = 2) = 1 +fun wrongTwoDefault(a: Int = 2, b: String = "") = "" + +fun wrongWithDefaultGeneric(t: T, d: T? = null) = 1 +fun 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 { _() } + + val b = simple(1) + b checkType { _() } + + val c = twoDefault() + c checkType { _() } + + val d = twoDefault(1) + d checkType { _() } + + val e = twoDefault(1, "") + e checkType { _() } + + val f = withGeneric(3) + f checkType { _() } + + val g = discriminateGeneric(1) + g checkType { _() } + + val h = withDefaultGeneric("") + h checkType { _() } + + + wrongTwoDefault(1) + + wrongWithDefaultGeneric("") + + wrong(null!!) +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/overload/defaultParameters.txt b/compiler/testData/diagnostics/tests/overload/defaultParameters.txt new file mode 100644 index 00000000000..a09f647110a --- /dev/null +++ b/compiler/testData/diagnostics/tests/overload/defaultParameters.txt @@ -0,0 +1,19 @@ +package + +internal fun 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 withDefaultGeneric(/*0*/ t: T, /*1*/ d: T? = ...): kotlin.Int +internal fun withDefaultGeneric(/*0*/ t: T, /*1*/ d: T? = ..., /*2*/ a: kotlin.Int = ...): kotlin.String +internal fun withGeneric(/*0*/ a: T): kotlin.Int +internal fun 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 wrongWithDefaultGeneric(/*0*/ t: T, /*1*/ d: T? = ...): kotlin.Int +internal fun wrongWithDefaultGeneric(/*0*/ t: T, /*1*/ d: T? = ..., /*2*/ a: kotlin.Int = ...): kotlin.String diff --git a/compiler/testData/diagnostics/tests/secondaryConstructors/errorsOnEmptyDelegationCall.kt b/compiler/testData/diagnostics/tests/secondaryConstructors/errorsOnEmptyDelegationCall.kt index cad517281d4..3736ef1a2a5 100644 --- a/compiler/testData/diagnostics/tests/secondaryConstructors/errorsOnEmptyDelegationCall.kt +++ b/compiler/testData/diagnostics/tests/secondaryConstructors/errorsOnEmptyDelegationCall.kt @@ -14,8 +14,8 @@ open class B1 { } class A1 : B1 { - constructor() - constructor(x: Int) : super() + constructor() + constructor(x: Int) : super() } // -------------------------- @@ -39,4 +39,4 @@ open class B3 { class A3 : B3 { constructor() constructor(x: Int) : super() -} +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index 7d4bbd924a5..40d0c0666b4 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -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"); diff --git a/j2k/testData/fileOrElement/constructors/parameterModification.kt b/j2k/testData/fileOrElement/constructors/parameterModification.kt index 913e836e98c..5a9b4e63e90 100644 --- a/j2k/testData/fileOrElement/constructors/parameterModification.kt +++ b/j2k/testData/fileOrElement/constructors/parameterModification.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