Fix codegen of as-casts for nullable generics

This commit is contained in:
Alexander Udalov
2013-02-06 17:02:54 +04:00
parent 70f67c7993
commit 75f09b7527
3 changed files with 29 additions and 13 deletions
@@ -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);
}
}
}
@@ -0,0 +1,13 @@
fun castToString<T>(t: T) {
t as String
}
fun box(): String {
try {
castToString<Any?>(null)
} catch (e: Exception) {
return "OK"
}
return "Fail"
}
@@ -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");