[FIR] Use Operator origin for ITERATOR and HAS_NEXT function calls instead of Regular

It automatically fixes KT-62356 since iterators now are operators

^KT-62356 Fixed
This commit is contained in:
Ivan Kochurkin
2024-02-18 19:15:14 +01:00
committed by Space Team
parent 313b230333
commit 55159694df
7 changed files with 40 additions and 22 deletions
@@ -260,9 +260,9 @@ FILE: forLoopChecker.kt
}
{
lval <iterator>: R|ImproperIterator6| = R|<local>/notRange9|.R|/NotRange9.iterator|()
while(R|<local>/<iterator>|.R|/ImproperIterator6.hasNext|()) {
lval i: R|kotlin/Int| = R|<local>/<iterator>|.R|/ImproperIterator6.next|()
lval <iterator>: R|ImproperIterator6| = R|<local>/notRange9|.R|/NotRange9.iterator<Inapplicable(CONVENTION_ERROR): /NotRange9.iterator>#|()
while(R|<local>/<iterator>|.R|/ImproperIterator6.hasNext<Inapplicable(CONVENTION_ERROR): /ImproperIterator6.hasNext>#|()) {
lval i: R|kotlin/Int| = R|<local>/<iterator>|.R|/ImproperIterator6.next<Inapplicable(CONVENTION_ERROR): /ImproperIterator6.next>#|()
{
}
@@ -105,7 +105,7 @@ fun test(
for (i in notRange6);
for (i in <!CONDITION_TYPE_MISMATCH!>notRange7<!>);
for (i in <!HAS_NEXT_MISSING!>notRange8<!>);
for (i in <!OPERATOR_MODIFIER_REQUIRED, OPERATOR_MODIFIER_REQUIRED, OPERATOR_MODIFIER_REQUIRED!>notRange9<!>);
for (i in <!OPERATOR_MODIFIER_REQUIRED!>notRange9<!>);
for (i in range0);
for (i in range1);
}
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.fir.expressions.FirFunctionCall
import org.jetbrains.kotlin.fir.expressions.FirWhileLoop
import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference
import org.jetbrains.kotlin.fir.references.isError
import org.jetbrains.kotlin.fir.resolve.calls.OperatorCallOfNonOperatorFunction
import org.jetbrains.kotlin.fir.resolve.calls.UnsafeCall
import org.jetbrains.kotlin.fir.resolve.diagnostics.*
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
@@ -106,10 +107,13 @@ object FirForLoopChecker : FirBlockChecker(MppCheckerKind.Common) {
when {
calleeReference.isError() -> {
when (val diagnostic = calleeReference.diagnostic) {
is ConeAmbiguityError -> if (diagnostic.applicability.isSuccess) {
reporter.reportOn(reportSource, ambiguityFactory, diagnostic.candidates.map { it.symbol }, context)
} else if (noneApplicableFactory != null) {
reporter.reportOn(reportSource, noneApplicableFactory, diagnostic.candidates.map { it.symbol }, context)
is ConeAmbiguityError -> {
reporter.reportOn(
reportSource,
noneApplicableFactory ?: ambiguityFactory,
diagnostic.candidates.map { it.symbol },
context
)
}
is ConeUnresolvedNameError -> {
reporter.reportOn(reportSource, missingFactory, context)
@@ -132,17 +136,25 @@ object FirForLoopChecker : FirBlockChecker(MppCheckerKind.Common) {
is ConeInapplicableCandidateError -> {
if (unsafeCallFactory != null || noneApplicableFactory != null) {
diagnostic.candidate.diagnostics.filter { it.applicability == diagnostic.applicability }.forEach {
if (it is UnsafeCall) {
if (unsafeCallFactory != null) {
when (it) {
is UnsafeCall -> {
if (unsafeCallFactory != null) {
reporter.reportOn(
reportSource, unsafeCallFactory, context
)
} else {
reporter.reportOn(
reportSource, noneApplicableFactory!!, listOf(diagnostic.candidate.symbol), context
)
}
return true
}
is OperatorCallOfNonOperatorFunction -> {
val symbol = it.function
reporter.reportOn(
reportSource, unsafeCallFactory, context
)
} else {
reporter.reportOn(
reportSource, noneApplicableFactory!!, listOf(diagnostic.candidate.symbol), context
reportSource, OPERATOR_MODIFIER_REQUIRED, symbol, symbol.name.asString(), context
)
}
return true
}
}
}
@@ -1172,6 +1172,7 @@ class LightTreeRawFirExpressionBuilder(
name = OperatorNameConventions.ITERATOR
}
explicitReceiver = calculatedRangeExpression
origin = FirFunctionCallOrigin.Operator
}
)
statements += iteratorVal
@@ -1184,6 +1185,7 @@ class LightTreeRawFirExpressionBuilder(
name = OperatorNameConventions.HAS_NEXT
}
explicitReceiver = generateResolvedAccessExpression(rangeSource, iteratorVal)
origin = FirFunctionCallOrigin.Operator
}
// break/continue in the for loop condition will refer to an outer loop if any.
// So, prepare the loop target after building the condition.
@@ -1204,6 +1206,7 @@ class LightTreeRawFirExpressionBuilder(
name = OperatorNameConventions.NEXT
}
explicitReceiver = generateResolvedAccessExpression(rangeSource, iteratorVal)
origin = FirFunctionCallOrigin.Operator
},
valueParameter.returnTypeRef,
extractedAnnotations = valueParameter.annotations
@@ -2743,6 +2743,7 @@ open class PsiRawFirBuilder(
name = OperatorNameConventions.ITERATOR
}
explicitReceiver = rangeExpression
origin = FirFunctionCallOrigin.Operator
},
)
statements += iteratorVal
@@ -2755,6 +2756,7 @@ open class PsiRawFirBuilder(
name = OperatorNameConventions.HAS_NEXT
}
explicitReceiver = generateResolvedAccessExpression(rangeSource, iteratorVal)
origin = FirFunctionCallOrigin.Operator
}
// break/continue in the for loop condition will refer to an outer loop if any.
// So, prepare the loop target after building the condition.
@@ -2776,6 +2778,7 @@ open class PsiRawFirBuilder(
name = OperatorNameConventions.NEXT
}
explicitReceiver = generateResolvedAccessExpression(rangeSource, iteratorVal)
origin = FirFunctionCallOrigin.Operator
},
typeRef = ktParameter.typeReference.toFirOrImplicitType(),
extractedAnnotations = ktParameter.modifierList?.annotationEntries?.map { it.convert<FirAnnotation>() },