[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)
}
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
}