Nabil
09/07/2020, 6:06 PMisIntOrNullableInt
is not available in KotlinBuiltIns
? (similar for example to isLongOrNullableLong
).
When using a property var age:Int?
isInt
returns false
(which is correct) isNullablePrimitiveType
returns true
(which is correct), but there's no mechanism to detect a return type is Int
or Int?
dmitriy.novozhilov
09/07/2020, 6:13 PMfun isIntOrNullableInt(type: KotlinType): Boolean {
return with(ClassicTypeCheckerContext()) {
KotlinBuiltIns.isInt(type.withNullability(true))
}
}
Nabil
09/07/2020, 7:56 PMfun IrType.isIntOrNullableInt(): Boolean {
return with(ClassicTypeCheckerContext(false)) {
KotlinBuiltIns.isInt(withNullability(true) as KotlinType)
}
}
method (edited to compile) throws a compiler error
java.lang.IllegalArgumentException: ClassicTypeSystemContext couldn't handle: org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl@ad907df4, class org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
dmitriy.novozhilov
09/07/2020, 9:08 PMIrType
and KotlinType
are different class hierarchies which are used in frontend and backend ir respectively. If you need check IrType
for int you should use IrTypeCheckerContext
and check like type.withNullability(true) == irBuiltins.intType
Nabil
09/09/2020, 7:34 AMwithNullability
is not available for IrType
fun isIntOrNullableInt(type: IrType, context: IrPluginContext): Boolean {
return with(IrTypeCheckerContext(context.irBuiltIns)) {
context.irBuiltIns.intType == type.withNullability(true)
}
}
dmitriy.novozhilov
09/09/2020, 9:20 AMwithNullability
is available only for simple types
add if (type !is IrSimpleType) return false
before return, and then it will workNabil
09/09/2020, 10:33 AMfalse
I'm calling it on the following IrType
[Kotlin Compiler] 2020-09-09T10:24:03.948Z DEBUG return type: org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl@ad907df4 dump : FUN DEFAULT_PROPERTY_ACCESSOR name:<get-age> visibility:public modality:FINAL <> ($shared.Person) returnType:<http://kotlin.Int|kotlin.Int>?
correspondingProperty: PROPERTY name:age visibility:public modality:FINAL [var]
$this: VALUE_PARAMETER name:<this> type:shared.Person
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun <get-age> (): <http://kotlin.Int|kotlin.Int>? declared in shared.Person'
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:age type:<http://kotlin.Int|kotlin.Int>? visibility:private' type=<http://kotlin.Int|kotlin.Int>? origin=null
receiver: GET_VAR '<this>: shared.Person declared in shared.Person.<get-age>' type=shared.Person origin=null
EVALUATION1 false
from
fun IrFunction.isReturningInt(irBuiltIns: IrBuiltIns) : Boolean {
return isPropertyAccessor
&& isGetter
&& returnType.isIntOrNullableInt(irBuiltIns)
}
fun IrType.isIntOrNullableInt(irBuiltIns: IrBuiltIns): Boolean {
if (this !is IrSimpleType) return false
return with(IrTypeCheckerContext(irBuiltIns)) {
irBuiltIns.intType == withNullability(true)
}
}
dmitriy.novozhilov
09/09/2020, 10:35 AMRoman Artemev [JB]
09/09/2020, 10:39 AMIrType.isNullable(): Boolean
in $kotlin/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeUtils.kt:38
dmitriy.novozhilov
09/09/2020, 10:40 AMIrType
is <http://kotlin.Int|kotlin.Int>
or <http://kotlin.Int|kotlin.Int>?
Roman Artemev [JB]
09/09/2020, 10:43 AMIrType.isInt()
for Int
in $kotlin/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/irTypePredicates.kt:58
which checks whether type is non-null <http://kotlin.int|kotlin.int>
.Roman Artemev [JB]
09/09/2020, 10:44 AMInt
or Int?
you could do like irType.makeNotNull().isInt()
Nabil
09/09/2020, 10:52 AM