Skip to content

Commit 9358070

Browse files
authored
Merge pull request #10506 from tamasvajk/kotlin-enum-type-access
Kotlin: Fix type access expressions in enum constructor calls
2 parents 8a6d56a + 01a2d16 commit 9358070

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
@@ -2498,11 +2498,23 @@ open class KotlinFileExtractor(
24982498

24992499
if (e is IrConstructorCall) {
25002500
extractConstructorTypeAccess(eType, typeAccessType, e.symbol, locId, id, -3, callable, enclosingStmt)
2501-
} else {
2502-
val typeAccessId =
2503-
extractTypeAccess(typeAccessType, locId, id, -3, callable, enclosingStmt)
2501+
} else if (e is IrEnumConstructorCall) {
2502+
val enumClass = e.symbol.owner.parent as? IrClass
2503+
if (enumClass == null) {
2504+
logger.warnElement("Couldn't find declaring class of enum constructor call", e)
2505+
return
2506+
}
25042507

2505-
extractTypeArguments(e, typeAccessId, callable, enclosingStmt)
2508+
val args = (0 until e.typeArgumentsCount).map { e.getTypeArgument(it) }.requireNoNullsOrNull()
2509+
if (args == null) {
2510+
logger.warnElement("Found null type argument in enum constructor call", e)
2511+
return
2512+
}
2513+
2514+
val enumType = enumClass.typeWith(args)
2515+
extractConstructorTypeAccess(enumType, useType(enumType), e.symbol, locId, id, -3, callable, enclosingStmt)
2516+
} else {
2517+
logger.errorElement("Unexpected constructor call type: ${e.javaClass}", e)
25062518
}
25072519
}
25082520

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ classes.kt:
158158
# 49| 5: [BlockStmt] { ... }
159159
# 49| 0: [ExprStmt] <Expr>;
160160
# 49| 0: [ClassInstanceExpr] new Enum(...)
161-
# 49| -3: [TypeAccess] Unit
161+
# 49| -3: [TypeAccess] Enum<Direction>
162162
# 49| 0: [TypeAccess] Direction
163163
# 49| 1: [BlockStmt] { ... }
164164
# 50| 5: [FieldDeclaration] Direction NORTH;
@@ -193,7 +193,7 @@ classes.kt:
193193
# 53| 5: [BlockStmt] { ... }
194194
# 53| 0: [ExprStmt] <Expr>;
195195
# 53| 0: [ClassInstanceExpr] new Enum(...)
196-
# 53| -3: [TypeAccess] Unit
196+
# 53| -3: [TypeAccess] Enum<Color>
197197
# 53| 0: [TypeAccess] Color
198198
# 53| 1: [BlockStmt] { ... }
199199
# 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)