diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/declarations/OperatorFunctionChecks.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/declarations/OperatorFunctionChecks.kt index 651b0c510ceac..89f04ff12571b 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/declarations/OperatorFunctionChecks.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/declarations/OperatorFunctionChecks.kt @@ -130,7 +130,7 @@ object OperatorFunctionChecks { setOf(OperatorNameConventions.INC, OperatorNameConventions.DEC), Checks.memberOrExtension, Checks.full("receiver must be a supertype of the return type") { session, function -> - val receiver = function.dispatchReceiverType ?: function.receiverParameter?.typeRef?.coneType ?: return@full false + val receiver = function.receiverParameter?.typeRef?.coneType ?: function.dispatchReceiverType ?: return@full false function.returnTypeRef.coneType.isSubtypeOf(session.typeContext, receiver) } ) diff --git a/compiler/testData/diagnostics/tests/OperatorChecks.kt b/compiler/testData/diagnostics/tests/OperatorChecks.kt index 4e6af22db4960..66981b7cc7c4a 100644 --- a/compiler/testData/diagnostics/tests/OperatorChecks.kt +++ b/compiler/testData/diagnostics/tests/OperatorChecks.kt @@ -96,6 +96,13 @@ interface Example3 { operator fun contains(n: Int) } +interface DispatchReceiverType { + interface ExtensionReceiverType + + operator fun ExtensionReceiverType.inc(): ExtensionReceiverType + operator fun ExtensionReceiverType.dec(): DispatchReceiverType +} + diff --git a/compiler/testData/diagnostics/tests/OperatorChecks.txt b/compiler/testData/diagnostics/tests/OperatorChecks.txt index c76f8b4da7eee..28159ca3866cc 100644 --- a/compiler/testData/diagnostics/tests/OperatorChecks.txt +++ b/compiler/testData/diagnostics/tests/OperatorChecks.txt @@ -96,6 +96,20 @@ public final class DelegatesWithErrors { public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } +public interface DispatchReceiverType { + 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 abstract operator fun DispatchReceiverType.ExtensionReceiverType.dec(): DispatchReceiverType + public abstract operator fun DispatchReceiverType.ExtensionReceiverType.inc(): DispatchReceiverType.ExtensionReceiverType + + public interface ExtensionReceiverType { + 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 Example { public abstract operator fun compareTo(/*0*/ other: Example): kotlin.Int public abstract operator fun component1(): kotlin.Int diff --git a/core/descriptors/src/org/jetbrains/kotlin/util/modifierChecks.kt b/core/descriptors/src/org/jetbrains/kotlin/util/modifierChecks.kt index 92fd67bf65241..9fbd448d660dc 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/util/modifierChecks.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/util/modifierChecks.kt @@ -216,7 +216,7 @@ object OperatorChecks : AbstractModifierChecks() { Checks(BINARY_OPERATION_NAMES, MemberOrExtension, SingleValueParameter, NoDefaultAndVarargsCheck), Checks(SIMPLE_UNARY_OPERATION_NAMES, MemberOrExtension, NoValueParameters), Checks(listOf(INC, DEC), MemberOrExtension) { - val receiver = dispatchReceiverParameter ?: extensionReceiverParameter + val receiver = extensionReceiverParameter ?: dispatchReceiverParameter ensure(receiver != null && ((returnType?.isSubtypeOf(receiver.type) ?: false) || incDecCheckForExpectClass(receiver))) { "receiver must be a supertype of the return type" }