Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nested inc and dec operator extensions #5348

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
)
Expand Down
7 changes: 7 additions & 0 deletions compiler/testData/diagnostics/tests/OperatorChecks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ interface Example3 {
<!INAPPLICABLE_OPERATOR_MODIFIER!>operator<!> fun contains(n: Int)
}

interface DispatchReceiverType {
interface ExtensionReceiverType

operator fun ExtensionReceiverType.inc(): ExtensionReceiverType
<!INAPPLICABLE_OPERATOR_MODIFIER!>operator<!> fun ExtensionReceiverType.dec(): DispatchReceiverType
}




Expand Down
14 changes: 14 additions & 0 deletions compiler/testData/diagnostics/tests/OperatorChecks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down