Cannot transform to flatMap if old working variable used in the nested loop

This commit is contained in:
Valentin Kipyatkov
2016-04-05 20:46:42 +03:00
parent 50bd766992
commit d7762778a2
2 changed files with 14 additions and 2 deletions
@@ -64,12 +64,14 @@ class FlatMapTransformation(
val iterableType = FuzzyType(builtIns.iterableType, builtIns.iterable.declaredTypeParameters)
if (iterableType.checkIsSuperTypeOf(nestedSequenceType) == null) return null
val nestedLoopBody = nestedLoop.body ?: return null
if (state.workingVariable.hasUsages(listOf(nestedLoopBody))) return null // workingVariable is still needed - cannot transform
val newWorkingVariable = nestedLoop.loopParameter ?: return null
val loopBody = nestedLoop.body ?: return null
val transformation = FlatMapTransformation(state.workingVariable, transform)
val newState = state.copy(
innerLoop = nestedLoop,
statements = listOf(loopBody),
statements = listOf(nestedLoopBody),
workingVariable = newWorkingVariable
)
return SequenceTransformationMatch(transformation, newState)
@@ -0,0 +1,10 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
fun foo(list: List<String>): String? {
<caret>for (s in list) {
for (line in s.lines()) {
if (line.isNotBlank() && line.length < s.length / 10) return line
}
}
return null
}