diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index 31132f968f4..a2972e164fd 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -3237,24 +3237,22 @@ The "returned" value of try expression with no finally is either the last expres assert rightType != null; Type rightTypeAsm = boxType(asmType(rightType)); JetExpression left = expression.getLeft(); - JetType leftType = bindingContext.get(BindingContext.EXPRESSION_TYPE, left); DeclarationDescriptor descriptor = rightType.getConstructor().getDeclarationDescriptor(); if (descriptor instanceof ClassDescriptor || descriptor instanceof TypeParameterDescriptor) { StackValue value = genQualified(receiver, left); value.put(boxType(value.type), v); - assert leftType != null; if (opToken != JetTokens.AS_SAFE) { - if (leftType.isNullable()) { - if (!rightType.isNullable()) { - v.dup(); - Label nonnull = new Label(); - v.ifnonnull(nonnull); - throwNewException(CLASS_TYPE_CAST_EXCEPTION, DescriptorRenderer.TEXT.renderType(leftType) + - " cannot be cast to " + - DescriptorRenderer.TEXT.renderType(rightType)); - v.mark(nonnull); - } + if (!rightType.isNullable()) { + v.dup(); + Label nonnull = new Label(); + v.ifnonnull(nonnull); + JetType leftType = bindingContext.get(BindingContext.EXPRESSION_TYPE, left); + assert leftType != null; + throwNewException(CLASS_TYPE_CAST_EXCEPTION, DescriptorRenderer.TEXT.renderType(leftType) + + " cannot be cast to " + + DescriptorRenderer.TEXT.renderType(rightType)); + v.mark(nonnull); } } else { @@ -3271,7 +3269,7 @@ The "returned" value of try expression with no finally is either the last expres return StackValue.onStack(rightTypeAsm); } else { - throw new UnsupportedOperationException("don't know how to handle non-class types in as/as?"); + throw new UnsupportedOperationException("Don't know how to handle non-class types in as/as? : " + descriptor); } } } diff --git a/compiler/testData/codegen/box/casts/castGenericNull.kt b/compiler/testData/codegen/box/casts/castGenericNull.kt new file mode 100644 index 00000000000..f7de6887b83 --- /dev/null +++ b/compiler/testData/codegen/box/casts/castGenericNull.kt @@ -0,0 +1,13 @@ +fun castToString(t: T) { + t as String +} + + +fun box(): String { + try { + castToString(null) + } catch (e: Exception) { + return "OK" + } + return "Fail" +} diff --git a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java index 81dc550c30e..7e3df00d58b 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java @@ -433,6 +433,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { blackBoxFileByFullPath("compiler/testData/codegen/box/casts/asSafe.kt"); } + @TestMetadata("castGenericNull.kt") + public void testCastGenericNull() throws Exception { + blackBoxFileByFullPath("compiler/testData/codegen/box/casts/castGenericNull.kt"); + } + @TestMetadata("is.kt") public void testIs() throws Exception { blackBoxFileByFullPath("compiler/testData/codegen/box/casts/is.kt");