Skip to content

Commit 01a2d16

Browse files
committed
Kotlin: Fix type access expressions in enum constructor calls
1 parent 9979fa3 commit 01a2d16

File tree

6 files changed

+24
-12
lines changed

6 files changed

+24
-12
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2482,11 +2482,23 @@ open class KotlinFileExtractor(
24822482

24832483
if (e is IrConstructorCall) {
24842484
extractConstructorTypeAccess(eType, typeAccessType, e.symbol, locId, id, -3, callable, enclosingStmt)
2485-
} else {
2486-
val typeAccessId =
2487-
extractTypeAccess(typeAccessType, locId, id, -3, callable, enclosingStmt)
2485+
} else if (e is IrEnumConstructorCall) {
2486+
val enumClass = e.symbol.owner.parent as? IrClass
2487+
if (enumClass == null) {
2488+
logger.warnElement("Couldn't find declaring class of enum constructor call", e)
2489+
return
2490+
}
24882491

2489-
extractTypeArguments(e, typeAccessId, callable, enclosingStmt)
2492+
val args = (0 until e.typeArgumentsCount).map { e.getTypeArgument(it) }.requireNoNullsOrNull()
2493+
if (args == null) {
2494+
logger.warnElement("Found null type argument in enum constructor call", e)
2495+
return
2496+
}
2497+
2498+
val enumType = enumClass.typeWith(args)
2499+
extractConstructorTypeAccess(enumType, useType(enumType), e.symbol, locId, id, -3, callable, enclosingStmt)
2500+
} else {
2501+
logger.errorElement("Unexpected constructor call type: ${e.javaClass}", e)
24902502
}
24912503
}
24922504

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ classes.kt:
134134
# 49| 5: [BlockStmt] { ... }
135135
# 49| 0: [ExprStmt] <Expr>;
136136
# 49| 0: [ClassInstanceExpr] new Enum(...)
137-
# 49| -3: [TypeAccess] Unit
137+
# 49| -3: [TypeAccess] Enum<Direction>
138138
# 49| 0: [TypeAccess] Direction
139139
# 49| 1: [BlockStmt] { ... }
140140
# 50| 5: [FieldDeclaration] Direction NORTH;
@@ -169,7 +169,7 @@ classes.kt:
169169
# 53| 5: [BlockStmt] { ... }
170170
# 53| 0: [ExprStmt] <Expr>;
171171
# 53| 0: [ClassInstanceExpr] new Enum(...)
172-
# 53| -3: [TypeAccess] Unit
172+
# 53| -3: [TypeAccess] Enum<Color>
173173
# 53| 0: [TypeAccess] Color
174174
# 53| 1: [BlockStmt] { ... }
175175
# 53| 0: [ExprStmt] <Expr>;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2909,7 +2909,7 @@ exprs.kt:
29092909
# 168| 5: [BlockStmt] { ... }
29102910
# 168| 0: [ExprStmt] <Expr>;
29112911
# 168| 0: [ClassInstanceExpr] new Enum(...)
2912-
# 168| -3: [TypeAccess] Unit
2912+
# 168| -3: [TypeAccess] Enum<Direction>
29132913
# 168| 0: [TypeAccess] Direction
29142914
# 168| 1: [BlockStmt] { ... }
29152915
# 169| 5: [FieldDeclaration] Direction NORTH;
@@ -2944,7 +2944,7 @@ exprs.kt:
29442944
# 172| 5: [BlockStmt] { ... }
29452945
# 172| 0: [ExprStmt] <Expr>;
29462946
# 172| 0: [ClassInstanceExpr] new Enum(...)
2947-
# 172| -3: [TypeAccess] Unit
2947+
# 172| -3: [TypeAccess] Enum<Color>
29482948
# 172| 0: [TypeAccess] Color
29492949
# 172| 1: [BlockStmt] { ... }
29502950
# 172| 0: [ExprStmt] <Expr>;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@
14461446
| exprs.kt:164:9:164:21 | ...=... | exprs.kt:159:1:166:1 | foo | AssignExpr |
14471447
| exprs.kt:164:21:164:21 | 3 | exprs.kt:159:1:166:1 | foo | IntegerLiteral |
14481448
| exprs.kt:168:1:170:1 | Direction | exprs.kt:168:6:170:1 | Direction | TypeAccess |
1449-
| exprs.kt:168:1:170:1 | Unit | exprs.kt:168:6:170:1 | Direction | TypeAccess |
1449+
| exprs.kt:168:1:170:1 | Enum<Direction> | exprs.kt:168:6:170:1 | Direction | TypeAccess |
14501450
| exprs.kt:168:1:170:1 | new Enum(...) | exprs.kt:168:6:170:1 | Direction | ClassInstanceExpr |
14511451
| exprs.kt:169:5:169:10 | ...=... | exprs.kt:0:0:0:0 | <clinit> | KtInitializerAssignExpr |
14521452
| exprs.kt:169:5:169:10 | Direction | exprs.kt:0:0:0:0 | <clinit> | TypeAccess |
@@ -1473,7 +1473,7 @@
14731473
| exprs.kt:169:25:169:28 | Direction.EAST | exprs.kt:0:0:0:0 | <clinit> | VarAccess |
14741474
| exprs.kt:169:25:169:28 | new Direction(...) | exprs.kt:0:0:0:0 | <clinit> | ClassInstanceExpr |
14751475
| exprs.kt:172:1:176:1 | Color | exprs.kt:172:6:176:1 | Color | TypeAccess |
1476-
| exprs.kt:172:1:176:1 | Unit | exprs.kt:172:6:176:1 | Color | TypeAccess |
1476+
| exprs.kt:172:1:176:1 | Enum<Color> | exprs.kt:172:6:176:1 | Color | TypeAccess |
14771477
| exprs.kt:172:1:176:1 | new Enum(...) | exprs.kt:172:6:176:1 | Color | ClassInstanceExpr |
14781478
| exprs.kt:172:18:172:29 | ...=... | exprs.kt:172:6:176:1 | Color | KtInitializerAssignExpr |
14791479
| exprs.kt:172:18:172:29 | int | file://:0:0:0:0 | <none> | TypeAccess |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ A.kt:
8686
# 23| 5: [BlockStmt] { ... }
8787
# 23| 0: [ExprStmt] <Expr>;
8888
# 23| 0: [ClassInstanceExpr] new Enum(...)
89-
# 23| -3: [TypeAccess] Unit
89+
# 23| -3: [TypeAccess] Enum<Enu>
9090
# 23| 0: [TypeAccess] Enu
9191
# 23| 1: [BlockStmt] { ... }
9292
# 24| 5: [FieldDeclaration] Enu A;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@
199199
| enumClass.kt:0:0:0:0 | EnumClass | TypeAccess |
200200
| enumClass.kt:0:0:0:0 | EnumClass[] | TypeAccess |
201201
| enumClass.kt:0:0:0:0 | String | TypeAccess |
202+
| enumClass.kt:1:1:4:1 | Enum<EnumClass> | TypeAccess |
202203
| enumClass.kt:1:1:4:1 | EnumClass | TypeAccess |
203-
| enumClass.kt:1:1:4:1 | Unit | TypeAccess |
204204
| enumClass.kt:1:1:4:1 | new Enum(...) | ClassInstanceExpr |
205205
| enumClass.kt:1:22:1:31 | ...=... | KtInitializerAssignExpr |
206206
| enumClass.kt:1:22:1:31 | int | TypeAccess |

0 commit comments

Comments
 (0)