From 308e1362ab13b6769e36b02aa6c92c712a280f3d Mon Sep 17 00:00:00 2001 From: pyos Date: Tue, 20 Dec 2022 13:32:32 +0100 Subject: [PATCH] Minor: FIR DFA: fix code style --- .../analysis/cfa/FirCallsEffectAnalyzer.kt | 4 +-- .../util/PathAwareControlFlowGraphVisitor.kt | 32 +++++++++---------- .../PropertyInitializationInfoCollector.kt | 4 +-- .../FirPropertyInitializationChecker.kt | 8 ++--- .../checkers/extended/UnusedChecker.kt | 4 +-- .../kotlin/fir/resolve/dfa/cfg/CFGNode.kt | 4 ++- 6 files changed, 29 insertions(+), 27 deletions(-) diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/FirCallsEffectAnalyzer.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/FirCallsEffectAnalyzer.kt index 7ae7fbb65ac..4089ab79944 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/FirCallsEffectAnalyzer.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/FirCallsEffectAnalyzer.kt @@ -227,11 +227,11 @@ object FirCallsEffectAnalyzer : FirControlFlowChecker() { val functionalTypeSymbols: Set> ) : PathAwareControlFlowGraphVisitor() { companion object { - val EMPTY: PathAwareLambdaInvocationInfo = persistentMapOf(NormalPath to LambdaInvocationInfo.EMPTY) + private val EMPTY_INFO: PathAwareLambdaInvocationInfo = persistentMapOf(NormalPath to LambdaInvocationInfo.EMPTY) } override val emptyInfo: PathAwareLambdaInvocationInfo - get() = EMPTY + get() = EMPTY_INFO override fun visitFunctionCallNode( node: FunctionCallNode, diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/util/PathAwareControlFlowGraphVisitor.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/util/PathAwareControlFlowGraphVisitor.kt index 9c6c183077e..fc08b03336a 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/util/PathAwareControlFlowGraphVisitor.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/util/PathAwareControlFlowGraphVisitor.kt @@ -33,31 +33,31 @@ abstract class PathAwareControlFlowGraphVisitor> : open fun visitEdge(from: CFGNode<*>, to: CFGNode<*>, metadata: Edge, data: PathAwareControlFlowInfo): PathAwareControlFlowInfo { val label = metadata.label - return if (from is FinallyBlockExitNode) { + return when { // Finally exit is splitting labeled flow. So if we have data for different labels, then // data for each only goes along an edge with the same label, and the leftover data // is forwarded along an UncaughtExceptionPath edge, if any, to the next finally block. - if (label == UncaughtExceptionPath) { - data.mutate { - for (other in from.followingNodes) { - val otherLabel = from.edgeTo(other).label - if (otherLabel != UncaughtExceptionPath) { - it.remove(otherLabel) + from is FinallyBlockExitNode -> { + if (label == UncaughtExceptionPath) { + data.mutate { + for (other in from.followingNodes) { + val otherLabel = from.edgeTo(other).label + if (otherLabel != UncaughtExceptionPath) { + it.remove(otherLabel) + } } - } - }.ifEmpty { emptyInfo } // there should always be UncaughtExceptionPath data, but just in case - } else { - val info = data[label] ?: return emptyInfo - persistentMapOf(NormalPath to info) + }.ifEmpty { emptyInfo } // there should always be UncaughtExceptionPath data, but just in case + } else { + val info = data[label] ?: return emptyInfo + persistentMapOf(NormalPath to info) + } } - } else if (label == NormalPath) { // A normal path forwards all data. (Non-normal paths should only have data in finally blocks.) - data - } else { + label == NormalPath -> data // Labeled edge from a jump statement to a `finally` block forks flow. Usually we'd only have // NormalPath data here, but technically it's possible (though questionable) to jump from a `finally` // (discarding the exception or aborting a previous jump in the process) so merge all data just in case. - persistentMapOf(label to data.values.reduce { a, b -> a.merge(b) }) + else -> persistentMapOf(label to data.values.reduce { a, b -> a.merge(b) }) } } diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/util/PropertyInitializationInfoCollector.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/util/PropertyInitializationInfoCollector.kt index 89385226562..3109761d8ba 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/util/PropertyInitializationInfoCollector.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/cfa/util/PropertyInitializationInfoCollector.kt @@ -26,11 +26,11 @@ class PropertyInitializationInfoCollector( private val declaredVariableCollector: DeclaredVariableCollector = DeclaredVariableCollector(), ) : PathAwareControlFlowGraphVisitor() { companion object { - val EMPTY: PathAwarePropertyInitializationInfo = persistentMapOf(NormalPath to PropertyInitializationInfo.EMPTY) + private val EMPTY_INFO: PathAwarePropertyInitializationInfo = persistentMapOf(NormalPath to PropertyInitializationInfo.EMPTY) } override val emptyInfo: PathAwarePropertyInitializationInfo - get() = EMPTY + get() = EMPTY_INFO override fun visitVariableAssignmentNode( node: VariableAssignmentNode, diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirPropertyInitializationChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirPropertyInitializationChecker.kt index 4f695c85bc7..cf142552897 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirPropertyInitializationChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirPropertyInitializationChecker.kt @@ -18,7 +18,7 @@ import org.jetbrains.kotlin.fir.visitors.FirVisitorVoid object FirPropertyInitializationChecker : FirRegularClassChecker() { override fun check(declaration: FirRegularClass, context: CheckerContext, reporter: DiagnosticReporter) { - val interestingProperties = mutableSetOf() + val declaredLater = mutableSetOf() val visitor = object : FirVisitorVoid() { override fun visitElement(element: FirElement) = element.acceptChildren(this) @@ -39,17 +39,17 @@ object FirPropertyInitializationChecker : FirRegularClassChecker() { override fun visitVariableAssignment(variableAssignment: FirVariableAssignment) { variableAssignment.acceptChildren(this) val propertySymbol = variableAssignment.lValue.toResolvedCallableSymbol() as? FirPropertySymbol ?: return - if (propertySymbol !in interestingProperties) return + if (propertySymbol !in declaredLater) return reporter.reportOn(variableAssignment.lValue.source, FirErrors.INITIALIZATION_BEFORE_DECLARATION, propertySymbol, context) } } for (member in declaration.declarations.asReversed()) { - if (interestingProperties.isNotEmpty()) { + if (declaredLater.isNotEmpty()) { member.accept(visitor) } if (member is FirProperty) { - interestingProperties.add(member.symbol) + declaredLater.add(member.symbol) } } } diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/extended/UnusedChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/extended/UnusedChecker.kt index cc685956ec9..8ba3d32b895 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/extended/UnusedChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/extended/UnusedChecker.kt @@ -158,11 +158,11 @@ object UnusedChecker : AbstractFirPropertyInitializationChecker() { private val localProperties: Set ) : PathAwareControlFlowGraphVisitor() { companion object { - val EMPTY: PathAwareVariableStatusInfo = persistentMapOf(NormalPath to VariableStatusInfo.EMPTY) + private val EMPTY_INFO: PathAwareVariableStatusInfo = persistentMapOf(NormalPath to VariableStatusInfo.EMPTY) } override val emptyInfo: PathAwareVariableStatusInfo - get() = EMPTY + get() = EMPTY_INFO fun getData(graph: ControlFlowGraph): Map, PathAwareVariableStatusInfo> { return graph.collectDataForNode(TraverseDirection.Backward, this) diff --git a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/CFGNode.kt b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/CFGNode.kt index f2c40efb36a..d022c4a286f 100644 --- a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/CFGNode.kt +++ b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/CFGNode.kt @@ -229,6 +229,7 @@ class AnonymousFunctionExpressionNode(owner: ControlFlowGraph, override val fir: class ClassEnterNode(owner: ControlFlowGraph, override val fir: FirClass, level: Int) : CFGNodeWithSubgraphs(owner, level), GraphEnterNodeMarker { + @set:CfgInternals override lateinit var subGraphs: List override fun accept(visitor: ControlFlowGraphVisitor, data: D): R { @@ -239,7 +240,8 @@ class ClassEnterNode(owner: ControlFlowGraph, override val fir: FirClass, level: class ClassExitNode(owner: ControlFlowGraph, override val fir: FirClass, level: Int) : CFGNodeWithSubgraphs(owner, level), GraphExitNodeMarker, UnionNodeMarker { - lateinit override var subGraphs: List + @set:CfgInternals + override lateinit var subGraphs: List override fun accept(visitor: ControlFlowGraphVisitor, data: D): R { return visitor.visitClassExitNode(this, data)