diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/BranchingExpressionGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/BranchingExpressionGenerator.kt index c7c246bd01c..ff3e9eddcda 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/BranchingExpressionGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/BranchingExpressionGenerator.kt @@ -205,12 +205,22 @@ class BranchingExpressionGenerator(statementGenerator: StatementGenerator) : Sta val typeOperand = getOrFail(BindingContext.TYPE, ktCondition.typeReference) val irTypeOperand = typeOperand.toIrType() val typeSymbol = irTypeOperand.classifierOrNull ?: throw AssertionError("Not a classifier type: $typeOperand") - return IrTypeOperatorCallImpl( + val irInstanceOf = IrTypeOperatorCallImpl( ktCondition.startOffset, ktCondition.endOffset, context.irBuiltIns.booleanType, - IrTypeOperator.INSTANCEOF, irTypeOperand, typeSymbol, + IrTypeOperator.INSTANCEOF, + irTypeOperand, typeSymbol, irSubject.defaultLoad() ) + return if (ktCondition.isNegated) + IrUnaryPrimitiveImpl( + ktCondition.startOffset, ktCondition.endOffset, + context.irBuiltIns.booleanType, + IrStatementOrigin.EXCL, context.irBuiltIns.booleanNotSymbol, + irInstanceOf + ) + else + irInstanceOf } private fun generateInRangeCondition(irSubject: IrVariable, ktCondition: KtWhenConditionInRange): IrExpression { diff --git a/compiler/testData/ir/irText/expressions/floatingPointComparisons/whenByFloatingPoint.txt b/compiler/testData/ir/irText/expressions/floatingPointComparisons/whenByFloatingPoint.txt index b4f5194c77a..7c292fd3d9f 100644 --- a/compiler/testData/ir/irText/expressions/floatingPointComparisons/whenByFloatingPoint.txt +++ b/compiler/testData/ir/irText/expressions/floatingPointComparisons/whenByFloatingPoint.txt @@ -75,9 +75,10 @@ FILE fqName: fileName:/whenByFloatingPoint.kt GET_VAR 'value-parameter x: Any' type=kotlin.Any origin=null WHEN type=kotlin.Int origin=WHEN BRANCH - if: TYPE_OP type=kotlin.Boolean origin=INSTANCEOF typeOperand=kotlin.Double - typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:Double modality:FINAL visibility:public flags: superTypes:[kotlin.Number; kotlin.Comparable; java.io.Serializable] - GET_VAR 'tmp0_subject: Any' type=kotlin.Any origin=null + if: CALL 'NOT(Boolean): Boolean' type=kotlin.Boolean origin=EXCL + arg0: TYPE_OP type=kotlin.Boolean origin=INSTANCEOF typeOperand=kotlin.Double + typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:Double modality:FINAL visibility:public flags: superTypes:[kotlin.Number; kotlin.Comparable; java.io.Serializable] + GET_VAR 'tmp0_subject: Any' type=kotlin.Any origin=null then: CONST Int type=kotlin.Int value=-1 BRANCH if: CALL 'ieee754equals(Double?, Double?): Boolean' type=kotlin.Boolean origin=EQEQ diff --git a/compiler/testData/ir/irText/expressions/when.kt b/compiler/testData/ir/irText/expressions/when.kt index fb4c3d2243c..5c98ee339ad 100644 --- a/compiler/testData/ir/irText/expressions/when.kt +++ b/compiler/testData/ir/irText/expressions/when.kt @@ -7,6 +7,7 @@ fun testWithSubject(x: Any?) = null -> "null" A -> "A" is String -> "String" + !is Number -> "!Number" in setOf() -> "nothingness?" else -> "something" } @@ -16,6 +17,7 @@ fun test(x: Any?) = x == null -> "null" x == A -> "A" x is String -> "String" + x !is Number -> "!Number" x in setOf() -> "nothingness?" else -> "something" } diff --git a/compiler/testData/ir/irText/expressions/when.txt b/compiler/testData/ir/irText/expressions/when.txt index 9f104dfc978..7abb99d7b35 100644 --- a/compiler/testData/ir/irText/expressions/when.txt +++ b/compiler/testData/ir/irText/expressions/when.txt @@ -42,11 +42,19 @@ FILE fqName: fileName:/when.kt GET_VAR 'tmp0_subject: Any?' type=kotlin.Any? origin=null then: CONST String type=kotlin.String value=String BRANCH - if: CALL 'contains(Any) on Iterable: Boolean' type=kotlin.Boolean origin=IN - : kotlin.Any + if: CALL 'NOT(Boolean): Boolean' type=kotlin.Boolean origin=EXCL + arg0: TYPE_OP type=kotlin.Boolean origin=INSTANCEOF typeOperand=kotlin.Number + typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:Number modality:ABSTRACT visibility:public flags: superTypes:[kotlin.Any; java.io.Serializable] + GET_VAR 'tmp0_subject: Any?' type=kotlin.Any? origin=null + then: CONST String type=kotlin.String value=!Number + BRANCH + if: CALL 'contains(Number) on Iterable: Boolean' type=kotlin.Boolean origin=IN + : kotlin.Number $receiver: CALL 'setOf(): Set' type=kotlin.collections.Set origin=null : kotlin.Nothing - element: GET_VAR 'tmp0_subject: Any?' type=kotlin.Any? origin=null + element: TYPE_OP type=kotlin.Number origin=IMPLICIT_CAST typeOperand=kotlin.Number + typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:Number modality:ABSTRACT visibility:public flags: superTypes:[kotlin.Any; java.io.Serializable] + GET_VAR 'tmp0_subject: Any?' type=kotlin.Any? origin=null then: CONST String type=kotlin.String value=nothingness? BRANCH if: CONST Boolean type=kotlin.Boolean value=true @@ -72,11 +80,18 @@ FILE fqName: fileName:/when.kt GET_VAR 'value-parameter x: Any?' type=kotlin.Any? origin=null then: CONST String type=kotlin.String value=String BRANCH - if: CALL 'contains(Any) on Iterable: Boolean' type=kotlin.Boolean origin=IN - : kotlin.Any + if: TYPE_OP type=kotlin.Boolean origin=NOT_INSTANCEOF typeOperand=kotlin.Number + typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:Number modality:ABSTRACT visibility:public flags: superTypes:[kotlin.Any; java.io.Serializable] + GET_VAR 'value-parameter x: Any?' type=kotlin.Any? origin=null + then: CONST String type=kotlin.String value=!Number + BRANCH + if: CALL 'contains(Number) on Iterable: Boolean' type=kotlin.Boolean origin=IN + : kotlin.Number $receiver: CALL 'setOf(): Set' type=kotlin.collections.Set origin=null : kotlin.Nothing - element: GET_VAR 'value-parameter x: Any?' type=kotlin.Any? origin=null + element: TYPE_OP type=kotlin.Number origin=IMPLICIT_CAST typeOperand=kotlin.Number + typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:Number modality:ABSTRACT visibility:public flags: superTypes:[kotlin.Any; java.io.Serializable] + GET_VAR 'value-parameter x: Any?' type=kotlin.Any? origin=null then: CONST String type=kotlin.String value=nothingness? BRANCH if: CONST Boolean type=kotlin.Boolean value=true diff --git a/compiler/testData/ir/irText/expressions/whenWithSubjectVariable.txt b/compiler/testData/ir/irText/expressions/whenWithSubjectVariable.txt index 568444cb7d6..b6325606c7d 100644 --- a/compiler/testData/ir/irText/expressions/whenWithSubjectVariable.txt +++ b/compiler/testData/ir/irText/expressions/whenWithSubjectVariable.txt @@ -24,9 +24,10 @@ FILE fqName: fileName:/whenWithSubjectVariable.kt typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:String modality:FINAL visibility:public flags: superTypes:[kotlin.Comparable; kotlin.CharSequence; java.io.Serializable] GET_VAR 'y: Any' type=kotlin.Any origin=null BRANCH - if: TYPE_OP type=kotlin.Boolean origin=INSTANCEOF typeOperand=kotlin.Int - typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:Int modality:FINAL visibility:public flags: superTypes:[kotlin.Number; kotlin.Comparable; java.io.Serializable] - GET_VAR 'y: Any' type=kotlin.Any origin=null + if: CALL 'NOT(Boolean): Boolean' type=kotlin.Boolean origin=EXCL + arg0: TYPE_OP type=kotlin.Boolean origin=INSTANCEOF typeOperand=kotlin.Int + typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:Int modality:FINAL visibility:public flags: superTypes:[kotlin.Number; kotlin.Comparable; java.io.Serializable] + GET_VAR 'y: Any' type=kotlin.Any origin=null then: CONST Int type=kotlin.Int value=2 BRANCH if: CALL 'contains(Int): Boolean' type=kotlin.Boolean origin=IN