Skip to content

Commit 8c32758

Browse files
authored
Merge pull request #9829 from smowton/smowton/fix/kotlin-underscore-parameter-names
Kotlin: Don't extract a name for a '_' parameter
2 parents 40d25cb + 0a351b7 commit 8c32758

File tree

13 files changed

+146
-6
lines changed

13 files changed

+146
-6
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.github.codeql.comments.CommentExtractor
44
import com.github.codeql.utils.*
55
import com.github.codeql.utils.versions.functionN
66
import com.github.codeql.utils.versions.getIrStubFromDescriptor
7+
import com.github.codeql.utils.versions.isUnderscoreParameter
78
import com.semmle.extractor.java.OdasaOutput
89
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
910
import org.jetbrains.kotlin.backend.common.lower.parents
@@ -642,7 +643,7 @@ open class KotlinFileExtractor(
642643
if (extractTypeAccess) {
643644
extractTypeAccessRecursive(substitutedType, location, id, -1)
644645
}
645-
val syntheticParameterNames = (vp.parent as? IrFunction)?.let { hasSynthesizedParameterNames(it) } ?: true
646+
val syntheticParameterNames = isUnderscoreParameter(vp) || ((vp.parent as? IrFunction)?.let { hasSynthesizedParameterNames(it) } ?: true)
646647
return extractValueParameter(id, substitutedType, vp.name.asString(), location, parent, idx, useValueParameter(vp, parentSourceDeclaration), vp.isVararg, syntheticParameterNames)
647648
}
648649
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.codeql.utils.versions
2+
3+
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
4+
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
5+
import org.jetbrains.kotlin.psi.KtParameter
6+
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
7+
import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore
8+
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
9+
10+
@OptIn(ObsoleteDescriptorBasedAPI::class)
11+
fun isUnderscoreParameter(vp: IrValueParameter) =
12+
try {
13+
DescriptorToSourceUtils.getSourceFromDescriptor(vp.descriptor)
14+
?.safeAs<KtParameter>()?.isSingleUnderscore == true
15+
} catch(e: NotImplementedError) {
16+
// Some kinds of descriptor throw in `getSourceFromDescriptor` as that method is not normally expected to
17+
// be applied to synthetic functions.
18+
false
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.codeql.utils.versions
2+
3+
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
4+
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
5+
import org.jetbrains.kotlin.psi.KtParameter
6+
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
7+
import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore
8+
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
9+
10+
@OptIn(ObsoleteDescriptorBasedAPI::class)
11+
fun isUnderscoreParameter(vp: IrValueParameter) =
12+
try {
13+
DescriptorToSourceUtils.getSourceFromDescriptor(vp.descriptor)
14+
?.safeAs<KtParameter>()?.isSingleUnderscore == true
15+
} catch(e: NotImplementedError) {
16+
// Some kinds of descriptor throw in `getSourceFromDescriptor` as that method is not normally expected to
17+
// be applied to synthetic functions.
18+
false
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.codeql.utils.versions
2+
3+
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
4+
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
5+
import org.jetbrains.kotlin.psi.KtParameter
6+
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
7+
import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore
8+
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
9+
10+
@OptIn(ObsoleteDescriptorBasedAPI::class)
11+
fun isUnderscoreParameter(vp: IrValueParameter) =
12+
try {
13+
DescriptorToSourceUtils.getSourceFromDescriptor(vp.descriptor)
14+
?.safeAs<KtParameter>()?.isSingleUnderscore == true
15+
} catch(e: NotImplementedError) {
16+
// Some kinds of descriptor throw in `getSourceFromDescriptor` as that method is not normally expected to
17+
// be applied to synthetic functions.
18+
false
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.codeql.utils.versions
2+
3+
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
4+
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
5+
import org.jetbrains.kotlin.psi.KtParameter
6+
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
7+
import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore
8+
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
9+
10+
@OptIn(ObsoleteDescriptorBasedAPI::class)
11+
fun isUnderscoreParameter(vp: IrValueParameter) =
12+
try {
13+
DescriptorToSourceUtils.getSourceFromDescriptor(vp.descriptor)
14+
?.safeAs<KtParameter>()?.isSingleUnderscore == true
15+
} catch(e: NotImplementedError) {
16+
// Some kinds of descriptor throw in `getSourceFromDescriptor` as that method is not normally expected to
17+
// be applied to synthetic functions.
18+
false
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.codeql.utils.versions
2+
3+
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
4+
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
5+
import org.jetbrains.kotlin.psi.KtParameter
6+
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
7+
import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore
8+
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
9+
10+
@OptIn(ObsoleteDescriptorBasedAPI::class)
11+
fun isUnderscoreParameter(vp: IrValueParameter) =
12+
try {
13+
DescriptorToSourceUtils.getSourceFromDescriptor(vp.descriptor)
14+
?.safeAs<KtParameter>()?.isSingleUnderscore == true
15+
} catch(e: NotImplementedError) {
16+
// Some kinds of descriptor throw in `getSourceFromDescriptor` as that method is not normally expected to
17+
// be applied to synthetic functions.
18+
false
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.codeql.utils.versions
2+
3+
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
4+
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
5+
import org.jetbrains.kotlin.psi.KtParameter
6+
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
7+
import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore
8+
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
9+
10+
@OptIn(ObsoleteDescriptorBasedAPI::class)
11+
fun isUnderscoreParameter(vp: IrValueParameter) =
12+
try {
13+
DescriptorToSourceUtils.getSourceFromDescriptor(vp.descriptor)
14+
?.safeAs<KtParameter>()?.isSingleUnderscore == true
15+
} catch(e: NotImplementedError) {
16+
// Some kinds of descriptor throw in `getSourceFromDescriptor` as that method is not normally expected to
17+
// be applied to synthetic functions.
18+
false
19+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.github.codeql.utils.versions
2+
3+
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
4+
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
5+
6+
fun isUnderscoreParameter(vp: IrValueParameter) = vp.origin == IrDeclarationOrigin.UNDERSCORE_PARAMETER
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.github.codeql.utils.versions
2+
3+
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
4+
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
5+
6+
fun isUnderscoreParameter(vp: IrValueParameter) = vp.origin == IrDeclarationOrigin.UNDERSCORE_PARAMETER

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3583,7 +3583,7 @@ funcExprs.kt:
35833583
# 27| 2: [Method] invoke
35843584
# 27| 3: [TypeAccess] int
35853585
#-----| 4: (Parameters)
3586-
# 27| 0: [Parameter] <anonymous parameter 0>
3586+
# 27| 0: [Parameter] p0
35873587
# 27| 0: [TypeAccess] int
35883588
# 27| 5: [BlockStmt] { ... }
35893589
# 27| 0: [ReturnStmt] return ...
@@ -3629,9 +3629,9 @@ funcExprs.kt:
36293629
# 30| 2: [Method] invoke
36303630
# 30| 3: [TypeAccess] int
36313631
#-----| 4: (Parameters)
3632-
# 30| 0: [Parameter] <anonymous parameter 0>
3632+
# 30| 0: [Parameter] p0
36333633
# 30| 0: [TypeAccess] int
3634-
# 30| 1: [Parameter] <anonymous parameter 1>
3634+
# 30| 1: [Parameter] p1
36353635
# 30| 0: [TypeAccess] int
36363636
# 30| 5: [BlockStmt] { ... }
36373637
# 30| 0: [ReturnStmt] return ...
@@ -3652,9 +3652,9 @@ funcExprs.kt:
36523652
# 31| 2: [Method] invoke
36533653
# 31| 3: [TypeAccess] int
36543654
#-----| 4: (Parameters)
3655-
# 31| 0: [Parameter] <anonymous parameter 0>
3655+
# 31| 0: [Parameter] p0
36563656
# 31| 0: [TypeAccess] int
3657-
# 31| 1: [Parameter] <anonymous parameter 1>
3657+
# 31| 1: [Parameter] p1
36583658
# 31| 0: [TypeAccess] int
36593659
# 31| 5: [BlockStmt] { ... }
36603660
# 31| 0: [ReturnStmt] return ...

0 commit comments

Comments
 (0)