[FIR] Fix incorrect updating of callable reference type in delegated property

... if the delegate happens to be a function call whose second
argument is a callable reference but not actually a provideDelegate
call.
The fix is to ensure that the call is _actually_ a desugared
provideDelegate call.

This fixes a CCE in a case where the delegate expression is a regular
function call and the second argument is a callable reference.

#KT-65165 Fixed
This commit is contained in:
Kirill Rakhman
2024-01-30 17:49:06 +01:00
committed by Space Team
parent fced126c9f
commit 853ad54699
22 changed files with 150 additions and 1 deletions
@@ -15788,6 +15788,12 @@ public class LLFirBlackBoxCodegenBasedTestGenerated extends AbstractLLFirBlackBo
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
@@ -15788,6 +15788,12 @@ public class LLFirReversedBlackBoxCodegenBasedTestGenerated extends AbstractLLFi
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
@@ -15789,6 +15789,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
@@ -15789,6 +15789,12 @@ public class FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
@@ -15789,6 +15789,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
@@ -53,6 +53,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.buildCurrentSubstitutor
import org.jetbrains.kotlin.resolve.calls.inference.components.TypeVariableDirectionCalculator
import org.jetbrains.kotlin.resolve.calls.inference.model.ProvideDelegateFixationPosition
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
import org.jetbrains.kotlin.util.OperatorNameConventions
import org.jetbrains.kotlin.util.PrivateForInline
import org.jetbrains.kotlin.utils.exceptions.requireWithAttachment
@@ -323,7 +324,13 @@ open class FirDeclarationsResolveTransformer(
(property.setter?.body?.statements?.singleOrNull() as? FirReturnExpression)?.let { returnExpression ->
(returnExpression.result as? FirFunctionCall)?.replacePropertyReferenceTypeInDelegateAccessors(property)
}
(property.delegate as? FirFunctionCall)?.replacePropertyReferenceTypeInDelegateAccessors(property)
val delegate = property.delegate
if (delegate is FirFunctionCall &&
delegate.calleeReference.name == OperatorNameConventions.PROVIDE_DELEGATE &&
delegate.source?.kind == KtFakeSourceElementKind.DelegatedPropertyAccessor
) {
delegate.replacePropertyReferenceTypeInDelegateAccessors(property)
}
}
private fun transformPropertyAccessorsWithDelegate(
@@ -0,0 +1,23 @@
// WITH_STDLIB
// ISSUE: KT-65165
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
private class TransformedConfigPropertyString(
defaultValue: String,
private val transform: (String) -> Regex
) : ReadOnlyProperty<SampleClass, Regex> {
override fun getValue(thisRef: SampleClass, property: KProperty<*>): Regex {
return transform("string")
}
}
class SampleClass {
val ignoreStringsRegex: Regex by TransformedConfigPropertyString("$^", String::toRegex)
}
fun box(): String {
SampleClass().ignoreStringsRegex
return "OK"
}
@@ -15789,6 +15789,12 @@ public class JvmAbiConsistencyTestBoxGenerated extends AbstractJvmAbiConsistency
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
@@ -15423,6 +15423,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
@@ -15789,6 +15789,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
@@ -15789,6 +15789,12 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
@@ -13005,6 +13005,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/capturePropertyInClosure.kt");
@@ -11889,6 +11889,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
@@ -11889,6 +11889,12 @@ public class FirJsES6CodegenBoxTestGenerated extends AbstractFirJsES6CodegenBoxT
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
@@ -11889,6 +11889,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
@@ -11889,6 +11889,12 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
@@ -13361,6 +13361,12 @@ public class FirNativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTe
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
@@ -13669,6 +13669,12 @@ public class FirNativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenB
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
@@ -13053,6 +13053,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
@@ -13362,6 +13362,12 @@ public class NativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenBoxT
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
@@ -11865,6 +11865,12 @@ public class FirWasmJsCodegenBoxTestGenerated extends AbstractFirWasmJsCodegenBo
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {
@@ -11865,6 +11865,12 @@ public class K1WasmCodegenBoxTestGenerated extends AbstractK1WasmCodegenBoxTest
runTest("compiler/testData/codegen/box/delegatedProperty/beforeDeclarationContainerOptimization.kt");
}
@Test
@TestMetadata("callableReferenceAsSecondArgumentOfDelegate.kt")
public void testCallableReferenceAsSecondArgumentOfDelegate() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/callableReferenceAsSecondArgumentOfDelegate.kt");
}
@Test
@TestMetadata("capturePropertyInClosure.kt")
public void testCapturePropertyInClosure() throws Exception {