Fix IR generation for '!is' pattern

This commit is contained in:
Dmitry Petrov
2018-08-29 14:13:28 +03:00
parent 623adbfe4a
commit 4cf98fd3ce
5 changed files with 43 additions and 14 deletions
@@ -205,12 +205,22 @@ class BranchingExpressionGenerator(statementGenerator: StatementGenerator) : Sta
val typeOperand = getOrFail(BindingContext.TYPE, ktCondition.typeReference) val typeOperand = getOrFail(BindingContext.TYPE, ktCondition.typeReference)
val irTypeOperand = typeOperand.toIrType() val irTypeOperand = typeOperand.toIrType()
val typeSymbol = irTypeOperand.classifierOrNull ?: throw AssertionError("Not a classifier type: $typeOperand") val typeSymbol = irTypeOperand.classifierOrNull ?: throw AssertionError("Not a classifier type: $typeOperand")
return IrTypeOperatorCallImpl( val irInstanceOf = IrTypeOperatorCallImpl(
ktCondition.startOffset, ktCondition.endOffset, ktCondition.startOffset, ktCondition.endOffset,
context.irBuiltIns.booleanType, context.irBuiltIns.booleanType,
IrTypeOperator.INSTANCEOF, irTypeOperand, typeSymbol, IrTypeOperator.INSTANCEOF,
irTypeOperand, typeSymbol,
irSubject.defaultLoad() 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 { private fun generateInRangeCondition(irSubject: IrVariable, ktCondition: KtWhenConditionInRange): IrExpression {
@@ -75,9 +75,10 @@ FILE fqName:<root> fileName:/whenByFloatingPoint.kt
GET_VAR 'value-parameter x: Any' type=kotlin.Any origin=null GET_VAR 'value-parameter x: Any' type=kotlin.Any origin=null
WHEN type=kotlin.Int origin=WHEN WHEN type=kotlin.Int origin=WHEN
BRANCH BRANCH
if: TYPE_OP type=kotlin.Boolean origin=INSTANCEOF typeOperand=kotlin.Double if: CALL 'NOT(Boolean): Boolean' type=kotlin.Boolean origin=EXCL
typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:Double modality:FINAL visibility:public flags: superTypes:[kotlin.Number; kotlin.Comparable<kotlin.Double>; java.io.Serializable] arg0: TYPE_OP type=kotlin.Boolean origin=INSTANCEOF typeOperand=kotlin.Double
GET_VAR 'tmp0_subject: Any' type=kotlin.Any origin=null typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:Double modality:FINAL visibility:public flags: superTypes:[kotlin.Number; kotlin.Comparable<kotlin.Double>; java.io.Serializable]
GET_VAR 'tmp0_subject: Any' type=kotlin.Any origin=null
then: CONST Int type=kotlin.Int value=-1 then: CONST Int type=kotlin.Int value=-1
BRANCH BRANCH
if: CALL 'ieee754equals(Double?, Double?): Boolean' type=kotlin.Boolean origin=EQEQ if: CALL 'ieee754equals(Double?, Double?): Boolean' type=kotlin.Boolean origin=EQEQ
+2
View File
@@ -7,6 +7,7 @@ fun testWithSubject(x: Any?) =
null -> "null" null -> "null"
A -> "A" A -> "A"
is String -> "String" is String -> "String"
!is Number -> "!Number"
in setOf<Nothing>() -> "nothingness?" in setOf<Nothing>() -> "nothingness?"
else -> "something" else -> "something"
} }
@@ -16,6 +17,7 @@ fun test(x: Any?) =
x == null -> "null" x == null -> "null"
x == A -> "A" x == A -> "A"
x is String -> "String" x is String -> "String"
x !is Number -> "!Number"
x in setOf<Nothing>() -> "nothingness?" x in setOf<Nothing>() -> "nothingness?"
else -> "something" else -> "something"
} }
+21 -6
View File
@@ -42,11 +42,19 @@ FILE fqName:<root> fileName:/when.kt
GET_VAR 'tmp0_subject: Any?' type=kotlin.Any? origin=null GET_VAR 'tmp0_subject: Any?' type=kotlin.Any? origin=null
then: CONST String type=kotlin.String value=String then: CONST String type=kotlin.String value=String
BRANCH BRANCH
if: CALL 'contains(Any) on Iterable<Any>: Boolean' type=kotlin.Boolean origin=IN if: CALL 'NOT(Boolean): Boolean' type=kotlin.Boolean origin=EXCL
<T>: kotlin.Any 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<Number>: Boolean' type=kotlin.Boolean origin=IN
<T>: kotlin.Number
$receiver: CALL 'setOf(): Set<Nothing>' type=kotlin.collections.Set<kotlin.Nothing> origin=null $receiver: CALL 'setOf(): Set<Nothing>' type=kotlin.collections.Set<kotlin.Nothing> origin=null
<T>: kotlin.Nothing <T>: 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? then: CONST String type=kotlin.String value=nothingness?
BRANCH BRANCH
if: CONST Boolean type=kotlin.Boolean value=true if: CONST Boolean type=kotlin.Boolean value=true
@@ -72,11 +80,18 @@ FILE fqName:<root> fileName:/when.kt
GET_VAR 'value-parameter x: Any?' type=kotlin.Any? origin=null GET_VAR 'value-parameter x: Any?' type=kotlin.Any? origin=null
then: CONST String type=kotlin.String value=String then: CONST String type=kotlin.String value=String
BRANCH BRANCH
if: CALL 'contains(Any) on Iterable<Any>: Boolean' type=kotlin.Boolean origin=IN if: TYPE_OP type=kotlin.Boolean origin=NOT_INSTANCEOF typeOperand=kotlin.Number
<T>: kotlin.Any 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<Number>: Boolean' type=kotlin.Boolean origin=IN
<T>: kotlin.Number
$receiver: CALL 'setOf(): Set<Nothing>' type=kotlin.collections.Set<kotlin.Nothing> origin=null $receiver: CALL 'setOf(): Set<Nothing>' type=kotlin.collections.Set<kotlin.Nothing> origin=null
<T>: kotlin.Nothing <T>: 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? then: CONST String type=kotlin.String value=nothingness?
BRANCH BRANCH
if: CONST Boolean type=kotlin.Boolean value=true if: CONST Boolean type=kotlin.Boolean value=true
@@ -24,9 +24,10 @@ FILE fqName:<root> fileName:/whenWithSubjectVariable.kt
typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:String modality:FINAL visibility:public flags: superTypes:[kotlin.Comparable<kotlin.String>; kotlin.CharSequence; java.io.Serializable] typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:String modality:FINAL visibility:public flags: superTypes:[kotlin.Comparable<kotlin.String>; kotlin.CharSequence; java.io.Serializable]
GET_VAR 'y: Any' type=kotlin.Any origin=null GET_VAR 'y: Any' type=kotlin.Any origin=null
BRANCH BRANCH
if: TYPE_OP type=kotlin.Boolean origin=INSTANCEOF typeOperand=kotlin.Int if: CALL 'NOT(Boolean): Boolean' type=kotlin.Boolean origin=EXCL
typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:Int modality:FINAL visibility:public flags: superTypes:[kotlin.Number; kotlin.Comparable<kotlin.Int>; java.io.Serializable] arg0: TYPE_OP type=kotlin.Boolean origin=INSTANCEOF typeOperand=kotlin.Int
GET_VAR 'y: Any' type=kotlin.Any origin=null typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:Int modality:FINAL visibility:public flags: superTypes:[kotlin.Number; kotlin.Comparable<kotlin.Int>; java.io.Serializable]
GET_VAR 'y: Any' type=kotlin.Any origin=null
then: CONST Int type=kotlin.Int value=2 then: CONST Int type=kotlin.Int value=2
BRANCH BRANCH
if: CALL 'contains(Int): Boolean' type=kotlin.Boolean origin=IN if: CALL 'contains(Int): Boolean' type=kotlin.Boolean origin=IN