From c59ea8bd2a603769a2c64acf12afd31a51b88f4e Mon Sep 17 00:00:00 2001 From: Dmitrii Gridin Date: Tue, 5 Dec 2023 15:34:17 +0100 Subject: [PATCH] [FIR] use delegate expression as source for FirWrappedDelegateExpressionBuilder The previous implementation was not stable because `extractDelegateExpression` has a source depending on extractDelegateExpression, and it led to inconsistency in lazy and regular mode in the case of labeled expression ```kotlin val a: String by l@ MyProperty() ``` In lazy mode the source is KtLabeledExpression, but in the regular one is KtCallExpression ^KT-56551 --- .../kotlin/fir/builder/PsiRawFirBuilder.kt | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/PsiRawFirBuilder.kt b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/PsiRawFirBuilder.kt index 00c356a4512..790dc82bb11 100644 --- a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/PsiRawFirBuilder.kt +++ b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/PsiRawFirBuilder.kt @@ -2049,20 +2049,21 @@ open class PsiRawFirBuilder( isExternal = hasModifier(EXTERNAL_KEYWORD) } - if (hasDelegate()) { - fun extractDelegateExpression() = - buildOrLazyExpression(this@toFirProperty.delegate?.expression?.toFirSourceElement(KtFakeSourceElementKind.WrappedDelegate)) { - this@toFirProperty.delegate?.expression?.let { expression -> - expression.toFirExpression("Should have delegate") - } ?: buildErrorExpression { + val psiPropertyDelegate = this@toFirProperty.delegate + if (psiPropertyDelegate != null) { + fun extractDelegateExpression(): FirExpression { + return buildOrLazyExpression(psiPropertyDelegate.expression?.toFirSourceElement(KtFakeSourceElementKind.WrappedDelegate)) { + psiPropertyDelegate.expression?.toFirExpression("Should have delegate") ?: buildErrorExpression { diagnostic = ConeSimpleDiagnostic("Should have delegate", DiagnosticKind.ExpressionExpected) } } + } val delegateBuilder = FirWrappedDelegateExpressionBuilder().apply { val delegateExpression = extractDelegateExpression() - source = delegateExpression.source?.fakeElement(KtFakeSourceElementKind.WrappedDelegate) - ?: this@toFirProperty.delegate?.toFirSourceElement(KtFakeSourceElementKind.WrappedDelegate) + source = (psiPropertyDelegate.expression ?: psiPropertyDelegate) + .toFirSourceElement(KtFakeSourceElementKind.WrappedDelegate) + expression = delegateExpression }