diff --git a/idea/src/org/jetbrains/jet/lang/annotations/LabelsAnnotator.java b/idea/src/org/jetbrains/jet/lang/annotations/LabelsAnnotator.java index c4a4d541ae7..a8a6b04023b 100644 --- a/idea/src/org/jetbrains/jet/lang/annotations/LabelsAnnotator.java +++ b/idea/src/org/jetbrains/jet/lang/annotations/LabelsAnnotator.java @@ -17,7 +17,7 @@ import org.jetbrains.jet.lexer.JetTokens; public class LabelsAnnotator implements Annotator { public void annotate(@NotNull PsiElement element, @NotNull final AnnotationHolder holder) { - if (ApplicationManager.getApplication().isUnitTestMode()) return; +// if (ApplicationManager.getApplication().isUnitTestMode()) return; element.accept(new JetVisitor() { @Override public void visitPrefixExpression(JetPrefixExpression expression) { diff --git a/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java b/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java index 905e7ece0ef..b8d3505120c 100644 --- a/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java +++ b/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java @@ -590,12 +590,11 @@ public class JetTypeInferrer { result = targetType; } else if (operationType == JetTokens.AS_KEYWORD) { - // TODO : Check for cast impossibility + checkForCastImpossibility(expression, actualType, targetType); result = targetType; } else if (operationType == JetTokens.AS_SAFE) { - // TODO : Check for cast impossibility - + checkForCastImpossibility(expression, actualType, targetType); result = TypeUtils.makeNullable(targetType); } else { @@ -604,6 +603,25 @@ public class JetTypeInferrer { } } + private void checkForCastImpossibility(JetBinaryExpressionWithTypeRHS expression, JetType actualType, JetType targetType) { + if (actualType == null) return; + + JetTypeChecker typeChecker = semanticServices.getTypeChecker(); + if (!typeChecker.isSubtypeOf(targetType, actualType)) { + if (typeChecker.isSubtypeOf(actualType, targetType)) { + semanticServices.getErrorHandler().genericWarning(expression.getOperationSign().getNode(), "No cast needed, use ':' instead"); + } + else { + semanticServices.getErrorHandler().genericError(expression.getOperationSign().getNode(), "This cast can never succeed"); + } + } + else { + if (typeChecker.isSubtypeOf(actualType, targetType)) { + semanticServices.getErrorHandler().genericWarning(expression.getOperationSign().getNode(), "No cast needed"); + } + } + } + @Override public void visitTupleExpression(JetTupleExpression expression) { List entries = expression.getEntries(); diff --git a/idea/testData/checker/Casts.jet b/idea/testData/checker/Casts.jet index c447acde213..385b1338535 100644 --- a/idea/testData/checker/Casts.jet +++ b/idea/testData/checker/Casts.jet @@ -5,12 +5,12 @@ fun test() : Unit { x : Int? y : Int x as Int : Int - y as Int : Int - x as Int? : Int? - y as Int? : Int? + y as Int : Int + x as Int? : Int? + y as Int? : Int? x as? Int : Int? - y as? Int : Int? - x as? Int? : Int? - y as? Int? : Int? + y as? Int : Int? + x as? Int? : Int? + y as? Int? : Int? () } \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/JetTestCaseBase.java b/idea/tests/org/jetbrains/jet/JetTestCaseBase.java index c086a793366..b20b50961d5 100644 --- a/idea/tests/org/jetbrains/jet/JetTestCaseBase.java +++ b/idea/tests/org/jetbrains/jet/JetTestCaseBase.java @@ -46,7 +46,7 @@ public abstract class JetTestCaseBase extends LightDaemonAnalyzerTestCase { @Override protected void runTest() throws Throwable { - doTest(getTestFilePath(), false, false); + doTest(getTestFilePath(), true, false); } @NotNull