Fix incorrect TypeCastException message when casting null

#KT-5121 Fixed
This commit is contained in:
Alexander Udalov
2015-07-04 03:18:50 +03:00
parent cb03f0df5a
commit ac0db5ce80
2 changed files with 19 additions and 8 deletions
@@ -3727,7 +3727,7 @@ The "returned" value of try expression with no finally is either the last expres
@Override
public StackValue visitBinaryWithTypeRHSExpression(@NotNull JetBinaryExpressionWithTypeRHS expression, StackValue receiver) {
final JetExpression left = expression.getLeft();
JetExpression left = expression.getLeft();
final IElementType opToken = expression.getOperationReference().getReferencedNameElementType();
if (opToken == JetTokens.COLON) {
return gen(left);
@@ -3752,10 +3752,7 @@ The "returned" value of try expression with no finally is either the last expres
v.dup();
Label nonnull = new Label();
v.ifnonnull(nonnull);
JetType leftType = bindingContext.getType(left);
assert leftType != null;
genThrow(v, "kotlin/TypeCastException", DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(leftType) +
" cannot be cast to " +
genThrow(v, "kotlin/TypeCastException", "null cannot be cast to non-null type " +
DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(rightType));
v.mark(nonnull);
}
@@ -1,4 +1,7 @@
import java.util.ArrayList
// KT-2823 TypeCastException has no message
// KT-5121 Better error message in on casting null to non-null type
fun box(): String {
try {
@@ -6,9 +9,20 @@ fun box(): String {
a as Array<String>
}
catch (e: TypeCastException) {
if (e.getMessage() == "kotlin.Any? cannot be cast to kotlin.Array<kotlin.String>") {
return "OK"
if (e.getMessage() != "null cannot be cast to non-null type kotlin.Array<kotlin.String>") {
return "Fail 1: $e"
}
}
return "fail"
try {
val x: String? = null
x as String
}
catch (e: TypeCastException) {
if (e.getMessage() != "null cannot be cast to non-null type kotlin.String") {
return "Fail 2: $e"
}
}
return "OK"
}