'As' function for constants
This commit is contained in:
+1
-1
@@ -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";
|
||||
|
||||
-9
@@ -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,
|
||||
|
||||
@@ -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<T>(param: T, f: (T) -> Unit): String {
|
||||
try {
|
||||
f(param)
|
||||
}
|
||||
catch (e: ClassCastException) {
|
||||
return "OK"
|
||||
}
|
||||
return "fail"
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
@@ -30,11 +30,11 @@ fun test() {
|
||||
<!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>: Double
|
||||
<!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>: Float
|
||||
|
||||
1 <!USELESS_CAST!>as<!> Byte
|
||||
1 <!CAST_NEVER_SUCCEEDS!>as<!> Byte
|
||||
1 <!USELESS_CAST!>as<!> Int
|
||||
0xff <!USELESS_CAST!>as<!> Long
|
||||
0xff <!CAST_NEVER_SUCCEEDS!>as<!> Long
|
||||
|
||||
<!CONSTANT_EXPECTED_TYPE_MISMATCH!>1.1<!> <!CAST_NEVER_SUCCEEDS!>as<!> Int
|
||||
1.1 <!CAST_NEVER_SUCCEEDS!>as<!> Int
|
||||
<!CONSTANT_EXPECTED_TYPE_MISMATCH!>1.1<!>: Int
|
||||
|
||||
varargByte(0x77, 1, 3, <!CONSTANT_EXPECTED_TYPE_MISMATCH!>200<!>, 0b111)
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
fun asCall() {
|
||||
1 <!USELESS_CAST!>as<!> Int
|
||||
1 <!CAST_NEVER_SUCCEEDS!>as<!> Byte
|
||||
1 <!CAST_NEVER_SUCCEEDS!>as<!> Short
|
||||
1 <!CAST_NEVER_SUCCEEDS!>as<!> Long
|
||||
1 <!CAST_NEVER_SUCCEEDS!>as<!> Char
|
||||
1 <!CAST_NEVER_SUCCEEDS!>as<!> Double
|
||||
1 <!CAST_NEVER_SUCCEEDS!>as<!> Float
|
||||
|
||||
1.0 <!CAST_NEVER_SUCCEEDS!>as<!> Int
|
||||
1.0 <!CAST_NEVER_SUCCEEDS!>as<!> Byte
|
||||
1.0 <!CAST_NEVER_SUCCEEDS!>as<!> Short
|
||||
1.0 <!CAST_NEVER_SUCCEEDS!>as<!> Long
|
||||
1.0 <!CAST_NEVER_SUCCEEDS!>as<!> Char
|
||||
1.0 <!USELESS_CAST!>as<!> Double
|
||||
1.0 <!CAST_NEVER_SUCCEEDS!>as<!> Float
|
||||
|
||||
1f <!CAST_NEVER_SUCCEEDS!>as<!> Int
|
||||
1f <!CAST_NEVER_SUCCEEDS!>as<!> Byte
|
||||
1f <!CAST_NEVER_SUCCEEDS!>as<!> Short
|
||||
1f <!CAST_NEVER_SUCCEEDS!>as<!> Long
|
||||
1f <!CAST_NEVER_SUCCEEDS!>as<!> Char
|
||||
1f <!CAST_NEVER_SUCCEEDS!>as<!> Double
|
||||
1f <!USELESS_CAST!>as<!> Float
|
||||
}
|
||||
|
||||
fun asSafe() {
|
||||
1 <!USELESS_CAST!>as?<!> Int
|
||||
1 <!CAST_NEVER_SUCCEEDS!>as?<!> Byte
|
||||
1 <!CAST_NEVER_SUCCEEDS!>as?<!> Short
|
||||
1 <!CAST_NEVER_SUCCEEDS!>as?<!> Long
|
||||
1 <!CAST_NEVER_SUCCEEDS!>as?<!> Char
|
||||
1 <!CAST_NEVER_SUCCEEDS!>as?<!> Double
|
||||
1 <!CAST_NEVER_SUCCEEDS!>as?<!> Float
|
||||
|
||||
1.0 <!CAST_NEVER_SUCCEEDS!>as?<!> Int
|
||||
1.0 <!CAST_NEVER_SUCCEEDS!>as?<!> Byte
|
||||
1.0 <!CAST_NEVER_SUCCEEDS!>as?<!> Short
|
||||
1.0 <!CAST_NEVER_SUCCEEDS!>as?<!> Long
|
||||
1.0 <!CAST_NEVER_SUCCEEDS!>as?<!> Char
|
||||
1.0 <!USELESS_CAST!>as?<!> Double
|
||||
1.0 <!CAST_NEVER_SUCCEEDS!>as?<!> Float
|
||||
|
||||
1f <!CAST_NEVER_SUCCEEDS!>as?<!> Int
|
||||
1f <!CAST_NEVER_SUCCEEDS!>as?<!> Byte
|
||||
1f <!CAST_NEVER_SUCCEEDS!>as?<!> Short
|
||||
1f <!CAST_NEVER_SUCCEEDS!>as?<!> Long
|
||||
1f <!CAST_NEVER_SUCCEEDS!>as?<!> Char
|
||||
1f <!CAST_NEVER_SUCCEEDS!>as?<!> Double
|
||||
1f <!USELESS_CAST!>as?<!> Float
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -4,5 +4,5 @@ fun test() {
|
||||
<error>1</error> : Double
|
||||
1 <warning>as</warning> Byte
|
||||
1 <warning>as</warning> Int
|
||||
<error>1</error> <warning>as</warning> Double
|
||||
1 <warning>as</warning> Double
|
||||
}
|
||||
Reference in New Issue
Block a user