From 1bbbc1ca1c2fbb11ec2fa6111dda38570ea26124 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Mon, 6 Mar 2017 18:25:36 +0300 Subject: [PATCH] KT-16684 `hashCode` doesn't check data class property value of generic type for null --- .../jetbrains/kotlin/psi2ir/KotlinUtils.kt | 6 +- .../testData/ir/irText/classes/dataClasses.kt | 4 +- .../ir/irText/classes/dataClasses.txt | 87 ++++++++++++++++++ .../ir/irText/classes/dataClassesGeneric.kt | 1 + .../ir/irText/classes/dataClassesGeneric.txt | 89 +++++++++++++++++++ .../kotlin/ir/IrTextTestCaseGenerated.java | 6 ++ 6 files changed, 188 insertions(+), 5 deletions(-) create mode 100644 compiler/testData/ir/irText/classes/dataClassesGeneric.kt create mode 100644 compiler/testData/ir/irText/classes/dataClassesGeneric.txt diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/KotlinUtils.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/KotlinUtils.kt index 38f8f126874..8bb8a681a0c 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/KotlinUtils.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/KotlinUtils.kt @@ -32,13 +32,11 @@ import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.types.KotlinType -import org.jetbrains.kotlin.types.checker.KotlinTypeChecker -import org.jetbrains.kotlin.types.typeUtil.builtIns -import org.jetbrains.kotlin.types.upperIfFlexible +import org.jetbrains.kotlin.types.TypeUtils import java.lang.Exception fun KotlinType.containsNull() = - KotlinTypeChecker.DEFAULT.isSubtypeOf(builtIns.nullableNothingType, this.upperIfFlexible()) + TypeUtils.isNullableType(this) fun KtElement.deparenthesize(): KtElement = if (this is KtExpression) KtPsiUtil.safeDeparenthesize(this) else this diff --git a/compiler/testData/ir/irText/classes/dataClasses.kt b/compiler/testData/ir/irText/classes/dataClasses.kt index 2bb29a731ea..6aac50c8493 100644 --- a/compiler/testData/ir/irText/classes/dataClasses.kt +++ b/compiler/testData/ir/irText/classes/dataClasses.kt @@ -1 +1,3 @@ -data class Test1(val x: Int, val y: String, val z: Any) \ No newline at end of file +data class Test1(val x: Int, val y: String, val z: Any) + +data class Test2(val x: Any?) \ No newline at end of file diff --git a/compiler/testData/ir/irText/classes/dataClasses.txt b/compiler/testData/ir/irText/classes/dataClasses.txt index 111610708d8..6e0610f893c 100644 --- a/compiler/testData/ir/irText/classes/dataClasses.txt +++ b/compiler/testData/ir/irText/classes/dataClasses.txt @@ -155,3 +155,90 @@ FILE /dataClasses.kt CONST Boolean type=kotlin.Boolean value='false' RETURN type=kotlin.Nothing from='equals(Any?): Boolean' CONST Boolean type=kotlin.Boolean value='true' + CLASS CLASS Test2 + CONSTRUCTOR public constructor Test2(x: kotlin.Any?) + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'constructor Any()' + INSTANCE_INITIALIZER_CALL classDescriptor='Test2' + PROPERTY public final val x: kotlin.Any? + FIELD PROPERTY_BACKING_FIELD public final val x: kotlin.Any? + EXPRESSION_BODY + GET_VAR 'value-parameter x: Any?' type=kotlin.Any? origin=INITIALIZE_PROPERTY_FROM_PARAMETER + FUN DEFAULT_PROPERTY_ACCESSOR public final fun (): kotlin.Any? + BLOCK_BODY + RETURN type=kotlin.Nothing from='(): Any?' + GET_FIELD 'x: Any?' type=kotlin.Any? origin=null + receiver: GET_VAR '' type=Test2 origin=null + FUN GENERATED_DATA_CLASS_MEMBER public final operator fun component1(): kotlin.Any? + BLOCK_BODY + RETURN type=kotlin.Nothing from='component1(): Any?' + CALL '(): Any?' type=kotlin.Any? origin=GET_PROPERTY + $this: GET_VAR '' type=Test2 origin=null + FUN GENERATED_DATA_CLASS_MEMBER public final fun copy(x: kotlin.Any? = ...): Test2 + x: EXPRESSION_BODY + CALL '(): Any?' type=kotlin.Any? origin=GET_PROPERTY + $this: GET_VAR '' type=Test2 origin=null + BLOCK_BODY + RETURN type=kotlin.Nothing from='copy(Any? = ...): Test2' + CALL 'constructor Test2(Any?)' type=Test2 origin=null + x: GET_VAR 'value-parameter x: Any? = ...' type=kotlin.Any? origin=null + FUN GENERATED_DATA_CLASS_MEMBER public open override fun toString(): kotlin.String + BLOCK_BODY + RETURN type=kotlin.Nothing from='toString(): String' + STRING_CONCATENATION type=kotlin.String + CONST String type=kotlin.String value='Test2(' + CONST String type=kotlin.String value='x=' + CALL '(): Any?' type=kotlin.Any? origin=GET_PROPERTY + $this: GET_VAR '' type=Test2 origin=null + CONST String type=kotlin.String value=')' + FUN GENERATED_DATA_CLASS_MEMBER public open override fun hashCode(): kotlin.Int + BLOCK_BODY + VAR IR_TEMPORARY_VARIABLE var tmp0_result: kotlin.Int + CONST Int type=kotlin.Int value='0' + SET_VAR 'tmp0_result: Int' type=kotlin.Unit origin=EQ + BLOCK type=kotlin.Int origin=null + VAR IR_TEMPORARY_VARIABLE val tmp1: kotlin.Any? + CALL '(): Any?' type=kotlin.Any? origin=GET_PROPERTY + $this: GET_VAR '' type=Test2 origin=null + WHEN type=kotlin.Int origin=null + BRANCH + if: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EQEQ + arg0: GET_VAR 'tmp1: Any?' type=kotlin.Any? origin=null + arg1: CONST Null type=kotlin.Nothing? value='null' + then: CONST Int type=kotlin.Int value='0' + BRANCH + if: CONST Boolean type=kotlin.Boolean value='true' + then: CALL 'hashCode(): Int' type=kotlin.Int origin=null + $this: GET_VAR 'tmp1: Any?' type=kotlin.Any? origin=null + RETURN type=kotlin.Nothing from='hashCode(): Int' + GET_VAR 'tmp0_result: Int' type=kotlin.Int origin=null + FUN GENERATED_DATA_CLASS_MEMBER public open override fun equals(other: kotlin.Any?): kotlin.Boolean + BLOCK_BODY + WHEN type=kotlin.Unit origin=null + BRANCH + if: CALL 'EQEQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EQEQEQ + arg0: GET_VAR '' type=Test2 origin=null + arg1: GET_VAR 'value-parameter other: Any?' type=kotlin.Any? origin=null + then: RETURN type=kotlin.Nothing from='equals(Any?): Boolean' + CONST Boolean type=kotlin.Boolean value='true' + WHEN type=kotlin.Unit origin=null + BRANCH + if: TYPE_OP type=kotlin.Boolean origin=NOT_INSTANCEOF typeOperand=Test2 + GET_VAR 'value-parameter other: Any?' type=kotlin.Any? origin=null + then: RETURN type=kotlin.Nothing from='equals(Any?): Boolean' + CONST Boolean type=kotlin.Boolean value='false' + VAR IR_TEMPORARY_VARIABLE val tmp0_other_with_cast: Test2 + TYPE_OP type=Test2 origin=CAST typeOperand=Test2 + GET_VAR 'value-parameter other: Any?' type=kotlin.Any? origin=null + WHEN type=kotlin.Unit origin=null + BRANCH + if: CALL 'NOT(Boolean): Boolean' type=kotlin.Boolean origin=EXCLEQ + arg0: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EXCLEQ + arg0: CALL '(): Any?' type=kotlin.Any? origin=GET_PROPERTY + $this: GET_VAR '' type=Test2 origin=null + arg1: CALL '(): Any?' type=kotlin.Any? origin=GET_PROPERTY + $this: GET_VAR 'tmp0_other_with_cast: Test2' type=Test2 origin=null + then: RETURN type=kotlin.Nothing from='equals(Any?): Boolean' + CONST Boolean type=kotlin.Boolean value='false' + RETURN type=kotlin.Nothing from='equals(Any?): Boolean' + CONST Boolean type=kotlin.Boolean value='true' diff --git a/compiler/testData/ir/irText/classes/dataClassesGeneric.kt b/compiler/testData/ir/irText/classes/dataClassesGeneric.kt new file mode 100644 index 00000000000..11e8b7786a4 --- /dev/null +++ b/compiler/testData/ir/irText/classes/dataClassesGeneric.kt @@ -0,0 +1 @@ +data class Test1(val x: T) \ No newline at end of file diff --git a/compiler/testData/ir/irText/classes/dataClassesGeneric.txt b/compiler/testData/ir/irText/classes/dataClassesGeneric.txt new file mode 100644 index 00000000000..a60310f757f --- /dev/null +++ b/compiler/testData/ir/irText/classes/dataClassesGeneric.txt @@ -0,0 +1,89 @@ +FILE /dataClassesGeneric.kt + CLASS CLASS Test1 + CONSTRUCTOR public constructor Test1(x: T) + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'constructor Any()' + INSTANCE_INITIALIZER_CALL classDescriptor='Test1' + PROPERTY public final val x: T + FIELD PROPERTY_BACKING_FIELD public final val x: T + EXPRESSION_BODY + GET_VAR 'value-parameter x: T' type=T origin=INITIALIZE_PROPERTY_FROM_PARAMETER + FUN DEFAULT_PROPERTY_ACCESSOR public final fun (): T + BLOCK_BODY + RETURN type=kotlin.Nothing from='(): T' + GET_FIELD 'x: T' type=T origin=null + receiver: GET_VAR '' type=Test1 origin=null + FUN GENERATED_DATA_CLASS_MEMBER public final operator fun component1(): T + BLOCK_BODY + RETURN type=kotlin.Nothing from='component1(): T' + CALL '(): T' type=T origin=GET_PROPERTY + $this: GET_VAR '' type=Test1 origin=null + FUN GENERATED_DATA_CLASS_MEMBER public final fun copy(x: T = ...): Test1 + x: EXPRESSION_BODY + CALL '(): T' type=T origin=GET_PROPERTY + $this: GET_VAR '' type=Test1 origin=null + BLOCK_BODY + RETURN type=kotlin.Nothing from='copy(T = ...): Test1' + CALL 'constructor Test1(T)' type=Test1 origin=null + x: GET_VAR 'value-parameter x: T = ...' type=T origin=null + FUN GENERATED_DATA_CLASS_MEMBER public open override fun toString(): kotlin.String + BLOCK_BODY + RETURN type=kotlin.Nothing from='toString(): String' + STRING_CONCATENATION type=kotlin.String + CONST String type=kotlin.String value='Test1(' + CONST String type=kotlin.String value='x=' + CALL '(): T' type=T origin=GET_PROPERTY + $this: GET_VAR '' type=Test1 origin=null + CONST String type=kotlin.String value=')' + FUN GENERATED_DATA_CLASS_MEMBER public open override fun hashCode(): kotlin.Int + BLOCK_BODY + VAR IR_TEMPORARY_VARIABLE var tmp0_result: kotlin.Int + CONST Int type=kotlin.Int value='0' + SET_VAR 'tmp0_result: Int' type=kotlin.Unit origin=EQ + BLOCK type=kotlin.Int origin=null + VAR IR_TEMPORARY_VARIABLE val tmp1: T + CALL '(): T' type=T origin=GET_PROPERTY + $this: GET_VAR '' type=Test1 origin=null + WHEN type=kotlin.Int origin=null + BRANCH + if: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EQEQ + arg0: GET_VAR 'tmp1: T' type=T origin=null + arg1: CONST Null type=kotlin.Nothing? value='null' + then: CONST Int type=kotlin.Int value='0' + BRANCH + if: CONST Boolean type=kotlin.Boolean value='true' + then: CALL 'hashCode(): Int' type=kotlin.Int origin=null + $this: TYPE_OP type=kotlin.Any origin=IMPLICIT_CAST typeOperand=kotlin.Any + GET_VAR 'tmp1: T' type=T origin=null + RETURN type=kotlin.Nothing from='hashCode(): Int' + GET_VAR 'tmp0_result: Int' type=kotlin.Int origin=null + FUN GENERATED_DATA_CLASS_MEMBER public open override fun equals(other: kotlin.Any?): kotlin.Boolean + BLOCK_BODY + WHEN type=kotlin.Unit origin=null + BRANCH + if: CALL 'EQEQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EQEQEQ + arg0: GET_VAR '' type=Test1 origin=null + arg1: GET_VAR 'value-parameter other: Any?' type=kotlin.Any? origin=null + then: RETURN type=kotlin.Nothing from='equals(Any?): Boolean' + CONST Boolean type=kotlin.Boolean value='true' + WHEN type=kotlin.Unit origin=null + BRANCH + if: TYPE_OP type=kotlin.Boolean origin=NOT_INSTANCEOF typeOperand=Test1 + GET_VAR 'value-parameter other: Any?' type=kotlin.Any? origin=null + then: RETURN type=kotlin.Nothing from='equals(Any?): Boolean' + CONST Boolean type=kotlin.Boolean value='false' + VAR IR_TEMPORARY_VARIABLE val tmp0_other_with_cast: Test1 + TYPE_OP type=Test1 origin=CAST typeOperand=Test1 + GET_VAR 'value-parameter other: Any?' type=kotlin.Any? origin=null + WHEN type=kotlin.Unit origin=null + BRANCH + if: CALL 'NOT(Boolean): Boolean' type=kotlin.Boolean origin=EXCLEQ + arg0: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EXCLEQ + arg0: CALL '(): T' type=T origin=GET_PROPERTY + $this: GET_VAR '' type=Test1 origin=null + arg1: CALL '(): T' type=T origin=GET_PROPERTY + $this: GET_VAR 'tmp0_other_with_cast: Test1' type=Test1 origin=null + then: RETURN type=kotlin.Nothing from='equals(Any?): Boolean' + CONST Boolean type=kotlin.Boolean value='false' + RETURN type=kotlin.Nothing from='equals(Any?): Boolean' + CONST Boolean type=kotlin.Boolean value='true' diff --git a/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java b/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java index ec8df60af32..2f0fe1ef3d9 100644 --- a/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java @@ -74,6 +74,12 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase { doTest(fileName); } + @TestMetadata("dataClassesGeneric.kt") + public void testDataClassesGeneric() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/classes/dataClassesGeneric.kt"); + doTest(fileName); + } + @TestMetadata("delegatedImplementation.kt") public void testDelegatedImplementation() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/classes/delegatedImplementation.kt");