Skip to content

Commit 4569b95

Browse files
authored
Merge pull request #10313 from tamasvajk/kotlin-fix-vararg
Kotlin: Fix `vararg` extraction outside of method call
2 parents ed772e5 + 07038d0 commit 4569b95

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3105,7 +3105,13 @@ open class KotlinFileExtractor(
31053105
extractTypeOperatorCall(e, callable, exprParent.parent, exprParent.idx, exprParent.enclosingStmt)
31063106
}
31073107
is IrVararg -> {
3108-
logger.errorElement("Unexpected IrVararg", e)
3108+
if (e.elements.size != 1 || e.elements[0] !is IrSpreadElement) {
3109+
logger.errorElement("Unexpected IrVararg", e)
3110+
return
3111+
}
3112+
// There are lowered IR cases when the vararg expression is not within a call, such as
3113+
// val temp0 = [*expr]
3114+
extractExpression((e.elements[0] as IrSpreadElement).expression, callable, parent)
31093115
}
31103116
is IrGetObjectValue -> {
31113117
// For `object MyObject { ... }`, the .class has an

java/ql/test/kotlin/library-tests/vararg/args.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
diag
12
varargsParams
23
| test.kt:8:15:8:28 | xs | file://:0:0:0:0 | int[] |
34
| test.kt:12:26:12:39 | xs | file://:0:0:0:0 | int[] |
45
| test.kt:20:24:20:37 | xs | file://:0:0:0:0 | int[] |
56
| test.kt:24:50:24:63 | xs | file://:0:0:0:0 | int[] |
67
| test.kt:28:37:28:50 | xs | file://:0:0:0:0 | int[] |
8+
| test.kt:50:5:50:31 | s | file://:0:0:0:0 | String[] |
79
explicitVarargsArguments
810
| test.kt:12:50:12:51 | xs | test.kt:12:44:12:52 | this(...) |
911
| test.kt:38:25:38:29 | array | test.kt:38:5:38:30 | funWithOnlyVarArgs(...) |
1012
| test.kt:39:41:39:45 | array | test.kt:39:5:39:46 | funWithArgsAndVarArgs(...) |
1113
| test.kt:40:34:40:38 | array | test.kt:40:5:40:49 | funWithMiddleVarArgs(...) |
1214
| test.kt:44:27:44:31 | array | test.kt:44:5:44:32 | new HasVarargConstructor(...) |
1315
| test.kt:45:34:45:38 | array | test.kt:45:5:45:39 | new HasVarargConstructor(...) |
16+
| test.kt:55:15:55:35 | tmp0_s | test.kt:55:13:55:43 | new X(...) |
1417
implicitVarargsArguments
1518
| intList.kt:3:21:3:22 | 10 | intList.kt:3:14:3:31 | listOf(...) | 0 |
1619
| intList.kt:3:25:3:26 | 11 | intList.kt:3:14:3:31 | listOf(...) | 1 |
@@ -78,3 +81,6 @@ implicitVarargsArguments
7881
| test.kt:44:5:44:32 | new HasVarargConstructor(...) | 0 | test.kt:44:27:44:31 | array |
7982
| test.kt:45:5:45:39 | new HasVarargConstructor(...) | 0 | test.kt:45:27:45:29 | foo |
8083
| test.kt:45:5:45:39 | new HasVarargConstructor(...) | 1 | test.kt:45:34:45:38 | array |
84+
| test.kt:55:13:55:43 | new X(...) | 0 | test.kt:55:42:55:42 | 1 |
85+
| test.kt:55:13:55:43 | new X(...) | 1 | test.kt:55:15:55:35 | tmp0_s |
86+
| test.kt:55:22:55:35 | toTypedArray(...) | 0 | test.kt:55:19:55:20 | sl |

java/ql/test/kotlin/library-tests/vararg/args.ql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import java
2+
import semmle.code.java.Diagnostics
3+
4+
query predicate diag(Diagnostic d) { d.getMessage() = "Unexpected IrVararg" }
25

36
query predicate varargsParams(Parameter p, Type t) {
47
p.getCallable().fromSource() and

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
fun sink(sunk: Int) {
3-
3+
44
}
55

66
open class HasVarargConstructor {
@@ -44,3 +44,13 @@ fun myFun() {
4444
HasVarargConstructor(*array)
4545
HasVarargConstructor("foo", *array)
4646
}
47+
48+
open class X(
49+
i: Int,
50+
public vararg val s: String
51+
) { }
52+
53+
fun fn(sl: List<String>) {
54+
// reordered args:
55+
val x = X(s = sl.toTypedArray(), i = 1)
56+
}

0 commit comments

Comments
 (0)