K2: implement FirForLoopStatementAssignmentChecker

#KT-60006 Fixed
This commit is contained in:
Tomas Husak
2024-02-20 17:34:49 +00:00
committed by Space Team
parent 9eb6646576
commit ec167d4d42
6 changed files with 57 additions and 20 deletions
@@ -106,6 +106,7 @@ object CommonExpressionCheckers : ExpressionCheckers() {
override val loopExpressionCheckers: Set<FirLoopExpressionChecker>
get() = setOf(
FirLoopConditionChecker,
FirForLoopStatementAssignmentChecker
)
override val loopJumpCheckers: Set<FirLoopJumpChecker>
@@ -128,7 +129,7 @@ object CommonExpressionCheckers : ExpressionCheckers() {
get() = setOf(
FirForLoopChecker,
FirConflictsExpressionChecker,
FirSingleNamedFunctionChecker
FirSingleNamedFunctionChecker,
)
override val checkNotNullCallCheckers: Set<FirCheckNotNullCallChecker>
@@ -0,0 +1,46 @@
/*
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.fir.analysis.checkers.expression
import org.jetbrains.kotlin.KtFakeSourceElementKind
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.diagnostics.reportOn
import org.jetbrains.kotlin.fir.analysis.checkers.MppCheckerKind
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
import org.jetbrains.kotlin.fir.declarations.FirDeclarationOrigin
import org.jetbrains.kotlin.fir.declarations.FirProperty
import org.jetbrains.kotlin.fir.declarations.utils.FirScriptCustomizationKind
import org.jetbrains.kotlin.fir.expressions.FirBlock
import org.jetbrains.kotlin.fir.expressions.FirErrorExpression
import org.jetbrains.kotlin.fir.expressions.FirLoop
import org.jetbrains.kotlin.fir.expressions.FirReturnExpression
object FirForLoopStatementAssignmentChecker : FirLoopExpressionChecker(MppCheckerKind.Common) {
override fun check(expression: FirLoop, context: CheckerContext, reporter: DiagnosticReporter) {
// Checks the pattern for desugared for loop.
val parent = if (context.containingElements.size >= 2) context.containingElements[context.containingElements.size - 2] else return
if (parent.source?.kind != KtFakeSourceElementKind.DesugaredForLoop) return
val grandParent = if (context.containingElements.size >= 3)
context.containingElements[context.containingElements.size - 3]
else
return
if (// It is used as a statement
grandParent is FirBlock
// It is used as a single statement in the method body
|| (grandParent is FirReturnExpression && grandParent.source?.kind == KtFakeSourceElementKind.ImplicitReturn.FromLastStatement)
// It is used in a kotlin script as a last statement
|| (grandParent is FirProperty && (grandParent.origin as? FirDeclarationOrigin.ScriptCustomization)?.kind == FirScriptCustomizationKind.RESULT_PROPERTY)
// There was a fail before (for example, using two labels before the for loop)
|| (grandParent is FirErrorExpression)
)
return
reporter.reportOn(expression.source, FirErrors.EXPRESSION_EXPECTED, context)
}
}