From d0a2ba5737e8e00ed841bf5eb587969984250df6 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Tue, 13 Nov 2012 22:27:22 +0400 Subject: [PATCH] Retain data flow info after is-expression --- .../types/expressions/PatternMatchingTypingVisitor.java | 9 ++++++--- .../tests/dataFlowInfoTraversal/IsExpression.kt | 6 ++++++ .../jet/checkers/JetDiagnosticsTestGenerated.java | 5 +++++ 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/dataFlowInfoTraversal/IsExpression.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/PatternMatchingTypingVisitor.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/PatternMatchingTypingVisitor.java index fe2e144abba..8a60f8ab2c0 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/PatternMatchingTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/PatternMatchingTypingVisitor.java @@ -49,13 +49,16 @@ public class PatternMatchingTypingVisitor extends ExpressionTypingVisitor { public JetTypeInfo visitIsExpression(JetIsExpression expression, ExpressionTypingContext contextWithExpectedType) { ExpressionTypingContext context = contextWithExpectedType.replaceExpectedType(TypeUtils.NO_EXPECTED_TYPE); JetExpression leftHandSide = expression.getLeftHandSide(); - JetType knownType = facade.safeGetTypeInfo(leftHandSide, context.replaceScope(context.scope)).getType(); + JetTypeInfo typeInfo = facade.safeGetTypeInfo(leftHandSide, context.replaceScope(context.scope)); + JetType knownType = typeInfo.getType(); + DataFlowInfo dataFlowInfo = typeInfo.getDataFlowInfo(); if (expression.getTypeRef() != null) { DataFlowValue dataFlowValue = DataFlowValueFactory.INSTANCE.createDataFlowValue(leftHandSide, knownType, context.trace.getBindingContext()); - DataFlowInfo newDataFlowInfo = checkTypeForIs(context, knownType, expression.getTypeRef(), dataFlowValue).thenInfo; + DataFlowInfo conditionInfo = checkTypeForIs(context, knownType, expression.getTypeRef(), dataFlowValue).thenInfo; + DataFlowInfo newDataFlowInfo = conditionInfo.and(dataFlowInfo); context.trace.record(BindingContext.DATAFLOW_INFO_AFTER_CONDITION, expression, newDataFlowInfo); } - return DataFlowUtils.checkType(KotlinBuiltIns.getInstance().getBooleanType(), expression, contextWithExpectedType, context.dataFlowInfo); + return DataFlowUtils.checkType(KotlinBuiltIns.getInstance().getBooleanType(), expression, contextWithExpectedType, dataFlowInfo); } @Override diff --git a/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/IsExpression.kt b/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/IsExpression.kt new file mode 100644 index 00000000000..202bb63b5f8 --- /dev/null +++ b/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/IsExpression.kt @@ -0,0 +1,6 @@ +fun foo(x: Number) { + if ((x as Int) is Int) { + x : Int + } + x : Int +} diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index 625bc7f9679..afac4cbb3fd 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -1256,6 +1256,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage doTest("compiler/testData/diagnostics/tests/dataFlowInfoTraversal/IfThenElseBothInvalid.kt"); } + @TestMetadata("IsExpression.kt") + public void testIsExpression() throws Exception { + doTest("compiler/testData/diagnostics/tests/dataFlowInfoTraversal/IsExpression.kt"); + } + @TestMetadata("MultiDeclaration.kt") public void testMultiDeclaration() throws Exception { doTest("compiler/testData/diagnostics/tests/dataFlowInfoTraversal/MultiDeclaration.kt");