Fix NPE in data class 'equals' codegen

Do not perform the optimization from 0a11385006 for data class
properties of nullable types
This commit is contained in:
Alexander Udalov
2018-04-12 19:47:49 +02:00
parent 579b773388
commit 30b9caeae6
6 changed files with 47 additions and 1 deletions
@@ -52,6 +52,7 @@ import org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver;
import org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitReceiver;
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.kotlin.types.TypeUtils;
import org.jetbrains.org.objectweb.asm.FieldVisitor;
import org.jetbrains.org.objectweb.asm.Label;
import org.jetbrains.org.objectweb.asm.MethodVisitor;
@@ -577,7 +578,13 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
if (isPrimitive(asmType)) {
StackValue value = StackValue.cmp(KtTokens.EQEQ, asmType, StackValue.onStack(asmType), StackValue.onStack(asmType));
value.put(Type.BOOLEAN_TYPE, iv);
} else {
}
else if (TypeUtils.isNullableType(propertyDescriptor.getType())) {
StackValue value =
genEqualsForExpressionsOnStack(KtTokens.EQEQ, StackValue.onStack(asmType), StackValue.onStack(asmType));
value.put(Type.BOOLEAN_TYPE, iv);
}
else {
Type owner =
DescriptorUtils.isInterface(propertyDescriptor.getType().getConstructor().getDeclarationDescriptor())
? AsmTypes.OBJECT_TYPE
@@ -0,0 +1,15 @@
data class A(val v: Any?)
data class B<T>(val v: T)
fun box(): String {
val a1 = A(null)
val a2 = A("")
if (a1 == a2 || a2 == a1) return "Fail 1"
val b1 = B(null)
val b2 = B("")
if (b1 == b2 || b2 == b1) return "Fail 2"
return "OK"
}
@@ -7230,6 +7230,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
doTest(fileName);
}
@TestMetadata("null.kt")
public void testNull() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/dataClasses/equals/null.kt");
doTest(fileName);
}
@TestMetadata("nullother.kt")
public void testNullother() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/dataClasses/equals/nullother.kt");
@@ -7230,6 +7230,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("null.kt")
public void testNull() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/dataClasses/equals/null.kt");
doTest(fileName);
}
@TestMetadata("nullother.kt")
public void testNullother() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/dataClasses/equals/nullother.kt");
@@ -7230,6 +7230,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
doTest(fileName);
}
@TestMetadata("null.kt")
public void testNull() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/dataClasses/equals/null.kt");
doTest(fileName);
}
@TestMetadata("nullother.kt")
public void testNullother() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/dataClasses/equals/nullother.kt");
@@ -8563,6 +8563,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
doTest(fileName);
}
@TestMetadata("null.kt")
public void testNull() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/dataClasses/equals/null.kt");
doTest(fileName);
}
@TestMetadata("nullother.kt")
public void testNullother() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/dataClasses/equals/nullother.kt");