From 3e9ef56c31e22bf128c449f6d724c72914a96ec2 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 8 Apr 2021 15:37:07 +0300 Subject: [PATCH] Forbid using ext.property type parameter also for delegate ext.receiver --- .../jetbrains/kotlin/resolve/DeclarationsChecker.kt | 4 +++- .../useTypeParameterOfExtensionProperty.fir.kt | 6 ++++++ .../useTypeParameterOfExtensionProperty.kt | 6 ++++++ .../useTypeParameterOfExtensionProperty.txt | 11 +++++++++++ ...seTypeParameterOfExtensionProperty_Disabled.fir.kt | 6 ++++++ .../useTypeParameterOfExtensionProperty_Disabled.kt | 6 ++++++ .../useTypeParameterOfExtensionProperty_Disabled.txt | 11 +++++++++++ 7 files changed, 49 insertions(+), 1 deletion(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt index 45a7ae1f6c1..7d456a96f12 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt @@ -715,8 +715,10 @@ class DeclarationsChecker( } else if (property.receiverTypeReference != null) { val delegatedPropertyResolvedCall = trace.get(DELEGATED_PROPERTY_RESOLVED_CALL, propertyDescriptor.getter) val dispatchReceiverType = delegatedPropertyResolvedCall?.dispatchReceiver?.type + val extensionReceiverType = delegatedPropertyResolvedCall?.extensionReceiver?.type val usedParameter = propertyDescriptor.typeParameters.find { typeParameter -> - dispatchReceiverType?.contains { it.constructor == typeParameter.typeConstructor } == true + dispatchReceiverType?.contains { it.constructor == typeParameter.typeConstructor } == true || + extensionReceiverType?.contains { it.constructor == typeParameter.typeConstructor } == true } if (usedParameter != null) { if (languageVersionSettings.supportsFeature(LanguageFeature.ForbidUsingExtensionPropertyTypeParameterInDelegate)) { diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty.fir.kt b/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty.fir.kt index 4b296090b0a..8785b1a6276 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty.fir.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty.fir.kt @@ -8,6 +8,12 @@ class Delegate { var List.foo by Delegate() +class Wrapper(val v: T? = null) + +operator fun Wrapper.getValue(thisRef: Any?, kp: Any?): T = v!! + +val List.bar by Wrapper() + fun useString(s: String) {} fun main(listInt: List, listStr: List) { diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty.kt b/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty.kt index cfd2c6d418c..bae208c3a97 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty.kt @@ -8,6 +8,12 @@ class Delegate { var List.foo by Delegate() +class Wrapper(val v: T? = null) + +operator fun Wrapper.getValue(thisRef: Any?, kp: Any?): T = v!! + +val List.bar by Wrapper() + fun useString(s: String) {} fun main(listInt: List, listStr: List) { diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty.txt b/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty.txt index 4823e379b3b..4272f00fd0f 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty.txt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty.txt @@ -1,8 +1,10 @@ package +public val kotlin.collections.List.bar: T public var kotlin.collections.List.foo: T public fun main(/*0*/ listInt: kotlin.collections.List, /*1*/ listStr: kotlin.collections.List): kotlin.Unit public fun useString(/*0*/ s: kotlin.String): kotlin.Unit +public operator fun Wrapper.getValue(/*0*/ thisRef: kotlin.Any?, /*1*/ kp: kotlin.Any?): T public final class Delegate { public constructor Delegate() @@ -13,3 +15,12 @@ public final class Delegate { public final operator fun setValue(/*0*/ thisRef: kotlin.Any?, /*1*/ kp: kotlin.Any?, /*2*/ newValue: T): kotlin.Unit public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } + +public final class Wrapper { + public constructor Wrapper(/*0*/ v: T? = ...) + public final val v: T? + 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 +} + diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty_Disabled.fir.kt b/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty_Disabled.fir.kt index bd4b67e76cc..a9b4d2ceb8c 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty_Disabled.fir.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty_Disabled.fir.kt @@ -8,6 +8,12 @@ class Delegate { var List.foo by Delegate() +class Wrapper(val v: T? = null) + +operator fun Wrapper.getValue(thisRef: Any?, kp: Any?): T = v!! + +val List.bar by Wrapper() + fun useString(s: String) {} fun main(listInt: List, listStr: List) { diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty_Disabled.kt b/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty_Disabled.kt index 6fe5cfbf79f..1d6573a6562 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty_Disabled.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty_Disabled.kt @@ -8,6 +8,12 @@ class Delegate { var List.foo by Delegate() +class Wrapper(val v: T? = null) + +operator fun Wrapper.getValue(thisRef: Any?, kp: Any?): T = v!! + +val List.bar by Wrapper() + fun useString(s: String) {} fun main(listInt: List, listStr: List) { diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty_Disabled.txt b/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty_Disabled.txt index 4823e379b3b..4272f00fd0f 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty_Disabled.txt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty_Disabled.txt @@ -1,8 +1,10 @@ package +public val kotlin.collections.List.bar: T public var kotlin.collections.List.foo: T public fun main(/*0*/ listInt: kotlin.collections.List, /*1*/ listStr: kotlin.collections.List): kotlin.Unit public fun useString(/*0*/ s: kotlin.String): kotlin.Unit +public operator fun Wrapper.getValue(/*0*/ thisRef: kotlin.Any?, /*1*/ kp: kotlin.Any?): T public final class Delegate { public constructor Delegate() @@ -13,3 +15,12 @@ public final class Delegate { public final operator fun setValue(/*0*/ thisRef: kotlin.Any?, /*1*/ kp: kotlin.Any?, /*2*/ newValue: T): kotlin.Unit public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } + +public final class Wrapper { + public constructor Wrapper(/*0*/ v: T? = ...) + public final val v: T? + 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 +} +