One more case for nullability: comparison to a non-null expression
This commit is contained in:
@@ -1381,24 +1381,39 @@ public class JetTypeInferrer {
|
|||||||
|| operationToken == JetTokens.EXCLEQEQEQ) {
|
|| operationToken == JetTokens.EXCLEQEQEQ) {
|
||||||
JetExpression left = expression.getLeft();
|
JetExpression left = expression.getLeft();
|
||||||
JetExpression right = expression.getRight();
|
JetExpression right = expression.getRight();
|
||||||
if (left instanceof JetConstantExpression) {
|
if (right == null) return;
|
||||||
|
|
||||||
|
if (!(left instanceof JetSimpleNameExpression)) {
|
||||||
JetExpression tmp = left;
|
JetExpression tmp = left;
|
||||||
left = right;
|
left = right;
|
||||||
right = tmp;
|
right = tmp;
|
||||||
|
|
||||||
|
if (!(left instanceof JetSimpleNameExpression)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(left instanceof JetSimpleNameExpression)) {
|
VariableDescriptor variableDescriptor = getVariableDescriptorFromSimpleName(left);
|
||||||
return;
|
|
||||||
}
|
|
||||||
JetSimpleNameExpression nameExpression = (JetSimpleNameExpression) left;
|
|
||||||
DeclarationDescriptor declarationDescriptor = trace.getBindingContext().resolveReferenceExpression(nameExpression);
|
|
||||||
if (!(declarationDescriptor instanceof VariableDescriptor)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
VariableDescriptor variableDescriptor = (VariableDescriptor) declarationDescriptor;
|
|
||||||
|
|
||||||
// TODO : validate that DF makes sense for this variable: local, val, internal w/backing field, etc
|
// TODO : validate that DF makes sense for this variable: local, val, internal w/backing field, etc
|
||||||
|
|
||||||
|
// Comparison to a non-null expression
|
||||||
|
JetType rhsType = trace.getBindingContext().getExpressionType(right);
|
||||||
|
if (rhsType != null && !rhsType.isNullable()) {
|
||||||
|
extendDataFlowWithNullComparison(operationToken, variableDescriptor, !conditionValue);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
VariableDescriptor rightVariable = getVariableDescriptorFromSimpleName(right);
|
||||||
|
if (rightVariable != null) {
|
||||||
|
JetType lhsType = trace.getBindingContext().getExpressionType(left);
|
||||||
|
if (lhsType != null && !lhsType.isNullable()) {
|
||||||
|
extendDataFlowWithNullComparison(operationToken, rightVariable, !conditionValue);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comparison to 'null'
|
||||||
if (!(right instanceof JetConstantExpression)) {
|
if (!(right instanceof JetConstantExpression)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -1407,12 +1422,16 @@ public class JetTypeInferrer {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (operationToken == JetTokens.EQEQ || operationToken == JetTokens.EQEQEQ) {
|
extendDataFlowWithNullComparison(operationToken, variableDescriptor, conditionValue);
|
||||||
result[0] = dataFlowInfo.equalsToNull(variableDescriptor, !conditionValue);
|
}
|
||||||
}
|
}
|
||||||
else if (operationToken == JetTokens.EXCLEQ || operationToken == JetTokens.EXCLEQEQEQ) {
|
|
||||||
result[0] = dataFlowInfo.equalsToNull(variableDescriptor, conditionValue);
|
private void extendDataFlowWithNullComparison(IElementType operationToken, VariableDescriptor variableDescriptor, boolean equalsToNull) {
|
||||||
}
|
if (operationToken == JetTokens.EQEQ || operationToken == JetTokens.EQEQEQ) {
|
||||||
|
result[0] = dataFlowInfo.equalsToNull(variableDescriptor, !equalsToNull);
|
||||||
|
}
|
||||||
|
else if (operationToken == JetTokens.EXCLEQ || operationToken == JetTokens.EXCLEQEQEQ) {
|
||||||
|
result[0] = dataFlowInfo.equalsToNull(variableDescriptor, equalsToNull);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -255,3 +255,20 @@ fun f7(s : String?, t : String?) {
|
|||||||
t?.get(0)
|
t?.get(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun f8(b : String?, a : String) {
|
||||||
|
b?.get(0)
|
||||||
|
if (b == a) {
|
||||||
|
b.get(0);
|
||||||
|
}
|
||||||
|
b?.get(0)
|
||||||
|
if (a == b) {
|
||||||
|
b.get(0)
|
||||||
|
}
|
||||||
|
if (a != b) {
|
||||||
|
b?.get(0)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
b.get(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user