Move checkNullability function in State.kt file

This commit is contained in:
Ivan Kylchik
2020-06-17 13:21:17 +03:00
parent e57de9a08f
commit c386cbeb54
3 changed files with 12 additions and 14 deletions
@@ -252,7 +252,7 @@ class IrInterpreter(irModule: IrModuleFragment, private val bodyMap: Map<IdSigna
(valueArguments[i] ?: defaultValues[i])?.interpret()?.check { return@newFrame it }
?: stack.pushReturnValue(listOf<Any?>().toPrimitiveStateArray(expression.getVarargType(i)!!)) // if vararg is empty
if (stack.peekReturnValue().isNull() && !valueParametersSymbols[i].isNullable()) {
stack.peekReturnValue().checkNullability(valueParametersSymbols[i].owner.type) {
val method = irFunction.getCapitalizedFileName() + "." + irFunction.fqNameWhenAvailable
val parameter = valueParametersSymbols[i].owner.name
throw IllegalArgumentException("Parameter specified as non-null is null: method $method, parameter $parameter")
@@ -204,17 +204,4 @@ internal fun State?.getCorrectReceiverByFunction(irFunction: IrFunction): State?
return generateSequence(original) { it.superClass }.firstOrNull { it.irClass.thisReceiver == other } ?: this
}
internal fun State.checkNullability(irType: IrType?): State {
if (irType !is IrSimpleType) return this
if (this.isNull() && !irType.hasQuestionMark) {
throw NullPointerException()
}
return this
}
fun IrValueParameterSymbol.isNullable(): Boolean {
val type = this.owner.type as? IrSimpleType ?: return false
return type.isNullable()
}
internal fun IrFunction.getCapitalizedFileName() = this.file.name.replace(".kt", "Kt").capitalize()
@@ -55,4 +55,15 @@ internal fun State.isSubtypeOf(other: IrType): Boolean {
}
return this.irClass.defaultType.isSubtypeOfClass(other.classOrNull!!)
}
/**
* This method used to check if for not null parameter there was passed null argument.
*/
internal fun State.checkNullability(irType: IrType?, throwException: () -> Nothing = { throw NullPointerException() }): State {
if (irType !is IrSimpleType) return this
if (this.isNull() && !irType.isNullable()) {
throwException()
}
return this
}