From 05b91d37a712b1aa6d2ef5741c53fb48b9b3ef89 Mon Sep 17 00:00:00 2001 From: Ivan Kochurkin Date: Fri, 17 Sep 2021 18:08:24 +0300 Subject: [PATCH] [FIR] Fix collection of annotations for assigment expressions --- .../converter/ExpressionsConverter.kt | 3 ++- .../kotlin/fir/builder/RawFirBuilder.kt | 8 ++++++- .../kotlin/fir/builder/BaseFirBuilder.kt | 21 ++++++++++++++----- .../FirExpressionsResolveTransformer.kt | 2 ++ 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt index 48d01d20dc6..bd44fe791c3 100644 --- a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt +++ b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt @@ -293,7 +293,8 @@ class ExpressionsConverter( binaryExpression.toFirSourceElement(), rightArg, rightArgAsFir, - firOperation + firOperation, + leftArgAsFir.annotations ) { getAsFirExpression(this) } } else { buildEqualityOperatorCall { diff --git a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt index 057d06c7895..1b5f333dbec 100644 --- a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt +++ b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt @@ -2162,7 +2162,13 @@ open class RawFirBuilder( } else { val firOperation = operationToken.toFirOperation() if (firOperation in FirOperation.ASSIGNMENTS) { - return expression.left.generateAssignment(source, expression.right, rightArgument, firOperation) { + return expression.left.generateAssignment( + source, + expression.right, + rightArgument, + firOperation, + leftArgument.annotations + ) { (this as KtExpression).toFirExpression("Incorrect expression in assignment: ${expression.text}") } } else { diff --git a/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/BaseFirBuilder.kt b/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/BaseFirBuilder.kt index c0a002a84ba..64bb083c826 100644 --- a/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/BaseFirBuilder.kt +++ b/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/BaseFirBuilder.kt @@ -540,7 +540,9 @@ abstract class BaseFirBuilder(val baseSession: FirSession, val context: Conte generateResolvedAccessExpression(source, resultVar) else resultInitializer, - FirOperation.ASSIGN, convert + FirOperation.ASSIGN, + resultInitializer.annotations, + convert ) fun appendAssignment() { @@ -889,6 +891,7 @@ abstract class BaseFirBuilder(val baseSession: FirSession, val context: Conte rhs: T?, value: FirExpression, // value is FIR for rhs operation: FirOperation, + annotations: List, convert: T.() -> FirExpression ): FirStatement { val unwrappedLhs = this.unwrap() ?: return buildErrorExpression { @@ -901,9 +904,11 @@ abstract class BaseFirBuilder(val baseSession: FirSession, val context: Conte context.arraySetArgument[unwrappedLhs] = value } return if (operation == FirOperation.ASSIGN) { - unwrappedLhs.convert() + val result = unwrappedLhs.convert() + (result.annotations as MutableList) += annotations + result } else { - generateAugmentedArraySetCall(unwrappedLhs, baseSource, operation, rhs, convert) + generateAugmentedArraySetCall(unwrappedLhs, baseSource, operation, rhs, annotations, convert) } } @@ -920,6 +925,7 @@ abstract class BaseFirBuilder(val baseSession: FirSession, val context: Conte ) } rightArgument = value + this.annotations += annotations } } require(operation == FirOperation.ASSIGN) @@ -927,7 +933,7 @@ abstract class BaseFirBuilder(val baseSession: FirSession, val context: Conte if (this?.elementType == SAFE_ACCESS_EXPRESSION && this != null) { val safeCallNonAssignment = convert() as? FirSafeCallExpression if (safeCallNonAssignment != null) { - return putAssignmentToSafeCall(safeCallNonAssignment, baseSource, value) + return putAssignmentToSafeCall(safeCallNonAssignment, baseSource, value, annotations) } } @@ -935,6 +941,7 @@ abstract class BaseFirBuilder(val baseSession: FirSession, val context: Conte source = baseSource rValue = value calleeReference = initializeLValue(unwrappedLhs) { convert() as? FirQualifiedAccess } + this.annotations += annotations } } @@ -942,7 +949,8 @@ abstract class BaseFirBuilder(val baseSession: FirSession, val context: Conte private fun putAssignmentToSafeCall( safeCallNonAssignment: FirSafeCallExpression, baseSource: KtSourceElement?, - value: FirExpression + value: FirExpression, + annotations: List ): FirSafeCallExpression { val nestedAccess = safeCallNonAssignment.regularQualifiedAccess @@ -951,6 +959,7 @@ abstract class BaseFirBuilder(val baseSession: FirSession, val context: Conte rValue = value calleeReference = nestedAccess.calleeReference explicitReceiver = safeCallNonAssignment.checkedSubjectRef.value + this.annotations += annotations } safeCallNonAssignment.replaceRegularQualifiedAccess( @@ -965,6 +974,7 @@ abstract class BaseFirBuilder(val baseSession: FirSession, val context: Conte baseSource: KtSourceElement?, operation: FirOperation, rhs: T?, + annotations: List, convert: T.() -> FirExpression ): FirStatement { return buildAugmentedArraySetCall { @@ -972,6 +982,7 @@ abstract class BaseFirBuilder(val baseSession: FirSession, val context: Conte this.operation = operation assignCall = generateAugmentedCallForAugmentedArraySetCall(unwrappedReceiver, operation, rhs, convert) setGetBlock = generateSetGetBlockForAugmentedArraySetCall(unwrappedReceiver, baseSource, operation, rhs, convert) + this.annotations += annotations } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirExpressionsResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirExpressionsResolveTransformer.kt index 4d3bf67d607..6b787020c32 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirExpressionsResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirExpressionsResolveTransformer.kt @@ -535,6 +535,7 @@ open class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransform dispatchReceiver = it.dispatchReceiver extensionReceiver = it.extensionReceiver } + annotations += assignmentOperatorStatement.annotations } return assignment.transform(transformer, ResolutionMode.ContextIndependent) } @@ -1038,6 +1039,7 @@ open class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransform val operatorName = FirOperationNameConventions.ASSIGNMENTS.getValue(augmentedArraySetCall.operation) val firstCalls = with(augmentedArraySetCall.setGetBlock.statements.last() as FirFunctionCall) setCall@{ + (annotations as MutableList) += augmentedArraySetCall.annotations buildList { add(this@setCall) with(arguments.last() as FirFunctionCall) plusCall@{