From 58e4157c4dfb046320de46924827fffb6f736685 Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Sat, 9 Jun 2012 13:09:48 +0400 Subject: [PATCH] KT-2234 'period!!' has type Int? #KT-2234 fixed --- .../BasicExpressionTypingVisitor.java | 24 ++++++------------- .../testData/diagnostics/tests/Constants.jet | 2 +- .../tests/nullabilityAndAutoCasts/kt2234.jet | 19 +++++++++++++++ 3 files changed, 27 insertions(+), 18 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/nullabilityAndAutoCasts/kt2234.jet diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java index 61b97cf14f1..131c84d9432 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java @@ -194,22 +194,18 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { if (right != null) { JetType targetType = context.expressionTypingServices.getTypeResolver().resolveType(context.scope, right, context.trace, true); - if (isTypeFlexible(expression.getLeft())) { + if (isTypeFlexible(expression.getLeft()) || expression.getOperationSign().getReferencedNameElementType() == JetTokens.COLON) { TemporaryBindingTrace temporaryTraceWithExpectedType = TemporaryBindingTrace.create(context.trace); - boolean success = checkBinaryWithTypeRHS(expression, context, targetType, targetType, temporaryTraceWithExpectedType); + boolean success = checkBinaryWithTypeRHS(expression, context.replaceBindingTrace(temporaryTraceWithExpectedType).replaceExpectedType(targetType), targetType); if (success) { temporaryTraceWithExpectedType.commit(); } else { - TemporaryBindingTrace temporaryTraceWithoutExpectedType = TemporaryBindingTrace.create(context.trace); - checkBinaryWithTypeRHS(expression, context, targetType, NO_EXPECTED_TYPE, temporaryTraceWithoutExpectedType); - temporaryTraceWithoutExpectedType.commit(); + checkBinaryWithTypeRHS(expression, context.replaceExpectedType(NO_EXPECTED_TYPE), targetType); } } else { - TemporaryBindingTrace temporaryTraceWithoutExpectedType = TemporaryBindingTrace.create(context.trace); - checkBinaryWithTypeRHS(expression, context, targetType, NO_EXPECTED_TYPE, temporaryTraceWithoutExpectedType); - temporaryTraceWithoutExpectedType.commit(); + checkBinaryWithTypeRHS(expression, context.replaceExpectedType(NO_EXPECTED_TYPE), targetType); } IElementType operationType = expression.getOperationSign().getReferencedNameElementType(); @@ -221,10 +217,9 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { return DataFlowUtils.checkType(result, expression, context); } - private boolean checkBinaryWithTypeRHS(JetBinaryExpressionWithTypeRHS expression, ExpressionTypingContext context, @NotNull JetType targetType, @NotNull JetType expectedType, TemporaryBindingTrace temporaryTrace) { - ExpressionTypingContext newContext = context.replaceExpectedType(expectedType).replaceBindingTrace(temporaryTrace); + private boolean checkBinaryWithTypeRHS(JetBinaryExpressionWithTypeRHS expression, ExpressionTypingContext context, @NotNull JetType targetType) { - JetType actualType = facade.getType(expression.getLeft(), newContext); + JetType actualType = facade.getType(expression.getLeft(), context); if (actualType == null) return false; JetSimpleNameExpression operationSign = expression.getOperationSign(); @@ -782,15 +777,10 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { // Special case for expr!! if (operationType == JetTokens.EXCLEXCL) { - JetType result; if (isKnownToBeNotNull(baseExpression, context)) { context.trace.report(UNNECESSARY_NOT_NULL_ASSERTION.on(operationSign, type)); - result = type; } - else { - result = TypeUtils.makeNotNullable(type); - } - return DataFlowUtils.checkType(result, expression, context); + return DataFlowUtils.checkType(TypeUtils.makeNotNullable(type), expression, context); } // Conventions for unary operations diff --git a/compiler/testData/diagnostics/tests/Constants.jet b/compiler/testData/diagnostics/tests/Constants.jet index 823fd98d279..b0993ea0d0f 100644 --- a/compiler/testData/diagnostics/tests/Constants.jet +++ b/compiler/testData/diagnostics/tests/Constants.jet @@ -1,7 +1,7 @@ fun test() { 1 : Byte 1 : Int - 1 : Double + 1 : Double 1 as Byte 1 as Int 1 as Double diff --git a/compiler/testData/diagnostics/tests/nullabilityAndAutoCasts/kt2234.jet b/compiler/testData/diagnostics/tests/nullabilityAndAutoCasts/kt2234.jet new file mode 100644 index 00000000000..b44cb01a354 --- /dev/null +++ b/compiler/testData/diagnostics/tests/nullabilityAndAutoCasts/kt2234.jet @@ -0,0 +1,19 @@ +package a + +//KT-2234 'period!!' has type Int? + +fun main(args : Array) { + val d : Long = 1 + val period : Int? = null + if (period != null) #(d, period!! : Int) else #(d, 1) + if (period != null) #(d, period : Int) else #(d, 1) +} + +fun foo() { + val x : Int? = 3 + if (x != null) { + val u = x!! : Int + val y = x : Int + val z : Int = y + } +}