Fix incorrect TypeCastException message when casting null
#KT-5121 Fixed
This commit is contained in:
@@ -3727,7 +3727,7 @@ The "returned" value of try expression with no finally is either the last expres
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StackValue visitBinaryWithTypeRHSExpression(@NotNull JetBinaryExpressionWithTypeRHS expression, StackValue receiver) {
|
public StackValue visitBinaryWithTypeRHSExpression(@NotNull JetBinaryExpressionWithTypeRHS expression, StackValue receiver) {
|
||||||
final JetExpression left = expression.getLeft();
|
JetExpression left = expression.getLeft();
|
||||||
final IElementType opToken = expression.getOperationReference().getReferencedNameElementType();
|
final IElementType opToken = expression.getOperationReference().getReferencedNameElementType();
|
||||||
if (opToken == JetTokens.COLON) {
|
if (opToken == JetTokens.COLON) {
|
||||||
return gen(left);
|
return gen(left);
|
||||||
@@ -3752,10 +3752,7 @@ The "returned" value of try expression with no finally is either the last expres
|
|||||||
v.dup();
|
v.dup();
|
||||||
Label nonnull = new Label();
|
Label nonnull = new Label();
|
||||||
v.ifnonnull(nonnull);
|
v.ifnonnull(nonnull);
|
||||||
JetType leftType = bindingContext.getType(left);
|
genThrow(v, "kotlin/TypeCastException", "null cannot be cast to non-null type " +
|
||||||
assert leftType != null;
|
|
||||||
genThrow(v, "kotlin/TypeCastException", DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(leftType) +
|
|
||||||
" cannot be cast to " +
|
|
||||||
DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(rightType));
|
DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(rightType));
|
||||||
v.mark(nonnull);
|
v.mark(nonnull);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
|
import java.util.ArrayList
|
||||||
|
|
||||||
// KT-2823 TypeCastException has no message
|
// KT-2823 TypeCastException has no message
|
||||||
|
// KT-5121 Better error message in on casting null to non-null type
|
||||||
|
|
||||||
fun box(): String {
|
fun box(): String {
|
||||||
try {
|
try {
|
||||||
@@ -6,9 +9,20 @@ fun box(): String {
|
|||||||
a as Array<String>
|
a as Array<String>
|
||||||
}
|
}
|
||||||
catch (e: TypeCastException) {
|
catch (e: TypeCastException) {
|
||||||
if (e.getMessage() == "kotlin.Any? cannot be cast to kotlin.Array<kotlin.String>") {
|
if (e.getMessage() != "null cannot be cast to non-null type kotlin.Array<kotlin.String>") {
|
||||||
return "OK"
|
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"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user