Hi, is there a reason why `isIntOrNullableInt` is ...
# compiler
n
Hi, is there a reason why
isIntOrNullableInt
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?
d
I guess because there were no need in this function. You can write you own `isIntOrNullableInt`:
Copy code
fun isIntOrNullableInt(type: KotlinType): Boolean {
    return with(ClassicTypeCheckerContext()) {
        KotlinBuiltIns.isInt(type.withNullability(true))
    }
}
n
Thanks Dmitriy, the following
Copy code
fun IrType.isIntOrNullableInt(): Boolean {
    return with(ClassicTypeCheckerContext(false)) {
        KotlinBuiltIns.isInt(withNullability(true) as KotlinType)
    }
}
method (edited to compile) throws a compiler error
Copy code
java.lang.IllegalArgumentException: ClassicTypeSystemContext couldn't handle: org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl@ad907df4, class org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
d
IrType
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
n
sorry Dmitriy I'm a bit confused
withNullability
is not available for IrType
Copy code
fun isIntOrNullableInt(type: IrType, context: IrPluginContext): Boolean {
    return with(IrTypeCheckerContext(context.irBuiltIns)) {
        context.irBuiltIns.intType == type.withNullability(true)
    }
}
d
Oh, I forgot that
withNullability
is available only for simple types add
if (type !is IrSimpleType) return false
before return, and then it will work
n
Hmm, this is somehow always returning
false
I'm calling it on the following IrType
Copy code
[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
Copy code
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)
    }
}
d
cc @Roman Artemev [JB]
r
I am a bit out of context. Am I correct that question is how to check IrType if it’s nullable? If so, there is an util
IrType.isNullable(): Boolean
in
$kotlin/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeUtils.kt:38
d
@Nabil wants to check that
IrType
is
<http://kotlin.Int|kotlin.Int>
or
<http://kotlin.Int|kotlin.Int>?
r
So here is checker
IrType.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>
.
So to check if type is either
Int
or
Int?
you could do like
irType.makeNotNull().isInt()
n
Thanks this should work.