From 608f99bd0d7bb36f2a26261b4b981aee9c492e46 Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Mon, 5 Sep 2022 10:05:08 +0200 Subject: [PATCH 1/2] Kotlin: Add test case for various array `set` operator overloads --- .../operator-overloads/PrintAst.expected | 68 ++++++++++++++----- .../operator-overloads/test.expected | 2 + .../library-tests/operator-overloads/test.kt | 5 ++ 3 files changed, 57 insertions(+), 18 deletions(-) diff --git a/java/ql/test/kotlin/library-tests/operator-overloads/PrintAst.expected b/java/ql/test/kotlin/library-tests/operator-overloads/PrintAst.expected index 4664710cd04..8bcfc4ec183 100644 --- a/java/ql/test/kotlin/library-tests/operator-overloads/PrintAst.expected +++ b/java/ql/test/kotlin/library-tests/operator-overloads/PrintAst.expected @@ -30,30 +30,62 @@ test.kt: # 4| -1: [VarAccess] mt # 4| 0: [IntegerLiteral] 1 # 4| 1: [IntegerLiteral] 2 -# 7| 2: [ExtensionMethod] get -# 7| 3: [TypeAccess] String +# 6| 3: [ExprStmt] ; +# 6| 0: [ImplicitCoercionToUnitExpr] +# 6| 0: [TypeAccess] Unit +# 7| 4: [ExprStmt] ; +# 7| 0: [ImplicitCoercionToUnitExpr] +# 7| 0: [TypeAccess] Unit +# 10| 2: [ExtensionMethod] get +# 10| 3: [TypeAccess] String #-----| 4: (Parameters) -# 7| 0: [Parameter] -# 7| 0: [TypeAccess] byte[] -# 7| 1: [Parameter] i -# 7| 0: [TypeAccess] int -# 7| 2: [Parameter] j -# 7| 0: [TypeAccess] int -# 7| 5: [BlockStmt] { ... } -# 7| 0: [ReturnStmt] return ... -# 7| 0: [StringLiteral] -# 10| 2: [Class] C -# 10| 1: [Constructor] C +# 10| 0: [Parameter] +# 10| 0: [TypeAccess] byte[] +# 10| 1: [Parameter] i +# 10| 0: [TypeAccess] int +# 10| 2: [Parameter] j +# 10| 0: [TypeAccess] int # 10| 5: [BlockStmt] { ... } -# 10| 0: [SuperConstructorInvocationStmt] super(...) -# 10| 1: [BlockStmt] { ... } -# 11| 2: [Method] get +# 10| 0: [ReturnStmt] return ... +# 10| 0: [StringLiteral] +# 11| 3: [ExtensionMethod] set # 11| 3: [TypeAccess] String #-----| 4: (Parameters) -# 11| 0: [Parameter] i +# 11| 0: [Parameter] +# 11| 0: [TypeAccess] byte[] +# 11| 1: [Parameter] i # 11| 0: [TypeAccess] int -# 11| 1: [Parameter] j +# 11| 2: [Parameter] j +# 11| 0: [TypeAccess] int +# 11| 3: [Parameter] k # 11| 0: [TypeAccess] int # 11| 5: [BlockStmt] { ... } # 11| 0: [ReturnStmt] return ... # 11| 0: [StringLiteral] +# 12| 4: [ExtensionMethod] set +# 12| 3: [TypeAccess] String +#-----| 4: (Parameters) +# 12| 0: [Parameter] +# 12| 0: [TypeAccess] byte[] +# 12| 1: [Parameter] i +# 12| 0: [TypeAccess] int +# 12| 2: [Parameter] c +# 12| 0: [TypeAccess] C +# 12| 5: [BlockStmt] { ... } +# 12| 0: [ReturnStmt] return ... +# 12| 0: [StringLiteral] +# 15| 2: [Class] C +# 15| 1: [Constructor] C +# 15| 5: [BlockStmt] { ... } +# 15| 0: [SuperConstructorInvocationStmt] super(...) +# 15| 1: [BlockStmt] { ... } +# 16| 2: [Method] get +# 16| 3: [TypeAccess] String +#-----| 4: (Parameters) +# 16| 0: [Parameter] i +# 16| 0: [TypeAccess] int +# 16| 1: [Parameter] j +# 16| 0: [TypeAccess] int +# 16| 5: [BlockStmt] { ... } +# 16| 0: [ReturnStmt] return ... +# 16| 0: [StringLiteral] diff --git a/java/ql/test/kotlin/library-tests/operator-overloads/test.expected b/java/ql/test/kotlin/library-tests/operator-overloads/test.expected index e69de29bb2d..afe544b0e6e 100644 --- a/java/ql/test/kotlin/library-tests/operator-overloads/test.expected +++ b/java/ql/test/kotlin/library-tests/operator-overloads/test.expected @@ -0,0 +1,2 @@ +| test.kt:6:5:6:13 | Unexpected Array.set function signature | +| test.kt:7:5:7:10 | Unexpected Array.set function signature | diff --git a/java/ql/test/kotlin/library-tests/operator-overloads/test.kt b/java/ql/test/kotlin/library-tests/operator-overloads/test.kt index 950b0914fe0..d332c995148 100644 --- a/java/ql/test/kotlin/library-tests/operator-overloads/test.kt +++ b/java/ql/test/kotlin/library-tests/operator-overloads/test.kt @@ -2,9 +2,14 @@ fun fn(arr: ByteArray, mt: C) { arr[1] arr[1, 2] mt[1, 2] + + arr[1, 2] = 3 + arr[1] = C() } public operator fun ByteArray.get(i: Int, j: Int) = "" +public operator fun ByteArray.set(i: Int, j: Int, k: Int) = "" +public operator fun ByteArray.set(i: Int, c: C) = "" public class C { From 6f7f7606828b8e7baf712bb7dbf1331ad7c46a2c Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Mon, 5 Sep 2022 10:19:35 +0200 Subject: [PATCH 2/2] Kotlin: Fix array `set` operator extraction --- .../src/main/kotlin/KotlinFileExtractor.kt | 2 +- .../operator-overloads/PrintAst.expected | 12 ++++++++++++ .../library-tests/operator-overloads/test.expected | 2 -- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt b/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt index 67b5851df87..f0a41ecc93e 100644 --- a/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt +++ b/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt @@ -2208,7 +2208,7 @@ open class KotlinFileExtractor( tw.writeExprsKotlinType(id, type.kotlinResult.id) binopDisp(id) } - isFunction(target, "kotlin", "(some array type)", { isArrayType(it) }, "set") && c.origin == IrStatementOrigin.EQ -> { + isFunction(target, "kotlin", "(some array type)", { isArrayType(it) }, "set") && c.origin == IrStatementOrigin.EQ && c.dispatchReceiver != null -> { val array = c.dispatchReceiver val arrayIdx = c.getValueArgument(0) val assignedValue = c.getValueArgument(1) diff --git a/java/ql/test/kotlin/library-tests/operator-overloads/PrintAst.expected b/java/ql/test/kotlin/library-tests/operator-overloads/PrintAst.expected index 8bcfc4ec183..af62c3e412e 100644 --- a/java/ql/test/kotlin/library-tests/operator-overloads/PrintAst.expected +++ b/java/ql/test/kotlin/library-tests/operator-overloads/PrintAst.expected @@ -33,9 +33,21 @@ test.kt: # 6| 3: [ExprStmt] ; # 6| 0: [ImplicitCoercionToUnitExpr] # 6| 0: [TypeAccess] Unit +# 6| 1: [MethodAccess] set(...) +# 6| -1: [TypeAccess] TestKt +# 6| 0: [VarAccess] arr +# 6| 1: [IntegerLiteral] 1 +# 6| 2: [IntegerLiteral] 2 +# 6| 3: [IntegerLiteral] 3 # 7| 4: [ExprStmt] ; # 7| 0: [ImplicitCoercionToUnitExpr] # 7| 0: [TypeAccess] Unit +# 7| 1: [MethodAccess] set(...) +# 7| -1: [TypeAccess] TestKt +# 7| 0: [VarAccess] arr +# 7| 1: [IntegerLiteral] 1 +# 7| 2: [ClassInstanceExpr] new C(...) +# 7| -3: [TypeAccess] C # 10| 2: [ExtensionMethod] get # 10| 3: [TypeAccess] String #-----| 4: (Parameters) diff --git a/java/ql/test/kotlin/library-tests/operator-overloads/test.expected b/java/ql/test/kotlin/library-tests/operator-overloads/test.expected index afe544b0e6e..e69de29bb2d 100644 --- a/java/ql/test/kotlin/library-tests/operator-overloads/test.expected +++ b/java/ql/test/kotlin/library-tests/operator-overloads/test.expected @@ -1,2 +0,0 @@ -| test.kt:6:5:6:13 | Unexpected Array.set function signature | -| test.kt:7:5:7:10 | Unexpected Array.set function signature |