Skip to content

Commit b129c40

Browse files
authored
Merge pull request github#10278 from tamasvajk/kotlin-reified-enum
Kotlin: Extract error expression for `enumValues<T>` calls
2 parents bc17d06 + f84e62e commit b129c40

File tree

5 files changed

+70
-4
lines changed

5 files changed

+70
-4
lines changed

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,13 +1785,35 @@ open class KotlinFileExtractor(
17851785
return
17861786
}
17871787

1788-
val func = ((c.getTypeArgument(0) as? IrSimpleType)?.classifier?.owner as? IrClass)?.declarations?.findSubType<IrFunction> { it.name.asString() == fnName }
1789-
if (func == null) {
1790-
logger.errorElement("Couldn't find function $fnName on enum type", c)
1788+
val enumType = (c.getTypeArgument(0) as? IrSimpleType)?.classifier?.owner
1789+
if (enumType == null) {
1790+
logger.errorElement("Couldn't find type of enum type", c)
17911791
return
17921792
}
17931793

1794-
extractMethodAccess(func, false)
1794+
if (enumType is IrClass) {
1795+
val func = enumType.declarations.findSubType<IrFunction> { it.name.asString() == fnName }
1796+
if (func == null) {
1797+
logger.errorElement("Couldn't find function $fnName on enum type", c)
1798+
return
1799+
}
1800+
1801+
extractMethodAccess(func, false)
1802+
} else if (enumType is IrTypeParameter && enumType.isReified) {
1803+
// A call to `enumValues<T>()` is being extracted, where `T` is a reified type parameter of an `inline` function.
1804+
// We can't generate a valid expression here, because we would need to know the type of T on the call site.
1805+
// TODO: replace error expression with something that better shows this expression is unrepresentable.
1806+
val id = tw.getFreshIdLabel<DbErrorexpr>()
1807+
val type = useType(c.type)
1808+
1809+
tw.writeExprs_errorexpr(id, type.javaResult.id, parent, idx)
1810+
tw.writeExprsKotlinType(id, type.kotlinResult.id)
1811+
tw.writeHasLocation(id, tw.getLocation(c))
1812+
tw.writeCallableEnclosingExpr(id, callable)
1813+
tw.writeStatementEnclosingExpr(id, enclosingStmt)
1814+
} else {
1815+
logger.errorElement("Unexpected enum type rep ${enumType.javaClass}", c)
1816+
}
17951817
}
17961818

17971819
fun binopReceiver(id: Label<out DbExpr>, receiver: IrExpression?, receiverDescription: String) {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| exprs.kt:272:52:272:66 | <error expr> | ErrorExpr | unexpected dead end |
2+
| exprs.kt:272:52:272:66 | { ... } | BlockStmt | unexpected dead end |

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,6 +2832,28 @@ exprs.kt:
28322832
# 268| 0: [AssignRemExpr] ...%=...
28332833
# 268| 0: [VarAccess] updated
28342834
# 268| 1: [IntegerLiteral] 1
2835+
# 272| 12: [Method] getEnumValues
2836+
#-----| 2: (Generic Parameters)
2837+
# 272| 0: [TypeVariable] T
2838+
# 272| 3: [TypeAccess] T[]
2839+
# 272| 0: [TypeAccess] T
2840+
# 272| 5: [BlockStmt] { ... }
2841+
# 272| 0: [ReturnStmt] return ...
2842+
# 272| 0: [ErrorExpr] <error expr>
2843+
# 274| 13: [Method] callToEnumValues
2844+
# 274| 3: [TypeAccess] Unit
2845+
# 274| 5: [BlockStmt] { ... }
2846+
# 275| 0: [ExprStmt] <Expr>;
2847+
# 275| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
2848+
# 275| 0: [TypeAccess] Unit
2849+
# 275| 1: [MethodAccess] values(...)
2850+
# 275| -1: [TypeAccess] Color
2851+
# 276| 1: [ExprStmt] <Expr>;
2852+
# 276| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
2853+
# 276| 0: [TypeAccess] Unit
2854+
# 276| 1: [MethodAccess] getEnumValues(...)
2855+
# 276| -2: [TypeAccess] Color
2856+
# 276| -1: [TypeAccess] ExprsKt
28352857
# 142| 2: [Class] C
28362858
# 142| 1: [Constructor] C
28372859
#-----| 4: (Parameters)

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,19 @@
17231723
| exprs.kt:268:3:268:9 | updated | exprs.kt:261:1:270:1 | inPlaceOperators | VarAccess |
17241724
| exprs.kt:268:3:268:14 | ...%=... | exprs.kt:261:1:270:1 | inPlaceOperators | AssignRemExpr |
17251725
| exprs.kt:268:14:268:14 | 1 | exprs.kt:261:1:270:1 | inPlaceOperators | IntegerLiteral |
1726+
| exprs.kt:272:8:272:66 | T | file://:0:0:0:0 | <none> | TypeAccess |
1727+
| 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 |
1729+
| exprs.kt:274:1:277:1 | Unit | file://:0:0:0:0 | <none> | TypeAccess |
1730+
| exprs.kt:275:5:275:23 | <implicit coercion to unit> | exprs.kt:274:1:277:1 | callToEnumValues | ImplicitCoercionToUnitExpr |
1731+
| exprs.kt:275:5:275:23 | Color | exprs.kt:274:1:277:1 | callToEnumValues | TypeAccess |
1732+
| exprs.kt:275:5:275:23 | Unit | exprs.kt:274:1:277:1 | callToEnumValues | TypeAccess |
1733+
| exprs.kt:275:5:275:23 | values(...) | exprs.kt:274:1:277:1 | callToEnumValues | MethodAccess |
1734+
| exprs.kt:276:5:276:26 | <implicit coercion to unit> | exprs.kt:274:1:277:1 | callToEnumValues | ImplicitCoercionToUnitExpr |
1735+
| exprs.kt:276:5:276:26 | Color | exprs.kt:274:1:277:1 | callToEnumValues | TypeAccess |
1736+
| exprs.kt:276:5:276:26 | ExprsKt | exprs.kt:274:1:277:1 | callToEnumValues | TypeAccess |
1737+
| exprs.kt:276:5:276:26 | Unit | exprs.kt:274:1:277:1 | callToEnumValues | TypeAccess |
1738+
| exprs.kt:276:5:276:26 | getEnumValues(...) | exprs.kt:274:1:277:1 | callToEnumValues | MethodAccess |
17261739
| funcExprs.kt:1:1:1:46 | Unit | file://:0:0:0:0 | <none> | TypeAccess |
17271740
| funcExprs.kt:1:26:1:37 | Function0<Integer> | file://:0:0:0:0 | <none> | TypeAccess |
17281741
| funcExprs.kt:1:26:1:37 | Integer | file://:0:0:0:0 | <none> | TypeAccess |

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,10 @@ fun inPlaceOperators() {
268268
updated %= 1
269269

270270
}
271+
272+
inline fun <reified T : Enum<T>> getEnumValues() = enumValues<T>()
273+
274+
fun callToEnumValues() {
275+
enumValues<Color>()
276+
getEnumValues<Color>()
277+
}

0 commit comments

Comments
 (0)