提交 495f1ea3 编写于 作者: A Andrey Breslav

If conditions typechecked

上级 1792a71a
......@@ -370,6 +370,14 @@ public class JetTypeInferrer {
@Override
public void visitIfExpression(JetIfExpression expression) {
// 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
JetExpression elseBranch = expression.getElse();
if (elseBranch == null) {
......@@ -618,8 +626,7 @@ public class JetTypeInferrer {
private JetType assureBooleanResult(JetSimpleNameExpression operationSign, String name, JetType resultType) {
if (resultType != null) {
// TODO : Relax?
TypeConstructor booleanTypeConstructor = semanticServices.getStandardLibrary().getBoolean().getTypeConstructor();
if (!resultType.getConstructor().equals(booleanTypeConstructor)) {
if (!isBoolean(resultType)) {
semanticServices.getErrorHandler().structuralError(operationSign.getNode(), "'" + name + "' must return Boolean but returns " + resultType);
return null;
} else {
......@@ -629,6 +636,11 @@ public class JetTypeInferrer {
return resultType;
}
private boolean isBoolean(@NotNull JetType resultType) {
TypeConstructor booleanTypeConstructor = semanticServices.getStandardLibrary().getBoolean().getTypeConstructor();
return resultType.getConstructor().equals(booleanTypeConstructor);
}
@Override
public void visitArrayAccessExpression(JetArrayAccessExpression expression) {
JetExpression arrayExpression = expression.getArrayExpression();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册