Add CodegenUtil.isNullableType(), fix assertions

JetType.isNullable() is not accurate when the type denotes a type parameter:
a parameter can be not null (isNullable=false), but its upper bound can be
nullable (<T: Any?>), so null may appear in the value of such type. Therefore
it's preferred to use a special check (isNullableType()) in codegen from now on

Do not generate assertion for parameters of not-null types which have a
nullable upper bound + the same with Java method calls

Also fix Intrinsics class internal name in tests

 #KT-3313 Fixed
This commit is contained in:
Alexander Udalov
2013-02-05 18:54:19 +04:00
parent b6870b4bba
commit 1f4dd8cd33
7 changed files with 53 additions and 5 deletions
@@ -47,6 +47,7 @@ import java.util.Set;
import static org.jetbrains.asm4.Opcodes.*;
import static org.jetbrains.jet.codegen.CodegenUtil.isInterface;
import static org.jetbrains.jet.codegen.CodegenUtil.isNullableType;
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isClassObject;
import static org.jetbrains.jet.lang.resolve.java.AsmTypeConstants.JAVA_STRING_TYPE;
@@ -532,7 +533,7 @@ public class AsmUtil {
for (ValueParameterDescriptor parameter : descriptor.getValueParameters()) {
JetType type = parameter.getReturnType();
if (type == null || type.isNullable()) continue;
if (type == null || isNullableType(type)) continue;
int index = frameMap.getIndex(parameter);
Type asmType = state.getTypeMapper().mapReturnType(type);
@@ -574,7 +575,7 @@ public class AsmUtil {
if (!isDeclaredInJava(descriptor, state.getBindingContext())) return;
JetType type = descriptor.getReturnType();
if (type == null || type.isNullable()) return;
if (type == null || isNullableType(type)) return;
Type asmType = state.getTypeMapper().mapReturnType(type);
if (asmType.getSort() == Type.OBJECT || asmType.getSort() == Type.ARRAY) {
@@ -41,6 +41,7 @@ import org.jetbrains.jet.lang.resolve.java.JvmStdlibNames;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeUtils;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import java.util.*;
@@ -278,5 +279,16 @@ public class CodegenUtil {
return ((MemberDescriptor) declaration).getModality() == ABSTRACT;
}
});
}
}
/**
* A work-around of the generic nullability problem in the type checker
* @return true if a value of this type can be null
*/
public static boolean isNullableType(@NotNull JetType type) {
if (type.getConstructor().getDeclarationDescriptor() instanceof TypeParameterDescriptor) {
return TypeUtils.hasNullableSuperType(type);
}
return type.isNullable();
}
}
@@ -0,0 +1,7 @@
fun foo<T>(t: T) {
}
fun box(): String {
foo(null)
return "OK"
}
@@ -0,0 +1 @@
fun foo<T : Any>(t: T) = t
@@ -0,0 +1,3 @@
fun foo<T>(a: List<T>) {
val t: T = a.get(0)
}
@@ -18,6 +18,7 @@ package org.jetbrains.jet.codegen;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import jet.runtime.Intrinsics;
import org.jetbrains.asm4.ClassReader;
import org.jetbrains.asm4.ClassVisitor;
import org.jetbrains.asm4.MethodVisitor;
@@ -29,6 +30,7 @@ import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys;
import org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentUtil;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.config.CompilerConfiguration;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
import org.jetbrains.jet.lang.resolve.java.PackageClassUtils;
import org.jetbrains.jet.lang.resolve.name.FqName;
@@ -138,11 +140,28 @@ public class GenerateNotNullAssertionsTest extends CodegenTestCase {
assertEquals(3, StringUtil.getOccurrenceCount(text, "checkParameterIsNotNull"));
}
public void testAssertionForNotNullTypeParam() {
setUpEnvironment(true, true);
loadFile("notNullAssertions/assertionForNotNullTypeParam.kt");
assertTrue(generateToText().contains("checkParameterIsNotNull"));
}
public void testNoAssertionForNullableGenericMethod() {
setUpEnvironment(true, false);
loadFile("notNullAssertions/noAssertionForNullableGenericMethod.kt");
assertNoIntrinsicsMethodIsCalled(PackageClassUtils.getPackageClassName(FqName.ROOT));
}
private void assertNoIntrinsicsMethodIsCalled(String className) {
ClassFileFactory classes = generateClassesInFile();
ClassReader reader = new ClassReader(classes.asBytes(className + ".class"));
final String intrinsics = JvmClassName.byFqNameWithoutInnerClasses(Intrinsics.class.getName()).getInternalName();
reader.accept(new ClassVisitor(Opcodes.ASM4) {
@Override
public MethodVisitor visitMethod(
@@ -152,8 +171,8 @@ public class GenerateNotNullAssertionsTest extends CodegenTestCase {
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
assertFalse(
"jet/Intrinsics method is called: " + name + desc + " Caller: " + callerName + callerDesc,
"jet/Intrinsics".equals(owner)
"Intrinsics method is called: " + name + desc + " Caller: " + callerName + callerDesc,
intrinsics.equals(owner)
);
}
};
@@ -1736,6 +1736,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
blackBoxFileByFullPath("compiler/testData/codegen/box/functions/kt2929.kt");
}
@TestMetadata("kt3313.kt")
public void testKt3313() throws Exception {
blackBoxFileByFullPath("compiler/testData/codegen/box/functions/kt3313.kt");
}
@TestMetadata("kt395.kt")
public void testKt395() throws Exception {
blackBoxFileByFullPath("compiler/testData/codegen/box/functions/kt395.kt");