Kotlin: Extract error expression for enumValues<T> calls

This commit is contained in:
Tamas Vajk
2022-09-02 15:42:00 +02:00
parent fd0d2ad767
commit 71cce9cf28
3 changed files with 27 additions and 4 deletions

View File

@@ -1778,13 +1778,34 @@ open class KotlinFileExtractor(
return
}
val func = ((c.getTypeArgument(0) as? IrSimpleType)?.classifier?.owner as? IrClass)?.declarations?.findSubType<IrFunction> { it.name.asString() == fnName }
if (func == null) {
logger.errorElement("Couldn't find function $fnName on enum type", c)
val enumType = (c.getTypeArgument(0) as? IrSimpleType)?.classifier?.owner
if (enumType == null) {
logger.errorElement("Couldn't find type of enum type", c)
return
}
extractMethodAccess(func, false)
if (enumType is IrClass) {
val func = enumType.declarations.findSubType<IrFunction> { it.name.asString() == fnName }
if (func == null) {
logger.errorElement("Couldn't find function $fnName on enum type", c)
return
}
extractMethodAccess(func, false)
} else if (enumType is IrTypeParameter && enumType.isReified) {
// A call to `enumValues<T>()` is being extracted, where `T` is a reified type parameter of an `inline` function.
// We can't generate a valid expression here, because we would need to know the type of T on the call site.
val id = tw.getFreshIdLabel<DbErrorexpr>()
val type = useType(c.type)
tw.writeExprs_errorexpr(id, type.javaResult.id, parent, idx)
tw.writeExprsKotlinType(id, type.kotlinResult.id)
tw.writeHasLocation(id, tw.getLocation(c))
tw.writeCallableEnclosingExpr(id, callable)
tw.writeStatementEnclosingExpr(id, enclosingStmt)
} else {
logger.errorElement("Unexpected enum type rep ${enumType.javaClass}", c)
}
}
fun binopReceiver(id: Label<out DbExpr>, receiver: IrExpression?, receiverDescription: String) {