[fir2ir] Fix field-receiver extracting for evaluator

KT-61144
This commit is contained in:
Alexey Merkulov
2023-07-27 13:51:44 +02:00
committed by Space Team
parent c520aeb5aa
commit 35eca56d32
14 changed files with 121 additions and 8 deletions
@@ -262,6 +262,18 @@ public class FirIdeNormalAnalysisSourceModuleCompilerFacilityTestGenerated exten
runTest("analysis/analysis-api/testData/components/compilerFacility/compilation/codeFragments/capturing/privateMemberCall.kt");
}
@Test
@TestMetadata("secondClassContextReceiver.kt")
public void testSecondClassContextReceiver() throws Exception {
runTest("analysis/analysis-api/testData/components/compilerFacility/compilation/codeFragments/capturing/secondClassContextReceiver.kt");
}
@Test
@TestMetadata("singleClassContextReceiver.kt")
public void testSingleClassContextReceiver() throws Exception {
runTest("analysis/analysis-api/testData/components/compilerFacility/compilation/codeFragments/capturing/singleClassContextReceiver.kt");
}
@Test
@TestMetadata("valueParameter.kt")
public void testValueParameter() throws Exception {
@@ -0,0 +1,4 @@
ContainingClass[name: <this>; isMutated: false; displayText: this@Test]
context(R|Ctx1|, R|Ctx2|)
class Test : R|kotlin/Any|
R|Test|
@@ -0,0 +1,14 @@
MODULE_FRAGMENT name:<Sources of main>
FILE fqName:<root> fileName:/fragment.kt
CLASS CLASS name:CodeFragment modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.CodeFragment
CONSTRUCTOR visibility:public <> () returnType:<root>.CodeFragment [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
FUN name:run visibility:public modality:FINAL <> (p0:<root>.Test) returnType:kotlin.Int
VALUE_PARAMETER name:p0 index:0 type:<root>.Test
EXPRESSION_BODY
BLOCK type=kotlin.Int origin=null
CALL 'public final fun useWithCtx2 ($context_receiver_0: <root>.Ctx2): kotlin.Int declared in <root>.SecondClassContextReceiverKt' type=kotlin.Int origin=null
$context_receiver_0: GET_FIELD 'FIELD FIELD_FOR_CLASS_CONTEXT_RECEIVER name:contextReceiverField1 type:<root>.Ctx2 visibility:private [final]' type=<root>.Ctx2 origin=null
receiver: GET_VAR 'p0: <root>.Test declared in <root>.CodeFragment.run' type=<root>.Test origin=null
@@ -0,0 +1,13 @@
// !LANGUAGE: +ContextReceivers
class Ctx1
class Ctx2
context(Ctx2)
fun useWithCtx2() = 3
context(Ctx1, Ctx2)
class Test {
fun foo() {
<caret>val x = 1
}
}
@@ -0,0 +1,5 @@
public final class CodeFragment {
// source: 'fragment.kt'
public method <init>(): void
public final static method run(p0: Test): int
}
@@ -0,0 +1,4 @@
ContainingClass[name: <this>; isMutated: false; displayText: this@Test]
context(R|Ctx1|)
class Test : R|kotlin/Any|
R|Test|
@@ -0,0 +1,14 @@
MODULE_FRAGMENT name:<Sources of main>
FILE fqName:<root> fileName:/fragment.kt
CLASS CLASS name:CodeFragment modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.CodeFragment
CONSTRUCTOR visibility:public <> () returnType:<root>.CodeFragment [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
FUN name:run visibility:public modality:FINAL <> (p0:<root>.Test) returnType:kotlin.Int
VALUE_PARAMETER name:p0 index:0 type:<root>.Test
EXPRESSION_BODY
BLOCK type=kotlin.Int origin=null
CALL 'public final fun useWithCtx1 ($context_receiver_0: <root>.Ctx1): kotlin.Int declared in <root>.SingleClassContextReceiverKt' type=kotlin.Int origin=null
$context_receiver_0: GET_FIELD 'FIELD FIELD_FOR_CLASS_CONTEXT_RECEIVER name:contextReceiverField0 type:<root>.Ctx1 visibility:private [final]' type=<root>.Ctx1 origin=null
receiver: GET_VAR 'p0: <root>.Test declared in <root>.CodeFragment.run' type=<root>.Test origin=null
@@ -0,0 +1,12 @@
// !LANGUAGE: +ContextReceivers
class Ctx1
context(Ctx1)
fun useWithCtx1() = 3
context(Ctx1)
class Test {
fun foo() {
<caret>val x = 1
}
}
@@ -0,0 +1,5 @@
public final class CodeFragment {
// source: 'fragment.kt'
public method <init>(): void
public final static method run(p0: Test): int
}
@@ -162,6 +162,18 @@ public class CodeFragmentCapturingTestGenerated extends AbstractCodeFragmentCapt
runTest("analysis/analysis-api/testData/components/compilerFacility/compilation/codeFragments/capturing/privateMemberCall.kt");
}
@Test
@TestMetadata("secondClassContextReceiver.kt")
public void testSecondClassContextReceiver() throws Exception {
runTest("analysis/analysis-api/testData/components/compilerFacility/compilation/codeFragments/capturing/secondClassContextReceiver.kt");
}
@Test
@TestMetadata("singleClassContextReceiver.kt")
public void testSingleClassContextReceiver() throws Exception {
runTest("analysis/analysis-api/testData/components/compilerFacility/compilation/codeFragments/capturing/singleClassContextReceiver.kt");
}
@Test
@TestMetadata("valueParameter.kt")
public void testValueParameter() throws Exception {
@@ -655,9 +655,12 @@ class Fir2IrVisitor(
): IrElement = whileAnalysing(session, thisReceiverExpression) {
val calleeReference = thisReceiverExpression.calleeReference
callGenerator.injectGetValueCall(thisReceiverExpression, calleeReference)?.let { return it }
val boundSymbol = calleeReference.boundSymbol
if (boundSymbol !is FirClassSymbol || calleeReference.contextReceiverNumber == -1) {
callGenerator.injectGetValueCall(thisReceiverExpression, calleeReference)?.let { return it }
}
when (boundSymbol) {
is FirClassSymbol -> {
// Object case
@@ -680,7 +683,9 @@ class Fir2IrVisitor(
val dispatchReceiver = conversionScope.dispatchReceiverParameter(irClass)
if (dispatchReceiver != null) {
return thisReceiverExpression.convertWithOffsets { startOffset, endOffset ->
val thisRef = IrGetValueImpl(startOffset, endOffset, dispatchReceiver.type, dispatchReceiver.symbol)
val thisRef = callGenerator.findInjectedValue(calleeReference)?.let {
callGenerator.useInjectedValue(it, calleeReference, startOffset, endOffset)
} ?: IrGetValueImpl(startOffset, endOffset, dispatchReceiver.type, dispatchReceiver.symbol)
if (calleeReference.contextReceiverNumber != -1) {
val constructorForCurrentlyGeneratedDelegatedConstructor =
conversionScope.getConstructorForCurrentlyGeneratedDelegatedConstructor(irClass)
@@ -343,18 +343,27 @@ class CallAndReferenceGenerator(
}
internal fun injectGetValueCall(element: FirElement, calleeReference: FirReference): IrExpression? {
val injectedValue = extensions.findInjectedValue(calleeReference, conversionScope)
val injectedValue = findInjectedValue(calleeReference)
if (injectedValue != null) {
return element.convertWithOffsets { startOffset, endOffset ->
val type = injectedValue.typeRef.toIrType()
val origin = calleeReference.statementOrigin()
IrGetValueImpl(startOffset, endOffset, type, injectedValue.irParameterSymbol, origin)
useInjectedValue(injectedValue, calleeReference, startOffset, endOffset)
}
}
return null
}
internal fun useInjectedValue(
injectedValue: InjectedValue,
calleeReference: FirReference,
startOffset: Int,
endOffset: Int,
): IrGetValueImpl {
val type = injectedValue.typeRef.toIrType()
val origin = calleeReference.statementOrigin()
return IrGetValueImpl(startOffset, endOffset, type, injectedValue.irParameterSymbol, origin)
}
fun convertToIrCall(
qualifiedAccess: FirQualifiedAccessExpression,
typeRef: FirTypeRef,
@@ -552,7 +561,7 @@ class CallAndReferenceGenerator(
}
private fun injectSetValueCall(element: FirElement, calleeReference: FirReference, assignedValue: IrExpression): IrExpression? {
val injectedValue = extensions.findInjectedValue(calleeReference, conversionScope)
val injectedValue = findInjectedValue(calleeReference)
if (injectedValue != null) {
return element.convertWithOffsets { startOffset, endOffset ->
val type = irBuiltIns.unitType
@@ -564,6 +573,8 @@ class CallAndReferenceGenerator(
return null
}
internal fun findInjectedValue(calleeReference: FirReference) = extensions.findInjectedValue(calleeReference, conversionScope)
fun convertToIrSetCall(variableAssignment: FirVariableAssignment, explicitReceiverExpression: IrExpression?): IrExpression {
try {
val type = irBuiltIns.unitType