From 495f1ea3b95f7d6adec10df9eba21448a3ed3a60 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Tue, 15 Mar 2011 22:27:20 +0300 Subject: [PATCH] If conditions typechecked --- .../jet/lang/types/JetTypeInferrer.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java b/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java index 73a9599ff2a..ef8e5da8095 100644 --- a/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java +++ b/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java @@ -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();