If conditions typechecked
This commit is contained in:
@@ -370,6 +370,14 @@ public class JetTypeInferrer {
|
|||||||
@Override
|
@Override
|
||||||
public void visitIfExpression(JetIfExpression expression) {
|
public void visitIfExpression(JetIfExpression expression) {
|
||||||
// TODO : check condition type
|
// TODO : check condition type
|
||||||
|
JetExpression condition = expression.getCondition();
|
||||||
|
if (condition != null) {
|
||||||
|
JetType conditionType = getType(scope, condition, false);
|
||||||
|
|
||||||
|
if (conditionType != null && !isBoolean(conditionType)) {
|
||||||
|
semanticServices.getErrorHandler().structuralError(condition.getNode(), "Condition must be of type Boolean, but was of type " + conditionType);
|
||||||
|
}
|
||||||
|
}
|
||||||
// TODO : change types according to is and nullability checks
|
// TODO : change types according to is and nullability checks
|
||||||
JetExpression elseBranch = expression.getElse();
|
JetExpression elseBranch = expression.getElse();
|
||||||
if (elseBranch == null) {
|
if (elseBranch == null) {
|
||||||
@@ -618,8 +626,7 @@ public class JetTypeInferrer {
|
|||||||
private JetType assureBooleanResult(JetSimpleNameExpression operationSign, String name, JetType resultType) {
|
private JetType assureBooleanResult(JetSimpleNameExpression operationSign, String name, JetType resultType) {
|
||||||
if (resultType != null) {
|
if (resultType != null) {
|
||||||
// TODO : Relax?
|
// TODO : Relax?
|
||||||
TypeConstructor booleanTypeConstructor = semanticServices.getStandardLibrary().getBoolean().getTypeConstructor();
|
if (!isBoolean(resultType)) {
|
||||||
if (!resultType.getConstructor().equals(booleanTypeConstructor)) {
|
|
||||||
semanticServices.getErrorHandler().structuralError(operationSign.getNode(), "'" + name + "' must return Boolean but returns " + resultType);
|
semanticServices.getErrorHandler().structuralError(operationSign.getNode(), "'" + name + "' must return Boolean but returns " + resultType);
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
@@ -629,6 +636,11 @@ public class JetTypeInferrer {
|
|||||||
return resultType;
|
return resultType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isBoolean(@NotNull JetType resultType) {
|
||||||
|
TypeConstructor booleanTypeConstructor = semanticServices.getStandardLibrary().getBoolean().getTypeConstructor();
|
||||||
|
return resultType.getConstructor().equals(booleanTypeConstructor);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitArrayAccessExpression(JetArrayAccessExpression expression) {
|
public void visitArrayAccessExpression(JetArrayAccessExpression expression) {
|
||||||
JetExpression arrayExpression = expression.getArrayExpression();
|
JetExpression arrayExpression = expression.getArrayExpression();
|
||||||
|
|||||||
Reference in New Issue
Block a user