[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
This commit is contained in:
Dmitrii Gridin
2023-12-05 15:34:17 +01:00
committed by Space Team
parent 1fcd277538
commit c59ea8bd2a
@@ -2049,20 +2049,21 @@ open class PsiRawFirBuilder(
isExternal = hasModifier(EXTERNAL_KEYWORD) isExternal = hasModifier(EXTERNAL_KEYWORD)
} }
if (hasDelegate()) { val psiPropertyDelegate = this@toFirProperty.delegate
fun extractDelegateExpression() = if (psiPropertyDelegate != null) {
buildOrLazyExpression(this@toFirProperty.delegate?.expression?.toFirSourceElement(KtFakeSourceElementKind.WrappedDelegate)) { fun extractDelegateExpression(): FirExpression {
this@toFirProperty.delegate?.expression?.let { expression -> return buildOrLazyExpression(psiPropertyDelegate.expression?.toFirSourceElement(KtFakeSourceElementKind.WrappedDelegate)) {
expression.toFirExpression("Should have delegate") psiPropertyDelegate.expression?.toFirExpression("Should have delegate") ?: buildErrorExpression {
} ?: buildErrorExpression {
diagnostic = ConeSimpleDiagnostic("Should have delegate", DiagnosticKind.ExpressionExpected) diagnostic = ConeSimpleDiagnostic("Should have delegate", DiagnosticKind.ExpressionExpected)
} }
} }
}
val delegateBuilder = FirWrappedDelegateExpressionBuilder().apply { val delegateBuilder = FirWrappedDelegateExpressionBuilder().apply {
val delegateExpression = extractDelegateExpression() val delegateExpression = extractDelegateExpression()
source = delegateExpression.source?.fakeElement(KtFakeSourceElementKind.WrappedDelegate) source = (psiPropertyDelegate.expression ?: psiPropertyDelegate)
?: this@toFirProperty.delegate?.toFirSourceElement(KtFakeSourceElementKind.WrappedDelegate) .toFirSourceElement(KtFakeSourceElementKind.WrappedDelegate)
expression = delegateExpression expression = delegateExpression
} }