KT-1075 No type check for 'in range' condition in 'when' expression
This commit is contained in:
@@ -279,6 +279,13 @@ public interface Errors {
|
||||
return element.getWhenKeywordElement().getTextRange();
|
||||
}
|
||||
};
|
||||
SimpleDiagnosticFactoryWithPsiElement<JetWhenConditionInRange> TYPE_MISMATCH_IN_RANGE = new SimpleDiagnosticFactoryWithPsiElement<JetWhenConditionInRange>(ERROR, "Type mismatch: incompatible types of range and element checked in it") {
|
||||
@NotNull
|
||||
@Override
|
||||
public TextRange getTextRange(@NotNull JetWhenConditionInRange condition) {
|
||||
return condition.getOperationReference().getTextRange();
|
||||
}
|
||||
};
|
||||
SimpleDiagnosticFactory CYCLIC_INHERITANCE_HIERARCHY = SimpleDiagnosticFactory.create(ERROR, "There's a cycle in the inheritance hierarchy for this type");
|
||||
|
||||
SimpleDiagnosticFactory MANY_CLASSES_IN_SUPERTYPE_LIST = SimpleDiagnosticFactory.create(ERROR, "Only one class may appear in a supertype list");
|
||||
|
||||
+2
-1
@@ -814,7 +814,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
|
||||
return DataFlowUtils.checkType(result, expression, contextWithExpectedType);
|
||||
}
|
||||
|
||||
public void checkInExpression(JetElement callElement, @NotNull JetSimpleNameExpression operationSign, @Nullable JetExpression left, @NotNull JetExpression right, ExpressionTypingContext context) {
|
||||
public boolean checkInExpression(JetElement callElement, @NotNull JetSimpleNameExpression operationSign, @Nullable JetExpression left, @NotNull JetExpression right, ExpressionTypingContext context) {
|
||||
String name = "contains";
|
||||
ExpressionReceiver receiver = safeGetExpressionReceiver(facade, right, context.replaceExpectedType(NO_EXPECTED_TYPE));
|
||||
OverloadResolutionResults<FunctionDescriptor> functionDescriptor = context.resolveCallWithGivenNameToDescriptor(
|
||||
@@ -823,6 +823,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
|
||||
name);
|
||||
JetType containsType = functionDescriptor.isSuccess() ? functionDescriptor.getResultingDescriptor().getReturnType() : null;
|
||||
ensureBooleanResult(operationSign, name, containsType, context);
|
||||
return functionDescriptor.isSuccess();
|
||||
}
|
||||
|
||||
private void ensureNonemptyIntersectionOfOperandTypes(JetBinaryExpression expression, ExpressionTypingContext context) {
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ import org.jetbrains.jet.lang.types.JetType;
|
||||
@Nullable
|
||||
JetType getSelectorReturnType(@NotNull ReceiverDescriptor receiver, @Nullable ASTNode callOperationNode, @NotNull JetExpression selectorExpression, @NotNull ExpressionTypingContext context);
|
||||
|
||||
void checkInExpression(JetElement callElement, @NotNull JetSimpleNameExpression operationSign, @Nullable JetExpression left, @NotNull JetExpression right, ExpressionTypingContext context);
|
||||
boolean checkInExpression(JetElement callElement, @NotNull JetSimpleNameExpression operationSign, @Nullable JetExpression left, @NotNull JetExpression right, ExpressionTypingContext context);
|
||||
|
||||
void checkStatementType(@NotNull JetExpression expression, ExpressionTypingContext context);
|
||||
}
|
||||
|
||||
+2
-2
@@ -59,8 +59,8 @@ public class ExpressionTypingVisitorDispatcher extends JetVisitor<JetType, Expre
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkInExpression(JetElement callElement, @NotNull JetSimpleNameExpression operationSign, @Nullable JetExpression left, @NotNull JetExpression right, ExpressionTypingContext context) {
|
||||
basic.checkInExpression(callElement, operationSign, left, right, context);
|
||||
public boolean checkInExpression(JetElement callElement, @NotNull JetSimpleNameExpression operationSign, @Nullable JetExpression left, @NotNull JetExpression right, ExpressionTypingContext context) {
|
||||
return basic.checkInExpression(callElement, operationSign, left, right, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+3
-1
@@ -126,7 +126,9 @@ public class PatternMatchingTypingVisitor extends ExpressionTypingVisitor {
|
||||
facade.getType(rangeExpression, context);
|
||||
return;
|
||||
}
|
||||
facade.checkInExpression(condition, condition.getOperationReference(), subjectExpression, rangeExpression, context);
|
||||
if (!facade.checkInExpression(condition, condition.getOperationReference(), subjectExpression, rangeExpression, context)) {
|
||||
context.trace.report(TYPE_MISMATCH_IN_RANGE.on(condition));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package kt1075
|
||||
|
||||
//KT-1075 No type check for 'in range' condition in 'when' expression
|
||||
|
||||
fun foo(b: String) {
|
||||
if (<!TYPE_MISMATCH!>b<!> in 1..10) {} //type mismatch
|
||||
when (b) {
|
||||
<!TYPE_MISMATCH_IN_RANGE!>in<!> 1..10 -> 1 //no type mismatch, but it should be here
|
||||
else -> 2
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user