[FIR] Fix building fir for += with complex rhs
Also fix choosing candidate for assignment operator call
This commit is contained in:
@@ -463,22 +463,6 @@ abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context =
|
||||
statements += arraySet.apply { lValue = FirSimpleNamedReference(psiArrayExpression?.toFirSourceElement(), name, null) }
|
||||
}
|
||||
}
|
||||
if (operation != FirOperation.ASSIGN &&
|
||||
tokenType != REFERENCE_EXPRESSION && tokenType != THIS_EXPRESSION &&
|
||||
((tokenType != DOT_QUALIFIED_EXPRESSION && tokenType != SAFE_ACCESS_EXPRESSION) || this.selectorExpression?.elementType != REFERENCE_EXPRESSION)
|
||||
) {
|
||||
return FirBlockImpl(this.getSourceOrNull()).apply {
|
||||
val name = Name.special("<complex-set>")
|
||||
statements += generateTemporaryVariable(
|
||||
this@BaseFirBuilder.session, this@generateAssignment.getSourceOrNull(), name,
|
||||
this@generateAssignment?.convert()
|
||||
?: FirErrorExpressionImpl(this.getSourceOrNull(), FirSimpleDiagnostic("No LValue in assignment", DiagnosticKind.Syntax))
|
||||
)
|
||||
statements += FirVariableAssignmentImpl(source, false, value, operation).apply {
|
||||
lValue = FirSimpleNamedReference(this.getSourceOrNull(), name, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (operation in FirOperation.ASSIGNMENTS && operation != FirOperation.ASSIGN) {
|
||||
return FirOperatorCallImpl(source, operation).apply {
|
||||
|
||||
@@ -64,11 +64,10 @@ FILE: lambda.kt
|
||||
public? final? fun test(list: List<Int>): R|kotlin/Unit| {
|
||||
lval map: <implicit> = mutableMapOf#<Int, String>()
|
||||
list#.forEach#(<L> = forEach@fun <implicit>.<anonymous>(): <implicit> {
|
||||
lval <complex-set>: <implicit> = map#.getOrPut#(it#, getOrPut@fun <implicit>.<anonymous>(): <implicit> {
|
||||
+=(map#.getOrPut#(it#, getOrPut@fun <implicit>.<anonymous>(): <implicit> {
|
||||
mutableListOf#()
|
||||
}
|
||||
)
|
||||
<complex-set># += String()
|
||||
), String())
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -11,6 +11,5 @@ FILE: modifications.kt
|
||||
+=(this#, String(Omega))
|
||||
}
|
||||
public? final? fun Any.modify(): R|kotlin/Unit| {
|
||||
lval <complex-set>: <implicit> = (this# as List<Int>)
|
||||
<complex-set># += Int(42)
|
||||
+=((this# as List<Int>), Int(42))
|
||||
}
|
||||
|
||||
+4
@@ -37,6 +37,10 @@ class FirOperatorAmbiguityError(val candidates: Collection<AbstractFirBasedSymbo
|
||||
override val reason: String get() = "Operator overload ambiguity. Compatible candidates: ${candidates.map { describeSymbol(it) }}"
|
||||
}
|
||||
|
||||
class FirVariableExpectedError : FirDiagnostic() {
|
||||
override val reason: String get() = "Variable expected"
|
||||
}
|
||||
|
||||
private fun describeSymbol(symbol: AbstractFirBasedSymbol<*>): String {
|
||||
return when (symbol) {
|
||||
is FirClassLikeSymbol<*> -> symbol.classId.asString()
|
||||
|
||||
+1
@@ -56,6 +56,7 @@ class ErrorNodeDiagnosticCollectorComponent(collector: AbstractDiagnosticCollect
|
||||
is FirInapplicableCandidateError -> FirErrors.INAPPLICABLE_CANDIDATE.onSource(source, diagnostic.candidates)
|
||||
is FirAmbiguityError -> FirErrors.AMBIGUITY.onSource(source, diagnostic.candidates)
|
||||
is FirOperatorAmbiguityError -> FirErrors.ASSIGN_OPERATOR_AMBIGUITY.onSource(source, diagnostic.candidates)
|
||||
is FirVariableExpectedError -> Errors.VARIABLE_EXPECTED.onSource(source)
|
||||
is FirSimpleDiagnostic -> diagnostic.getFactory().onSource(source)
|
||||
FirEmptyDiagnostic -> null
|
||||
else -> throw IllegalArgumentException("Unsupported diagnostic type: ${diagnostic.javaClass}")
|
||||
|
||||
+19
-13
@@ -7,7 +7,9 @@ package org.jetbrains.kotlin.fir.resolve.transformers.body.resolve
|
||||
|
||||
import org.jetbrains.kotlin.fir.BuiltinTypes
|
||||
import org.jetbrains.kotlin.fir.FirCallResolver
|
||||
import org.jetbrains.kotlin.fir.declarations.FirProperty
|
||||
import org.jetbrains.kotlin.fir.declarations.FirTypeParametersOwner
|
||||
import org.jetbrains.kotlin.fir.declarations.FirVariable
|
||||
import org.jetbrains.kotlin.fir.diagnostics.DiagnosticKind
|
||||
import org.jetbrains.kotlin.fir.diagnostics.FirSimpleDiagnostic
|
||||
import org.jetbrains.kotlin.fir.expressions.*
|
||||
@@ -26,13 +28,14 @@ import org.jetbrains.kotlin.fir.resolve.*
|
||||
import org.jetbrains.kotlin.fir.resolve.calls.ConeInferenceContext
|
||||
import org.jetbrains.kotlin.fir.resolve.calls.candidate
|
||||
import org.jetbrains.kotlin.fir.resolve.diagnostics.FirOperatorAmbiguityError
|
||||
import org.jetbrains.kotlin.fir.resolve.diagnostics.FirUnresolvedReferenceError
|
||||
import org.jetbrains.kotlin.fir.resolve.diagnostics.FirVariableExpectedError
|
||||
import org.jetbrains.kotlin.fir.resolve.transformers.InvocationKindTransformer
|
||||
import org.jetbrains.kotlin.fir.resolve.transformers.StoreReceiver
|
||||
import org.jetbrains.kotlin.fir.resolvedTypeFromPrototype
|
||||
import org.jetbrains.kotlin.fir.scopes.impl.withReplacedConeType
|
||||
import org.jetbrains.kotlin.fir.symbols.StandardClassIds
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.invoke
|
||||
import org.jetbrains.kotlin.fir.types.*
|
||||
import org.jetbrains.kotlin.fir.types.impl.ConeClassTypeImpl
|
||||
@@ -200,13 +203,13 @@ class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransformer) :
|
||||
}
|
||||
|
||||
override fun transformOperatorCall(operatorCall: FirOperatorCall, data: ResolutionMode): CompositeTransformResult<FirStatement> {
|
||||
val result = if (operatorCall.operation in FirOperation.BOOLEANS) {
|
||||
(operatorCall.transformChildren(transformer, ResolutionMode.ContextIndependent) as FirOperatorCall).also {
|
||||
if (operatorCall.operation in FirOperation.BOOLEANS) {
|
||||
val result = (operatorCall.transformChildren(transformer, ResolutionMode.ContextIndependent) as FirOperatorCall).also {
|
||||
it.resultType = builtinTypes.booleanType
|
||||
}
|
||||
} else {
|
||||
transformExpression(operatorCall, data).single
|
||||
} as FirOperatorCall
|
||||
dataFlowAnalyzer.exitOperatorCall(result)
|
||||
return result.compose()
|
||||
}
|
||||
|
||||
if (operatorCall.operation in FirOperation.ASSIGNMENTS) {
|
||||
require(operatorCall.operation != FirOperation.ASSIGN)
|
||||
@@ -230,20 +233,24 @@ class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransformer) :
|
||||
val assignOperatorCall = createFunctionCall(assignmentOperatorName)
|
||||
val resolvedAssignCall = assignOperatorCall.transformSingle(this, ResolutionMode.ContextIndependent)
|
||||
val assignCallReference = resolvedAssignCall.toResolvedCallableReference()
|
||||
// x + y
|
||||
val assignIsError = resolvedAssignCall.typeRef is FirErrorTypeRef
|
||||
// x = x + y
|
||||
val simpleOperatorName = FirOperationNameConventions.ASSIGNMENTS_TO_SIMPLE_OPERATOR.getValue(operatorCall.operation)
|
||||
val simpleOperatorCall = createFunctionCall(simpleOperatorName)
|
||||
val resolvedOperatorCall = simpleOperatorCall.transformSingle(this, ResolutionMode.ContextIndependent)
|
||||
val operatorCallReference = resolvedOperatorCall.toResolvedCallableReference()
|
||||
|
||||
val property = (leftArgument.toResolvedCallableSymbol() as? FirPropertySymbol)?.fir
|
||||
val lhsReference = leftArgument.toResolvedCallableReference()
|
||||
val lhsIsVar = (lhsReference?.resolvedSymbol as? FirVariableSymbol<*>)?.fir?.isVar == true
|
||||
return when {
|
||||
operatorCallReference == null || property?.isVal == true -> resolvedAssignCall.compose()
|
||||
operatorCallReference == null || (!lhsIsVar && !assignIsError) -> resolvedAssignCall.compose()
|
||||
assignCallReference == null -> {
|
||||
val assignment =
|
||||
FirVariableAssignmentImpl(operatorCall.source, false, resolvedOperatorCall, FirOperation.ASSIGN).apply {
|
||||
lValue = (leftArgument as? FirQualifiedAccess)?.calleeReference
|
||||
?: FirErrorNamedReferenceImpl(null, FirUnresolvedReferenceError())
|
||||
lValue = if (lhsIsVar)
|
||||
lhsReference!!
|
||||
else
|
||||
FirErrorNamedReferenceImpl(operatorCall.arguments.first().source, FirVariableExpectedError())
|
||||
}
|
||||
assignment.transform(transformer, ResolutionMode.ContextIndependent)
|
||||
}
|
||||
@@ -254,8 +261,7 @@ class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransformer) :
|
||||
}
|
||||
}
|
||||
|
||||
dataFlowAnalyzer.exitOperatorCall(result)
|
||||
return result.compose()
|
||||
throw IllegalArgumentException(operatorCall.render())
|
||||
}
|
||||
|
||||
override fun transformTypeOperatorCall(typeOperatorCall: FirTypeOperatorCall, data: ResolutionMode): CompositeTransformResult<FirStatement> {
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
var x: Int = 1
|
||||
set(value) {
|
||||
field += value
|
||||
}
|
||||
|
||||
val y: Int = 1
|
||||
get() {
|
||||
<!VARIABLE_EXPECTED!>field<!> += 1
|
||||
return 1
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
FILE: fieldPlusAssign.kt
|
||||
public final var x: R|kotlin/Int| = Int(1)
|
||||
public get(): R|kotlin/Int|
|
||||
public set(value: R|kotlin/Int|): R|kotlin/Unit| {
|
||||
F|/x| = F|/x|.R|kotlin/Int.plus|(R|<local>/value|)
|
||||
}
|
||||
public final val y: R|kotlin/Int| = Int(1)
|
||||
public get(): R|kotlin/Int| {
|
||||
<Variable expected># = F|/y|.R|kotlin/Int.plus|(Int(1))
|
||||
^ Int(1)
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
fun List<String>.modify() {
|
||||
<!VARIABLE_EXPECTED!>this<!> += "Alpha"
|
||||
<!VARIABLE_EXPECTED!>this<!> += "Omega"
|
||||
}
|
||||
|
||||
fun Any.modify() {
|
||||
(<!VARIABLE_EXPECTED!>this as List<Int><!>) += 42
|
||||
}
|
||||
|
||||
operator fun <T> Set<T>.plusAssign(x: T) {}
|
||||
|
||||
fun Set<String>.modify() {
|
||||
this += "Alpha"
|
||||
this += "Omega"
|
||||
}
|
||||
|
||||
fun Any.modifySet() {
|
||||
(this as Set<Int>) += 42
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
FILE: listPlusAssign.kt
|
||||
public final fun R|kotlin/collections/List<kotlin/String>|.modify(): R|kotlin/Unit| {
|
||||
<Variable expected># = this@R|/modify|.R|kotlin/collections/plus|<R|kotlin/String|>(String(Alpha))
|
||||
<Variable expected># = this@R|/modify|.R|kotlin/collections/plus|<R|kotlin/String|>(String(Omega))
|
||||
}
|
||||
public final fun R|kotlin/Any|.modify(): R|kotlin/Unit| {
|
||||
<Variable expected># = (this@R|/modify| as R|kotlin/collections/List<kotlin/Int>|).R|kotlin/collections/plus|<R|kotlin/Int|>(Int(42))
|
||||
}
|
||||
public final operator fun <T> R|kotlin/collections/Set<T>|.plusAssign(x: R|T|): R|kotlin/Unit| {
|
||||
}
|
||||
public final fun R|kotlin/collections/Set<kotlin/String>|.modify(): R|kotlin/Unit| {
|
||||
this@R|/modify|.R|/plusAssign|<R|kotlin/String|>(String(Alpha))
|
||||
this@R|/modify|.R|/plusAssign|<R|kotlin/String|>(String(Omega))
|
||||
}
|
||||
public final fun R|kotlin/Any|.modifySet(): R|kotlin/Unit| {
|
||||
(this@R|/modifySet| as R|kotlin/collections/Set<kotlin/Int>|).R|/plusAssign|<R|kotlin/Int|>(Int(42))
|
||||
}
|
||||
+5
@@ -230,6 +230,11 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest {
|
||||
runTest("compiler/fir/resolve/testData/resolve/arguments/default.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldPlusAssign.kt")
|
||||
public void testFieldPlusAssign() throws Exception {
|
||||
runTest("compiler/fir/resolve/testData/resolve/arguments/fieldPlusAssign.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("invoke.kt")
|
||||
public void testInvoke() throws Exception {
|
||||
runTest("compiler/fir/resolve/testData/resolve/arguments/invoke.kt");
|
||||
|
||||
Generated
+5
@@ -98,6 +98,11 @@ public class FirDiagnosticsWithStdlibTestGenerated extends AbstractFirDiagnostic
|
||||
runTest("compiler/fir/resolve/testData/resolve/stdlib/implicitReceiverOrder.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("listPlusAssign.kt")
|
||||
public void testListPlusAssign() throws Exception {
|
||||
runTest("compiler/fir/resolve/testData/resolve/stdlib/listPlusAssign.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("mapList.kt")
|
||||
public void testMapList() throws Exception {
|
||||
runTest("compiler/fir/resolve/testData/resolve/stdlib/mapList.kt");
|
||||
|
||||
Reference in New Issue
Block a user