From 3f4e2514ea090043abe96430023d38e244f75c7b Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 22 Jun 2012 13:18:45 +0400 Subject: [PATCH] extract DataFlowInfo from when-condition + more tests for kt2146 --- .../PatternMatchingTypingVisitor.java | 8 ++++- .../tests/nullabilityAndAutoCasts/kt2146.jet | 35 +++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) 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 aff2b589b24..f637d1ddd55 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 @@ -263,13 +263,19 @@ public class PatternMatchingTypingVisitor extends ExpressionTypingVisitor { public void visitExpressionPattern(JetExpressionPattern pattern) { JetExpression expression = pattern.getExpression(); if (expression == null) return; - JetType type = facade.getTypeInfo(expression, context.replaceScope(scopeToExtend)).getType(); + JetTypeInfo typeInfo = facade.getTypeInfo(expression, context.replaceScope(scopeToExtend)); + JetType type = typeInfo.getType(); if (type == null) return; if (conditionExpected) { JetType booleanType = JetStandardLibrary.getInstance().getBooleanType(); if (!JetTypeChecker.INSTANCE.equalTypes(booleanType, type)) { context.trace.report(TYPE_MISMATCH_IN_CONDITION.on(pattern, type)); } + else { + DataFlowInfo ifInfo = DataFlowUtils.extractDataFlowInfoFromCondition(expression, true, scopeToExtend, context); + DataFlowInfo elseInfo = DataFlowUtils.extractDataFlowInfoFromCondition(expression, false, null, context); + result.set(Pair.create(ifInfo, elseInfo)); + } return; } checkTypeCompatibility(type, subjectType, pattern); diff --git a/compiler/testData/diagnostics/tests/nullabilityAndAutoCasts/kt2146.jet b/compiler/testData/diagnostics/tests/nullabilityAndAutoCasts/kt2146.jet index e5e640267ec..579f1095ba5 100644 --- a/compiler/testData/diagnostics/tests/nullabilityAndAutoCasts/kt2146.jet +++ b/compiler/testData/diagnostics/tests/nullabilityAndAutoCasts/kt2146.jet @@ -1,9 +1,40 @@ //KT-2146 Nullability casts in when. package kt2146 -fun f(s : Int?) : Int { +fun f1(s: Int?): Int { return when (s) { null -> 3 - else -> s // type mismatch + else -> s + } +} + +fun f2(s: Int?): Int { + return when (s) { + is 4 -> s + is null -> s + else -> s + } +} + +fun f3(s: Int?): Int { + return when (s) { + is Int -> s + else -> s + } +} + +fun f4(s: Int?): Int { + return when { + s == 4 -> s + s == null -> s + else -> s + } +} + +fun f5(s: Int?): Int { + return when (s) { + s!! -> s + s -> s + else -> 0 } }