diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemBuilderImpl.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemBuilderImpl.kt index 8a4191125b8..cde75ecbd9e 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemBuilderImpl.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemBuilderImpl.kt @@ -37,7 +37,12 @@ import org.jetbrains.kotlin.types.typeUtil.defaultProjections import org.jetbrains.kotlin.types.typeUtil.isDefaultBound import java.util.* -class ConstraintSystemBuilderImpl : ConstraintSystem.Builder { +class ConstraintSystemBuilderImpl(private val mode: Mode = ConstraintSystemBuilderImpl.Mode.INFERENCE) : ConstraintSystem.Builder { + enum class Mode { + INFERENCE, + SPECIFICITY + } + internal data class Constraint( val kind: ConstraintKind, val subtype: KotlinType, val superType: KotlinType, val position: ConstraintPosition ) @@ -217,15 +222,13 @@ class ConstraintSystemBuilderImpl : ConstraintSystem.Builder { generateTypeParameterBound(superType, subType, constraintKind.bound.reverse(), constraintContext) return } - // if subType is nullable and superType is not nullable, unsafe call or type mismatch error will be generated later, - // but constraint system should be solved anyway - val subTypeNotNullable = if (constraintContext.initial) TypeUtils.makeNotNullable(subType) else subType - val superTypeNotNullable = if (constraintContext.initial) TypeUtils.makeNotNullable(superType) else superType + val subType2 = simplifyType(subType, constraintContext.initial) + val superType2 = simplifyType(superType, constraintContext.initial) val result = if (constraintKind == EQUAL) { - typeCheckingProcedure.equalTypes(subTypeNotNullable, superTypeNotNullable) + typeCheckingProcedure.equalTypes(subType2, superType2) } else { - typeCheckingProcedure.isSubtypeOf(subTypeNotNullable, superType) + typeCheckingProcedure.isSubtypeOf(subType2, superType) } if (!result) errors.add(newTypeInferenceOrParameterConstraintError(constraintPosition)) } @@ -237,6 +240,15 @@ class ConstraintSystemBuilderImpl : ConstraintSystem.Builder { simplifyConstraint(newSubType, superType) } + private fun simplifyType(type: KotlinType, isInitialConstraint: Boolean): KotlinType = + if (mode == Mode.SPECIFICITY || !isInitialConstraint) + type + else { + // if subType is nullable and superType is not nullable, unsafe call or type mismatch error will be generated later, + // but constraint system should be solved anyway + TypeUtils.makeNotNullable(type) + } + internal fun addBound( typeVariable: TypeVariable, constrainingType: KotlinType, @@ -393,6 +405,10 @@ class ConstraintSystemBuilderImpl : ConstraintSystem.Builder { override fun build(): ConstraintSystem { return ConstraintSystemImpl(allTypeParameterBounds, usedInBounds, errors, initialConstraints, typeVariableSubstitutors) } + + companion object { + fun forSpecificity() = ConstraintSystemBuilderImpl(Mode.SPECIFICITY) + } } internal fun createTypeForFunctionPlaceholder( diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignatureSpecificity.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignatureSpecificity.kt index 06cba25d2c4..0d0d55ac8e8 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignatureSpecificity.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignatureSpecificity.kt @@ -37,7 +37,7 @@ fun isSignatureNotLessSpecific( if (specific.valueParameterTypes.size != general.valueParameterTypes.size) return false val typeParameters = general.typeParameters - val constraintSystemBuilder: ConstraintSystem.Builder = ConstraintSystemBuilderImpl() + val constraintSystemBuilder: ConstraintSystem.Builder = ConstraintSystemBuilderImpl.forSpecificity() val typeSubstitutor = constraintSystemBuilder.registerTypeVariables(callHandle, typeParameters) var numConstraints = 0 diff --git a/compiler/testData/diagnostics/tests/duplicateJvmSignature/vararg.kt b/compiler/testData/diagnostics/tests/duplicateJvmSignature/vararg.kt index a0f9b68c76a..878fc256fa9 100644 --- a/compiler/testData/diagnostics/tests/duplicateJvmSignature/vararg.kt +++ b/compiler/testData/diagnostics/tests/duplicateJvmSignature/vararg.kt @@ -5,4 +5,4 @@ fun foo(x: Array) {} fun foo(vararg nn: Number) {} -fun foo(nn: Array) {} \ No newline at end of file +fun foo(nn: Array) {} diff --git a/compiler/testData/diagnostics/testsWithStdLib/resolve/kt10732a.kt b/compiler/testData/diagnostics/testsWithStdLib/resolve/kt10732a.kt index 51607a90819..f7ac21f1f61 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/resolve/kt10732a.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/resolve/kt10732a.kt @@ -1,5 +1,10 @@ -fun List?.foo() {} +fun List?.foo() {} -// NB Not a redeclaration -@JvmName("f1") -fun List.foo() {} +@JvmName("foo1") +fun List.foo() {} + + +fun bar(x: List) = x + +@JvmName("bar1") +fun bar(x: List?) = x diff --git a/compiler/testData/diagnostics/testsWithStdLib/resolve/kt10732a.txt b/compiler/testData/diagnostics/testsWithStdLib/resolve/kt10732a.txt index adc1a34a6e2..df0ca52a828 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/resolve/kt10732a.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/resolve/kt10732a.txt @@ -1,4 +1,6 @@ package -@kotlin.jvm.JvmName(name = "f1") public fun kotlin.collections.List.foo(): kotlin.Unit -public fun kotlin.collections.List?.foo(): kotlin.Unit +public fun bar(/*0*/ x: kotlin.collections.List): kotlin.collections.List +@kotlin.jvm.JvmName(name = "bar1") public fun bar(/*0*/ x: kotlin.collections.List?): kotlin.collections.List? +public fun kotlin.collections.List?.foo(): kotlin.Unit +@kotlin.jvm.JvmName(name = "foo1") public fun kotlin.collections.List.foo(): kotlin.Unit