Skip to content

Commit 71cce9c

Browse files
committed
Kotlin: Extract error expression for enumValues<T> calls
1 parent fd0d2ad commit 71cce9c

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,13 +1778,34 @@ open class KotlinFileExtractor(
17781778
return
17791779
}
17801780

1781-
val func = ((c.getTypeArgument(0) as? IrSimpleType)?.classifier?.owner as? IrClass)?.declarations?.findSubType<IrFunction> { it.name.asString() == fnName }
1782-
if (func == null) {
1783-
logger.errorElement("Couldn't find function $fnName on enum type", c)
1781+
val enumType = (c.getTypeArgument(0) as? IrSimpleType)?.classifier?.owner
1782+
if (enumType == null) {
1783+
logger.errorElement("Couldn't find type of enum type", c)
17841784
return
17851785
}
17861786

1787-
extractMethodAccess(func, false)
1787+
if (enumType is IrClass) {
1788+
val func = enumType.declarations.findSubType<IrFunction> { it.name.asString() == fnName }
1789+
if (func == null) {
1790+
logger.errorElement("Couldn't find function $fnName on enum type", c)
1791+
return
1792+
}
1793+
1794+
extractMethodAccess(func, false)
1795+
} else if (enumType is IrTypeParameter && enumType.isReified) {
1796+
// A call to `enumValues<T>()` is being extracted, where `T` is a reified type parameter of an `inline` function.
1797+
// We can't generate a valid expression here, because we would need to know the type of T on the call site.
1798+
val id = tw.getFreshIdLabel<DbErrorexpr>()
1799+
val type = useType(c.type)
1800+
1801+
tw.writeExprs_errorexpr(id, type.javaResult.id, parent, idx)
1802+
tw.writeExprsKotlinType(id, type.kotlinResult.id)
1803+
tw.writeHasLocation(id, tw.getLocation(c))
1804+
tw.writeCallableEnclosingExpr(id, callable)
1805+
tw.writeStatementEnclosingExpr(id, enclosingStmt)
1806+
} else {
1807+
logger.errorElement("Unexpected enum type rep ${enumType.javaClass}", c)
1808+
}
17881809
}
17891810

17901811
fun binopReceiver(id: Label<out DbExpr>, receiver: IrExpression?, receiverDescription: String) {

java/ql/test/kotlin/library-tests/exprs/PrintAst.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2839,6 +2839,7 @@ exprs.kt:
28392839
# 272| 0: [TypeAccess] T
28402840
# 272| 5: [BlockStmt] { ... }
28412841
# 272| 0: [ReturnStmt] return ...
2842+
# 272| 0: [ErrorExpr] <error expr>
28422843
# 274| 13: [Method] callToEnumValues
28432844
# 274| 3: [TypeAccess] Unit
28442845
# 274| 5: [BlockStmt] { ... }

java/ql/test/kotlin/library-tests/exprs/exprs.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,7 @@
17251725
| exprs.kt:268:14:268:14 | 1 | exprs.kt:261:1:270:1 | inPlaceOperators | IntegerLiteral |
17261726
| exprs.kt:272:8:272:66 | T | file://:0:0:0:0 | <none> | TypeAccess |
17271727
| exprs.kt:272:8:272:66 | T[] | file://:0:0:0:0 | <none> | TypeAccess |
1728+
| exprs.kt:272:52:272:66 | <error expr> | exprs.kt:272:8:272:66 | getEnumValues | ErrorExpr |
17281729
| exprs.kt:274:1:277:1 | Unit | file://:0:0:0:0 | <none> | TypeAccess |
17291730
| exprs.kt:275:5:275:23 | <implicit coercion to unit> | exprs.kt:274:1:277:1 | callToEnumValues | ImplicitCoercionToUnitExpr |
17301731
| exprs.kt:275:5:275:23 | Color | exprs.kt:274:1:277:1 | callToEnumValues | TypeAccess |

0 commit comments

Comments
 (0)