From e515d7f773e72e5c40f1eae7f4b6a13e1d328a25 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 25 Jul 2016 12:43:14 +0300 Subject: [PATCH] Data flow analysis: unnecessary assignment removed (related to complex expressions) --- .../BasicExpressionTypingVisitor.java | 13 ++----------- .../ControlStructureTypingVisitor.java | 7 ------- .../expressions/ExpressionTypingServices.java | 7 ------- .../expressions/PatternMatchingTypingVisitor.kt | 16 +++------------- 4 files changed, 5 insertions(+), 38 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java index 6b1efdff635..de672415502 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java @@ -172,14 +172,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { if (innerExpression == null) { return TypeInfoFactoryKt.noTypeInfo(context); } - KotlinTypeInfo result = facade.getTypeInfo(innerExpression, context.replaceScope(context.scope)); - KotlinType resultType = result.getType(); - if (resultType != null) { - DataFlowValue innerValue = DataFlowValueFactory.createDataFlowValue(innerExpression, resultType, context); - DataFlowValue resultValue = DataFlowValueFactory.createDataFlowValue(expression, resultType, context); - result = result.replaceDataFlowInfo(result.getDataFlowInfo().assign(resultValue, innerValue)); - } - return result; + return facade.getTypeInfo(innerExpression, context.replaceScope(context.scope)); } @Override @@ -1235,10 +1228,8 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { } } DataFlowValue resultValue = DataFlowValueFactory.createDataFlowValue(expression, type, context); - dataFlowInfo = dataFlowInfo.assign(resultValue, leftValue).disequate(resultValue, nullValue); + dataFlowInfo = dataFlowInfo.disequate(resultValue, nullValue); if (!jumpInRight) { - DataFlowValue rightValue = DataFlowValueFactory.createDataFlowValue(right, rightType, context); - rightDataFlowInfo = rightDataFlowInfo.assign(resultValue, rightValue); dataFlowInfo = dataFlowInfo.or(rightDataFlowInfo); } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingVisitor.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingVisitor.java index 2ccf238c34e..13d84e543ed 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingVisitor.java @@ -154,13 +154,6 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor { KotlinType elseType = elseTypeInfo.getType(); DataFlowInfo thenDataFlowInfo = thenTypeInfo.getDataFlowInfo(); DataFlowInfo elseDataFlowInfo = elseTypeInfo.getDataFlowInfo(); - if (resultType != null && thenType != null && elseType != null) { - DataFlowValue resultValue = DataFlowValueFactory.createDataFlowValue(ifExpression, resultType, context); - DataFlowValue thenValue = DataFlowValueFactory.createDataFlowValue(thenBranch, thenType, context); - thenDataFlowInfo = thenDataFlowInfo.assign(resultValue, thenValue); - DataFlowValue elseValue = DataFlowValueFactory.createDataFlowValue(elseBranch, elseType, context); - elseDataFlowInfo = elseDataFlowInfo.assign(resultValue, elseValue); - } loopBreakContinuePossible |= thenTypeInfo.getJumpOutPossible() || elseTypeInfo.getJumpOutPossible(); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingServices.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingServices.java index 34d63ce3aad..f2a631430f2 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingServices.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingServices.java @@ -246,13 +246,6 @@ public class ExpressionTypingServices { result = getTypeOfLastExpressionInBlock( statementExpression, newContext.replaceExpectedType(context.expectedType), coercionStrategyForLastExpression, blockLevelVisitor); - if (result.getType() != null && statementExpression.getParent() instanceof KtBlockExpression) { - DataFlowValue lastExpressionValue = DataFlowValueFactory.createDataFlowValue( - statementExpression, result.getType(), context); - DataFlowValue blockExpressionValue = DataFlowValueFactory.createDataFlowValue( - (KtBlockExpression) statementExpression.getParent(), result.getType(), context); - result = result.replaceDataFlowInfo(result.getDataFlowInfo().assign(blockExpressionValue, lastExpressionValue)); - } } else { result = blockLevelVisitor diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/PatternMatchingTypingVisitor.kt b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/PatternMatchingTypingVisitor.kt index d5f2479d1e0..7f215eb5ca1 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/PatternMatchingTypingVisitor.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/PatternMatchingTypingVisitor.kt @@ -86,10 +86,8 @@ class PatternMatchingTypingVisitor internal constructor(facade: ExpressionTyping val dataFlowInfoForEntries = analyzeConditionsInWhenEntries(expression, contextAfterSubject, subjectDataFlowValue, subjectType) val whenReturnType = inferTypeForWhenExpression(expression, contextWithExpectedType, contextAfterSubject, dataFlowInfoForEntries) - val whenResultValue = whenReturnType?.let { DataFlowValueFactory.createDataFlowValue(expression, it, contextAfterSubject) } - val branchesTypeInfo = - joinWhenExpressionBranches(expression, contextAfterSubject, whenReturnType, jumpOutPossibleInSubject, whenResultValue) + val branchesTypeInfo = joinWhenExpressionBranches(expression, contextAfterSubject, whenReturnType, jumpOutPossibleInSubject) val isExhaustive = WhenChecker.isWhenExhaustive(expression, trace) @@ -174,8 +172,7 @@ class PatternMatchingTypingVisitor internal constructor(facade: ExpressionTyping expression: KtWhenExpression, contextAfterSubject: ExpressionTypingContext, resultType: KotlinType?, - jumpOutPossibleInSubject: Boolean, - whenResultValue: DataFlowValue? + jumpOutPossibleInSubject: Boolean ): KotlinTypeInfo { val bindingContext = contextAfterSubject.trace.bindingContext @@ -192,14 +189,7 @@ class PatternMatchingTypingVisitor internal constructor(facade: ExpressionTyping errorTypeExistInBranch = true } - val entryDataFlowInfo = - if (whenResultValue != null && entryType != null) { - val entryValue = DataFlowValueFactory.createDataFlowValue(entryExpression, entryType, contextAfterSubject) - entryTypeInfo.dataFlowInfo.assign(whenResultValue, entryValue) - } - else { - entryTypeInfo.dataFlowInfo - } + val entryDataFlowInfo = entryTypeInfo.dataFlowInfo currentDataFlowInfo = if (entryType != null && KotlinBuiltIns.isNothing(entryType))