KT-2234 'period!!' has type Int?
#KT-2234 fixed
This commit is contained in:
+7
-17
@@ -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
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
fun test() {
|
||||
1 : Byte
|
||||
1 : Int
|
||||
<!TYPE_MISMATCH, TYPE_MISMATCH!>1<!> : Double
|
||||
<!TYPE_MISMATCH!>1<!> : Double
|
||||
1 <!USELESS_CAST!>as<!> Byte
|
||||
1 <!USELESS_CAST!>as<!> Int
|
||||
<!ERROR_COMPILE_TIME_VALUE!>1<!> <!CAST_NEVER_SUCCEEDS!>as<!> Double
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package a
|
||||
|
||||
//KT-2234 'period!!' has type Int?
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
val d : Long = 1
|
||||
val period : Int? = null
|
||||
if (period != null) #(d, period<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!> : Int) else #(d, 1)
|
||||
if (period != null) #(d, period : Int) else #(d, 1)
|
||||
}
|
||||
|
||||
fun foo() {
|
||||
val x : Int? = 3
|
||||
if (x != null) {
|
||||
val <!UNUSED_VARIABLE!>u<!> = x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!> : Int
|
||||
val y = x : Int
|
||||
val <!UNUSED_VARIABLE!>z<!> : Int = y
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user