diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java index 623f5301f89..dac6177a4e0 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java @@ -153,7 +153,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { TypeResolutionContext typeResolutionContext = new TypeResolutionContext(context.scope, context.trace, true, allowBareTypes); PossiblyBareType possiblyBareTarget = context.expressionTypingServices.getTypeResolver().resolvePossiblyBareType(typeResolutionContext, right); - if (isTypeFlexible(left) || operationType == JetTokens.COLON) { + if (operationType == JetTokens.COLON) { // We do not allow bare types on static assertions, because static assertions provide an expected type for their argument, // thus causing a circularity in type dependencies assert !possiblyBareTarget.isBare() : "Bare types should not be allowed for static assertions, because argument inference makes no sense there"; diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingUtils.java index eeedff37c39..40f35dfa691 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingUtils.java @@ -132,15 +132,6 @@ public class ExpressionTypingUtils { } } - public static boolean isTypeFlexible(@Nullable JetExpression expression) { - if (expression == null) return false; - - return TokenSet.create( - JetNodeTypes.INTEGER_CONSTANT, - JetNodeTypes.FLOAT_CONSTANT - ).contains(expression.getNode().getElementType()); - } - private static boolean isCapturedInInline( @NotNull BindingContext context, @NotNull DeclarationDescriptor scopeContainer, diff --git a/compiler/testData/codegen/box/casts/asForConstants.kt b/compiler/testData/codegen/box/casts/asForConstants.kt new file mode 100644 index 00000000000..83d400c01b4 --- /dev/null +++ b/compiler/testData/codegen/box/casts/asForConstants.kt @@ -0,0 +1,38 @@ +fun box(): String { + if (check(1, { it as Int }) == "OK") return "fail 1" + if (check(1, { it as Byte }) != "OK") return "fail 2" + if (check(1, { it as Short }) != "OK") return "fail 3" + if (check(1, { it as Long }) != "OK") return "fail 4" + if (check(1, { it as Char }) != "OK") return "fail 5" + if (check(1, { it as Double }) != "OK") return "fail 6" + if (check(1, { it as Float }) != "OK") return "fail 7" + + if (check(1.0, { it as Int }) != "OK") return "fail 11" + if (check(1.0, { it as Byte }) != "OK") return "fail 12" + if (check(1.0, { it as Short }) != "OK") return "fail 13" + if (check(1.0, { it as Long }) != "OK") return "fail 14" + if (check(1.0, { it as Char }) != "OK") return "fail 15" + if (check(1.0, { it as Double }) == "OK") return "fail 16" + if (check(1.0, { it as Float }) != "OK") return "fail 17" + + if (check(1f, { it as Int }) != "OK") return "fail 21" + if (check(1f, { it as Byte }) != "OK") return "fail 22" + if (check(1f, { it as Short }) != "OK") return "fail 23" + if (check(1f, { it as Long }) != "OK") return "fail 24" + if (check(1f, { it as Char }) != "OK") return "fail 25" + if (check(1f, { it as Double }) != "OK") return "fail 26" + if (check(1f, { it as Float }) == "OK") return "fail 27" + + return "OK" +} + +fun check(param: T, f: (T) -> Unit): String { + try { + f(param) + } + catch (e: ClassCastException) { + return "OK" + } + return "fail" +} + diff --git a/compiler/testData/codegen/box/casts/asSafeForConstants.kt b/compiler/testData/codegen/box/casts/asSafeForConstants.kt new file mode 100644 index 00000000000..e64bf29a41d --- /dev/null +++ b/compiler/testData/codegen/box/casts/asSafeForConstants.kt @@ -0,0 +1,27 @@ +fun box(): String { + if ((1 as? Int) == null) return "fail 1" + if ((1 as? Byte) != null) return "fail 2" + if ((1 as? Short) != null) return "fail 3" + if ((1 as? Long) != null) return "fail 4" + if ((1 as? Char) != null) return "fail 5" + if ((1 as? Double) != null) return "fail 6" + if ((1 as? Float) != null) return "fail 7" + + if ((1.0 as? Int) != null) return "fail 11" + if ((1.0 as? Byte) != null) return "fail 12" + if ((1.0 as? Short) != null) return "fail 13" + if ((1.0 as? Long) != null) return "fail 14" + if ((1.0 as? Char) != null) return "fail 15" + if ((1.0 as? Double) == null) return "fail 16" + if ((1.0 as? Float) != null) return "fail 17" + + if ((1f as? Int) != null) return "fail 21" + if ((1f as? Byte) != null) return "fail 22" + if ((1f as? Short) != null) return "fail 23" + if ((1f as? Long) != null) return "fail 24" + if ((1f as? Char) != null) return "fail 25" + if ((1f as? Double) != null) return "fail 26" + if ((1f as? Float) == null) return "fail 27" + + return "OK" +} diff --git a/compiler/testData/diagnostics/tests/Constants.kt b/compiler/testData/diagnostics/tests/Constants.kt index 50557da8897..f0ecb9e116c 100644 --- a/compiler/testData/diagnostics/tests/Constants.kt +++ b/compiler/testData/diagnostics/tests/Constants.kt @@ -30,11 +30,11 @@ fun test() { 1: Double 1: Float - 1 as Byte + 1 as Byte 1 as Int - 0xff as Long + 0xff as Long - 1.1 as Int + 1.1 as Int 1.1: Int varargByte(0x77, 1, 3, 200, 0b111) diff --git a/compiler/testData/diagnostics/tests/cast/constants.kt b/compiler/testData/diagnostics/tests/cast/constants.kt new file mode 100644 index 00000000000..53540e9843b --- /dev/null +++ b/compiler/testData/diagnostics/tests/cast/constants.kt @@ -0,0 +1,51 @@ +fun asCall() { + 1 as Int + 1 as Byte + 1 as Short + 1 as Long + 1 as Char + 1 as Double + 1 as Float + + 1.0 as Int + 1.0 as Byte + 1.0 as Short + 1.0 as Long + 1.0 as Char + 1.0 as Double + 1.0 as Float + + 1f as Int + 1f as Byte + 1f as Short + 1f as Long + 1f as Char + 1f as Double + 1f as Float +} + +fun asSafe() { + 1 as? Int + 1 as? Byte + 1 as? Short + 1 as? Long + 1 as? Char + 1 as? Double + 1 as? Float + + 1.0 as? Int + 1.0 as? Byte + 1.0 as? Short + 1.0 as? Long + 1.0 as? Char + 1.0 as? Double + 1.0 as? Float + + 1f as? Int + 1f as? Byte + 1f as? Short + 1f as? Long + 1f as? Char + 1f as? Double + 1f as? Float +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index c4621e3ff7c..c331c6b321f 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -990,6 +990,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage doTest("compiler/testData/diagnostics/tests/cast/AsErasedWarning.kt"); } + @TestMetadata("constants.kt") + public void testConstants() throws Exception { + doTest("compiler/testData/diagnostics/tests/cast/constants.kt"); + } + @TestMetadata("DowncastMap.kt") public void testDowncastMap() throws Exception { doTest("compiler/testData/diagnostics/tests/cast/DowncastMap.kt"); diff --git a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java index e62748e8cd3..af8e4b37c3b 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java @@ -728,6 +728,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest("compiler/testData/codegen/box/casts/as.kt"); } + @TestMetadata("asForConstants.kt") + public void testAsForConstants() throws Exception { + doTest("compiler/testData/codegen/box/casts/asForConstants.kt"); + } + @TestMetadata("asSafe.kt") public void testAsSafe() throws Exception { doTest("compiler/testData/codegen/box/casts/asSafe.kt"); @@ -738,6 +743,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest("compiler/testData/codegen/box/casts/asSafeFail.kt"); } + @TestMetadata("asSafeForConstants.kt") + public void testAsSafeForConstants() throws Exception { + doTest("compiler/testData/codegen/box/casts/asSafeForConstants.kt"); + } + @TestMetadata("castGenericNull.kt") public void testCastGenericNull() throws Exception { doTest("compiler/testData/codegen/box/casts/castGenericNull.kt"); diff --git a/idea/testData/checker/Constants.kt b/idea/testData/checker/Constants.kt index 2ff80a0c1bb..d1b647b8fa3 100644 --- a/idea/testData/checker/Constants.kt +++ b/idea/testData/checker/Constants.kt @@ -4,5 +4,5 @@ fun test() { 1 : Double 1 as Byte 1 as Int - 1 as Double + 1 as Double } \ No newline at end of file