[FE, PSI2IR] Pass context receivers when calling typealias constructor

This commit is contained in:
Anastasiya Shadrina
2021-11-17 05:14:30 +07:00
committed by TeamCityServer
parent fb99df235e
commit bd4e51f304
8 changed files with 63 additions and 5 deletions
@@ -15976,6 +15976,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/extensionClasses/simple.kt");
}
@Test
@TestMetadata("typealiasForContextualClass.kt")
public void testTypealiasForContextualClass() throws Exception {
runTest("compiler/testData/codegen/box/extensionClasses/typealiasForContextualClass.kt");
}
@Test
@TestMetadata("useFromAnotherModule.kt")
public void testUseFromAnotherModule() throws Exception {
@@ -311,9 +311,9 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
.map(typeReference -> {
KotlinType kotlinType =
c.getTypeResolver().resolveType(getScopeForClassHeaderResolution(), typeReference, c.getTrace(), true);
return new ReceiverParameterDescriptorImpl(
return DescriptorFactory.createContextReceiverParameterForClass(
this,
new ContextClassReceiver(this, kotlinType, null),
kotlinType,
Annotations.Companion.getEMPTY()
);
}).collect(Collectors.toList());
@@ -252,7 +252,7 @@ fun StatementGenerator.generateCallReceiver(
}
dispatchReceiverValue = generateReceiverOrNull(ktDefaultElement, extensionReceiver ?: dispatchReceiver)
extensionReceiverValue = null
contextReceiverValues = emptyList()
contextReceiverValues = contextReceivers.mapNotNull { generateReceiverOrNull(ktDefaultElement, it) }
}
else -> {
dispatchReceiverValue = generateReceiverOrNull(ktDefaultElement, dispatchReceiver)
@@ -0,0 +1,17 @@
// !LANGUAGE: +ContextReceivers
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_FIR: JVM_IR
class A {
val ok = "OK"
}
context(A)
class B {
val result = ok
}
typealias C = B
fun box(): String =
with(A()) { C().result }
@@ -15976,6 +15976,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/extensionClasses/simple.kt");
}
@Test
@TestMetadata("typealiasForContextualClass.kt")
public void testTypealiasForContextualClass() throws Exception {
runTest("compiler/testData/codegen/box/extensionClasses/typealiasForContextualClass.kt");
}
@Test
@TestMetadata("useFromAnotherModule.kt")
public void testUseFromAnotherModule() throws Exception {
@@ -83,7 +83,7 @@ class TypeAliasConstructorDescriptorImpl private constructor(
typeAliasConstructor.initialize(
null,
underlyingConstructorDescriptor.dispatchReceiverParameter?.substitute(substitutorForUnderlyingClass),
emptyList(),
underlyingConstructorDescriptor.contextReceiverParameters.map { it.substitute(substitutorForUnderlyingClass) },
typeAliasDescriptor.declaredTypeParameters,
valueParameters,
returnType,
@@ -203,10 +203,21 @@ class TypeAliasConstructorDescriptorImpl private constructor(
)
}
val classDescriptor = typeAliasDescriptor.classDescriptor
val contextReceiverParameters = classDescriptor?.let {
constructor.contextReceiverParameters.map {
DescriptorFactory.createContextReceiverParameterForClass(
classDescriptor,
substitutorForUnderlyingClass.safeSubstitute(it.type, Variance.INVARIANT),
Annotations.EMPTY
)
}
} ?: emptyList()
typeAliasConstructor.initialize(
receiverParameter,
null,
emptyList(),
contextReceiverParameters,
typeAliasDescriptor.declaredTypeParameters,
valueParameters,
returnType,
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.descriptors.impl.*;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.resolve.scopes.receivers.ContextClassReceiver;
import org.jetbrains.kotlin.resolve.scopes.receivers.ContextReceiver;
import org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver;
import org.jetbrains.kotlin.types.KotlinType;
@@ -205,4 +206,15 @@ public class DescriptorFactory {
? null
: new ReceiverParameterDescriptorImpl(owner, new ContextReceiver(owner, receiverParameterType, null), annotations);
}
@Nullable
public static ReceiverParameterDescriptor createContextReceiverParameterForClass(
@NotNull ClassDescriptor owner,
@Nullable KotlinType receiverParameterType,
@NotNull Annotations annotations
) {
return receiverParameterType == null
? null
: new ReceiverParameterDescriptorImpl(owner, new ContextClassReceiver(owner, receiverParameterType, null), annotations);
}
}
@@ -16161,6 +16161,12 @@ public class NativeExtBlackBoxTestGenerated extends AbstractNativeBlackBoxTest {
runTest("compiler/testData/codegen/box/extensionClasses/simple.kt");
}
@Test
@TestMetadata("typealiasForContextualClass.kt")
public void testTypealiasForContextualClass() throws Exception {
runTest("compiler/testData/codegen/box/extensionClasses/typealiasForContextualClass.kt");
}
@Test
@TestMetadata("useFromAnotherModule.kt")
public void testUseFromAnotherModule() throws Exception {