[FE] Report ambiguity when label can refer to multiple receivers
This commit is contained in:
committed by
TeamCityServer
parent
fd7d000138
commit
ac27fda965
+6
@@ -10612,6 +10612,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
|
||||
runTest("compiler/testData/diagnostics/tests/extensions/contextReceivers/insideDeclaration.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("labelsFromClassNameForbidden.kt")
|
||||
public void testLabelsFromClassNameForbidden() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/extensions/contextReceivers/labelsFromClassNameForbidden.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("lazy.kt")
|
||||
public void testLazy() throws Exception {
|
||||
|
||||
+1
-1
@@ -437,7 +437,7 @@ fun getDeclarationLabels(lambdaOrFun: PsiElement?, descriptor: DeclarationDescri
|
||||
val result = HashSet<String>()
|
||||
|
||||
if (lambdaOrFun != null) {
|
||||
val label = LabelResolver.getLabelNamesIfAny(lambdaOrFun, addContextReceiverNames = false)
|
||||
val label = LabelResolver.getLabelNamesIfAny(lambdaOrFun, addClassNameLabels = false)
|
||||
if (label.isNotEmpty()) {
|
||||
result.add(label.single().asString())
|
||||
}
|
||||
|
||||
+6
@@ -10612,6 +10612,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
|
||||
runTest("compiler/testData/diagnostics/tests/extensions/contextReceivers/insideDeclaration.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("labelsFromClassNameForbidden.kt")
|
||||
public void testLabelsFromClassNameForbidden() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/extensions/contextReceivers/labelsFromClassNameForbidden.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("lazy.kt")
|
||||
public void testLazy() throws Exception {
|
||||
|
||||
+6
@@ -10612,6 +10612,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
|
||||
runTest("compiler/testData/diagnostics/tests/extensions/contextReceivers/insideDeclaration.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("labelsFromClassNameForbidden.kt")
|
||||
public void testLabelsFromClassNameForbidden() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/extensions/contextReceivers/labelsFromClassNameForbidden.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("lazy.kt")
|
||||
public void testLazy() throws Exception {
|
||||
|
||||
@@ -228,32 +228,36 @@ class FunctionDescriptorResolver(
|
||||
}
|
||||
}
|
||||
|
||||
val receiverToLabelMap = linkedMapOf<ReceiverParameterDescriptor, String>()
|
||||
val extensionReceiver = receiverType?.let {
|
||||
val splitter = AnnotationSplitter(storageManager, receiverType.annotations, EnumSet.of(AnnotationUseSiteTarget.RECEIVER))
|
||||
val splitter = AnnotationSplitter(storageManager, it.annotations, EnumSet.of(AnnotationUseSiteTarget.RECEIVER))
|
||||
DescriptorFactory.createExtensionReceiverParameterForCallable(
|
||||
functionDescriptor, it, splitter.getAnnotationsForTarget(AnnotationUseSiteTarget.RECEIVER)
|
||||
)
|
||||
}?.apply {
|
||||
val extensionReceiverName = receiverTypeRef?.nameForReceiverLabel()
|
||||
if (extensionReceiverName != null) {
|
||||
receiverToLabelMap[this] = extensionReceiverName
|
||||
}
|
||||
}
|
||||
|
||||
val contextReceiverDescriptors = contextReceiverTypes.mapNotNull { type ->
|
||||
val splitter = AnnotationSplitter(storageManager, type.annotations, EnumSet.of(AnnotationUseSiteTarget.RECEIVER))
|
||||
val contextReceiverDescriptors = contextReceiverTypes.mapNotNull {
|
||||
val splitter = AnnotationSplitter(storageManager, it.annotations, EnumSet.of(AnnotationUseSiteTarget.RECEIVER))
|
||||
DescriptorFactory.createContextReceiverParameterForCallable(
|
||||
functionDescriptor, type, splitter.getAnnotationsForTarget(AnnotationUseSiteTarget.RECEIVER)
|
||||
functionDescriptor, it, splitter.getAnnotationsForTarget(AnnotationUseSiteTarget.RECEIVER)
|
||||
)
|
||||
}
|
||||
contextReceiverDescriptors.zip(0 until contextReceivers.size).reversed()
|
||||
.forEach { (contextReceiverDescriptor, i) ->
|
||||
contextReceivers[i].name()?.let {
|
||||
receiverToLabelMap[contextReceiverDescriptor] = it
|
||||
|
||||
if (languageVersionSettings.supportsFeature(LanguageFeature.ContextReceivers)) {
|
||||
val receiverToLabelMap = linkedMapOf<ReceiverParameterDescriptor, String>()
|
||||
if (receiverTypeRef != null && extensionReceiver != null) {
|
||||
receiverTypeRef.nameForReceiverLabel()?.let {
|
||||
receiverToLabelMap[extensionReceiver] = it
|
||||
}
|
||||
}
|
||||
trace.record(BindingContext.DESCRIPTOR_TO_NAMED_RECEIVERS, functionDescriptor, receiverToLabelMap)
|
||||
contextReceiverDescriptors.zip(0 until contextReceivers.size).reversed()
|
||||
.forEach { (contextReceiverDescriptor, i) ->
|
||||
contextReceivers[i].name()?.let {
|
||||
receiverToLabelMap[contextReceiverDescriptor] = it
|
||||
}
|
||||
}
|
||||
|
||||
trace.record(BindingContext.DESCRIPTOR_TO_NAMED_RECEIVERS, functionDescriptor, receiverToLabelMap)
|
||||
}
|
||||
|
||||
functionDescriptor.initialize(
|
||||
extensionReceiver,
|
||||
getDispatchReceiverParameterIfNeeded(container),
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.jetbrains.kotlin.types.expressions
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.diagnostics.Errors.LABEL_NAME_CLASH
|
||||
import org.jetbrains.kotlin.diagnostics.Errors.UNRESOLVED_REFERENCE
|
||||
@@ -33,12 +34,12 @@ object LabelResolver {
|
||||
private fun getElementsByLabelName(
|
||||
labelName: Name,
|
||||
labelExpression: KtSimpleNameExpression,
|
||||
isThisExpression: Boolean
|
||||
classNameLabelsEnabled: Boolean
|
||||
): Set<KtElement> {
|
||||
val elements = linkedSetOf<KtElement>()
|
||||
var parent: PsiElement? = labelExpression.parent
|
||||
while (parent != null) {
|
||||
val names = getLabelNamesIfAny(parent, isThisExpression)
|
||||
val names = getLabelNamesIfAny(parent, classNameLabelsEnabled)
|
||||
if (names.contains(labelName)) {
|
||||
elements.add(getExpressionUnderLabel(parent as KtExpression))
|
||||
}
|
||||
@@ -47,7 +48,7 @@ object LabelResolver {
|
||||
return elements
|
||||
}
|
||||
|
||||
fun getLabelNamesIfAny(element: PsiElement, addContextReceiverNames: Boolean): List<Name> {
|
||||
fun getLabelNamesIfAny(element: PsiElement, addClassNameLabels: Boolean): List<Name> {
|
||||
val result = mutableListOf<Name>()
|
||||
when (element) {
|
||||
is KtLabeledExpression -> result.addIfNotNull(element.getLabelNameAsName())
|
||||
@@ -60,10 +61,10 @@ object LabelResolver {
|
||||
is KtPropertyAccessor -> element.property
|
||||
else -> return result
|
||||
}
|
||||
if (addContextReceiverNames) {
|
||||
if (addClassNameLabels) {
|
||||
functionOrProperty.receiverTypeReference?.nameForReceiverLabel()?.let { result.add(Name.identifier(it)) }
|
||||
functionOrProperty.contextReceivers
|
||||
.mapNotNullTo(result) { it.name()?.let { s -> Name.identifier(s) } }
|
||||
functionOrProperty.receiverTypeReference?.nameForReceiverLabel()?.let { result.add(Name.identifier(it)) }
|
||||
}
|
||||
val name = functionOrProperty.nameAsName ?: getLabelForFunctionalExpression(functionOrProperty)
|
||||
result.addIfNotNull(name)
|
||||
@@ -129,9 +130,9 @@ object LabelResolver {
|
||||
labelName: Name,
|
||||
labelExpression: KtSimpleNameExpression,
|
||||
trace: BindingTrace,
|
||||
isThisExpression: Boolean
|
||||
classNameLabelsEnabled: Boolean
|
||||
): KtElement? {
|
||||
val list = getElementsByLabelName(labelName, labelExpression, isThisExpression)
|
||||
val list = getElementsByLabelName(labelName, labelExpression, classNameLabelsEnabled)
|
||||
if (list.isEmpty()) return null
|
||||
|
||||
if (list.size > 1) {
|
||||
@@ -175,15 +176,28 @@ object LabelResolver {
|
||||
return LabeledReceiverResolutionResult.labelResolutionSuccess(thisReceiver)
|
||||
}
|
||||
0 -> {
|
||||
val element = resolveNamedLabel(labelName, targetLabel, context.trace, expression is KtThisExpression)
|
||||
val element = resolveNamedLabel(
|
||||
labelName, targetLabel, context.trace,
|
||||
classNameLabelsEnabled = expression is KtThisExpression
|
||||
&& context.languageVersionSettings.supportsFeature(LanguageFeature.ContextReceivers)
|
||||
)
|
||||
val declarationDescriptor = context.trace.bindingContext[DECLARATION_TO_DESCRIPTOR, element]
|
||||
if (declarationDescriptor is FunctionDescriptor) {
|
||||
val receiverToLabelMap =
|
||||
context.trace.bindingContext[DESCRIPTOR_TO_NAMED_RECEIVERS, if (declarationDescriptor is PropertyAccessorDescriptor) declarationDescriptor.correspondingProperty else declarationDescriptor]
|
||||
val thisReceiver = receiverToLabelMap?.entries?.find {
|
||||
val receiverToLabelMap = context.trace.bindingContext[
|
||||
DESCRIPTOR_TO_NAMED_RECEIVERS,
|
||||
if (declarationDescriptor is PropertyAccessorDescriptor) declarationDescriptor.correspondingProperty else declarationDescriptor
|
||||
]
|
||||
val thisReceivers = receiverToLabelMap?.entries?.filter {
|
||||
it.value == labelName.identifier
|
||||
}?.key ?: declarationDescriptor.extensionReceiverParameter
|
||||
if (thisReceiver != null) {
|
||||
}?.map { it.key } ?: emptyList()
|
||||
val thisReceiver = when {
|
||||
thisReceivers.isEmpty() -> declarationDescriptor.extensionReceiverParameter
|
||||
thisReceivers.size == 1 -> thisReceivers.single()
|
||||
else -> {
|
||||
BindingContextUtils.reportAmbiguousLabel(context.trace, targetLabel, declarationsByLabel)
|
||||
return LabeledReceiverResolutionResult.labelResolutionFailed()
|
||||
}
|
||||
}?.also {
|
||||
context.trace.record(LABEL_TARGET, targetLabel, element)
|
||||
context.trace.record(REFERENCE_TARGET, referenceExpression, declarationDescriptor)
|
||||
}
|
||||
|
||||
+1
@@ -1,3 +1,4 @@
|
||||
// !LANGUAGE: +ContextReceivers
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun List<Int>.decimateEveryEvenThird() = <!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>sequence<!> {
|
||||
|
||||
Vendored
+1
@@ -1,3 +1,4 @@
|
||||
// !LANGUAGE: +ContextReceivers
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun List<Int>.decimateEveryEvenThird() = sequence {
|
||||
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
fun List<Int>.f() {
|
||||
this<!UNRESOLVED_LABEL!>@List<!>.size
|
||||
}
|
||||
|
||||
context(String)
|
||||
fun Int.f() {
|
||||
this<!UNRESOLVED_LABEL!>@String<!>.length
|
||||
this<!UNRESOLVED_LABEL!>@Int<!>.toDouble()
|
||||
}
|
||||
|
||||
context(String)
|
||||
val p: String get() = this<!UNRESOLVED_LABEL!>@String<!>
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
fun List<Int>.f() {
|
||||
this<!UNRESOLVED_REFERENCE!>@List<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>size<!>
|
||||
}
|
||||
|
||||
<!UNSUPPORTED_FEATURE!>context(String)<!>
|
||||
fun Int.f() {
|
||||
this<!UNRESOLVED_REFERENCE!>@String<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>length<!>
|
||||
this<!UNRESOLVED_REFERENCE!>@Int<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>toDouble<!>()
|
||||
}
|
||||
|
||||
<!UNSUPPORTED_FEATURE!>context(String)<!>
|
||||
val p: String get() = this<!UNRESOLVED_REFERENCE!>@String<!>
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
package
|
||||
|
||||
public val p: kotlin.String
|
||||
public fun kotlin.Int.f(): kotlin.Unit
|
||||
public fun kotlin.collections.List<kotlin.Int>.f(): kotlin.Unit
|
||||
+11
-1
@@ -4,7 +4,17 @@ class A<T>(val a: T)
|
||||
class B(val b: Any)
|
||||
class C(val c: Any)
|
||||
|
||||
context(A<Int>, A<String>, B) fun f() {
|
||||
context(A<String>) fun A<Int>.f() {
|
||||
this<!UNRESOLVED_LABEL!>@A<!>.a.length
|
||||
}
|
||||
|
||||
<!CONFLICTING_OVERLOADS!>context(A<String>, B) fun f()<!> {
|
||||
this<!UNRESOLVED_LABEL!>@A<!>.a.length
|
||||
this<!UNRESOLVED_LABEL!>@B<!>.b
|
||||
<!NO_THIS!>this<!>
|
||||
}
|
||||
|
||||
<!CONFLICTING_OVERLOADS!>context(A<Int>, A<String>, B) fun f()<!> {
|
||||
this<!UNRESOLVED_LABEL!>@A<!>.a.length
|
||||
this<!UNRESOLVED_LABEL!>@B<!>.b
|
||||
<!NO_THIS!>this<!>
|
||||
|
||||
Vendored
+13
-3
@@ -4,16 +4,26 @@ class A<T>(val a: T)
|
||||
class B(val b: Any)
|
||||
class C(val c: Any)
|
||||
|
||||
context(A<Int>, A<String>, B) fun f() {
|
||||
context(A<String>) fun A<Int>.f() {
|
||||
this<!AMBIGUOUS_LABEL!>@A<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>a<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>length<!>
|
||||
}
|
||||
|
||||
context(A<String>, B) fun f() {
|
||||
this@A.a.length
|
||||
this@B.b
|
||||
<!NO_THIS!>this<!>
|
||||
}
|
||||
|
||||
context(A<Int>, A<String>, B) fun f() {
|
||||
this<!AMBIGUOUS_LABEL!>@A<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>a<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>length<!>
|
||||
this@B.b
|
||||
<!NO_THIS!>this<!>
|
||||
}
|
||||
|
||||
context(A<Int>, A<String>, B) fun C.f() {
|
||||
this@A.a.length
|
||||
this<!AMBIGUOUS_LABEL!>@A<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>a<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>length<!>
|
||||
this@B.b
|
||||
this@C.c
|
||||
this@f.c
|
||||
this.c
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+3
@@ -1,6 +1,8 @@
|
||||
package
|
||||
|
||||
public fun f(): kotlin.Unit
|
||||
public fun f(): kotlin.Unit
|
||||
public fun A<kotlin.Int>.f(): kotlin.Unit
|
||||
public fun C.f(): kotlin.Unit
|
||||
|
||||
public final class A</*0*/ T> {
|
||||
@@ -26,3 +28,4 @@ public final class C {
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
|
||||
+15
-1
@@ -4,7 +4,21 @@ class A<T>(val a: T)
|
||||
class B(val b: Any)
|
||||
class C(val c: Any)
|
||||
|
||||
<!MUST_BE_INITIALIZED!>context(A<Int>, A<String>, B) var p: Int<!>
|
||||
<!MUST_BE_INITIALIZED!>context(A<String>, B) var <!REDECLARATION!>p<!>: Int<!>
|
||||
get() {
|
||||
this<!UNRESOLVED_LABEL!>@A<!>.a.length
|
||||
this<!UNRESOLVED_LABEL!>@B<!>.b
|
||||
<!NO_THIS!>this<!>
|
||||
return 1
|
||||
}
|
||||
set(value) {
|
||||
this<!UNRESOLVED_LABEL!>@A<!>.a.length
|
||||
this<!UNRESOLVED_LABEL!>@B<!>.b
|
||||
<!NO_THIS!>this<!>
|
||||
field = value
|
||||
}
|
||||
|
||||
<!MUST_BE_INITIALIZED!>context(A<Int>, A<String>, B) var <!REDECLARATION!>p<!>: Int<!>
|
||||
get() {
|
||||
this<!UNRESOLVED_LABEL!>@A<!>.a.toDouble()
|
||||
this<!UNRESOLVED_LABEL!>@A<!>.a.length
|
||||
|
||||
Vendored
+18
-4
@@ -4,9 +4,8 @@ class A<T>(val a: T)
|
||||
class B(val b: Any)
|
||||
class C(val c: Any)
|
||||
|
||||
context(A<Int>, A<String>, B) var p: Int
|
||||
context(A<String>, B) var p: Int
|
||||
get() {
|
||||
this@A.a.<!UNRESOLVED_REFERENCE!>toDouble<!>()
|
||||
this@A.a.length
|
||||
this@B.b
|
||||
<!NO_THIS!>this<!>
|
||||
@@ -19,12 +18,27 @@ context(A<Int>, A<String>, B) var p: Int
|
||||
<!UNRESOLVED_REFERENCE!>field<!> = value
|
||||
}
|
||||
|
||||
context(A<Int>, A<String>, B) var p: Int
|
||||
get() {
|
||||
this<!AMBIGUOUS_LABEL!>@A<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>a<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>toDouble<!>()
|
||||
this<!AMBIGUOUS_LABEL!>@A<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>a<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>length<!>
|
||||
this@B.b
|
||||
<!NO_THIS!>this<!>
|
||||
return 1
|
||||
}
|
||||
set(value) {
|
||||
this<!AMBIGUOUS_LABEL!>@A<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>a<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>length<!>
|
||||
this@B.b
|
||||
<!NO_THIS!>this<!>
|
||||
<!UNRESOLVED_REFERENCE!>field<!> = value
|
||||
}
|
||||
|
||||
context(A<Int>, A<String>, B) val C.p: Int
|
||||
get() {
|
||||
this@A.a.length
|
||||
this<!AMBIGUOUS_LABEL!>@A<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>a<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>length<!>
|
||||
this@B.b
|
||||
this@C.c
|
||||
this@p.c
|
||||
this.c
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
compiler/testData/diagnostics/tests/extensions/contextReceivers/thisWithReceiverLabelsProperties.txt
Vendored
+1
@@ -1,5 +1,6 @@
|
||||
package
|
||||
|
||||
public var p: kotlin.Int
|
||||
public var p: kotlin.Int
|
||||
public val C.p: kotlin.Int
|
||||
|
||||
|
||||
Generated
+6
@@ -10618,6 +10618,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
|
||||
runTest("compiler/testData/diagnostics/tests/extensions/contextReceivers/insideDeclaration.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("labelsFromClassNameForbidden.kt")
|
||||
public void testLabelsFromClassNameForbidden() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/extensions/contextReceivers/labelsFromClassNameForbidden.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("lazy.kt")
|
||||
public void testLazy() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user