Skip to content

Commit b89b6e4

Browse files
Kotlin 2.4.0: Use IrAnnotationImpl for annotation creation
In 2.4.0, annotation lists are typed as List<IrAnnotation> and IrConstructorCallImpl does not extend IrAnnotation. Replace all IrConstructorCallImpl.fromSymbolOwner() calls that create annotations with a compat wrapper codeQlAnnotationFromSymbolOwner() that uses IrAnnotationImpl.fromSymbolOwner() in 2.4.0 (returning proper IrAnnotation instances) and IrConstructorCallImpl.fromSymbolOwner() in pre-2.4.0 versions. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 9723044 commit b89b6e4

5 files changed

Lines changed: 34 additions & 9 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ open class KotlinFileExtractor(
721721
(it.type as? IrSimpleType)?.classFqName?.asString() != "kotlin.Deprecated"
722722
} +
723723
// Note we lose any arguments to @java.lang.Deprecated that were written in source.
724-
IrConstructorCallImpl.fromSymbolOwner(
724+
codeQlAnnotationFromSymbolOwner(
725725
UNDEFINED_OFFSET,
726726
UNDEFINED_OFFSET,
727727
jldConstructor.returnType,
@@ -2327,7 +2327,7 @@ open class KotlinFileExtractor(
23272327
getClassByFqName(pluginContext, it)?.let { annotationClass ->
23282328
annotationClass.owner.declarations.firstIsInstanceOrNull<IrConstructor>()?.let {
23292329
annotationConstructor ->
2330-
IrConstructorCallImpl.fromSymbolOwner(
2330+
codeQlAnnotationFromSymbolOwner(
23312331
UNDEFINED_OFFSET,
23322332
UNDEFINED_OFFSET,
23332333
annotationConstructor.returnType,

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.codeql
22

3+
import com.github.codeql.utils.versions.codeQlAnnotationFromSymbolOwner
34
import com.github.codeql.utils.versions.codeQlGetValueArgument
45
import com.github.codeql.utils.versions.codeQlPutValueArgument
56
import com.github.codeql.utils.versions.codeQlSetAnnotations
@@ -121,7 +122,7 @@ class MetaAnnotationSupport(
121122
)
122123
return null
123124
} else {
124-
return IrConstructorCallImpl.fromSymbolOwner(
125+
return codeQlAnnotationFromSymbolOwner(
125126
containerClass.defaultType,
126127
containerConstructor.symbol
127128
)
@@ -234,7 +235,7 @@ class MetaAnnotationSupport(
234235
)
235236
}
236237

237-
return IrConstructorCallImpl.fromSymbolOwner(
238+
return codeQlAnnotationFromSymbolOwner(
238239
UNDEFINED_OFFSET,
239240
UNDEFINED_OFFSET,
240241
targetConstructor.returnType,
@@ -287,7 +288,7 @@ class MetaAnnotationSupport(
287288
val targetConstructor =
288289
retentionType.declarations.firstIsInstanceOrNull<IrConstructor>() ?: return null
289290

290-
return IrConstructorCallImpl.fromSymbolOwner(
291+
return codeQlAnnotationFromSymbolOwner(
291292
UNDEFINED_OFFSET,
292293
UNDEFINED_OFFSET,
293294
targetConstructor.returnType,
@@ -419,7 +420,7 @@ class MetaAnnotationSupport(
419420
.map { it.deepCopyWithSymbols(containerClass) } +
420421
listOfNotNull(
421422
repeatableContainerAnnotation?.let {
422-
IrConstructorCallImpl.fromSymbolOwner(
423+
codeQlAnnotationFromSymbolOwner(
423424
UNDEFINED_OFFSET,
424425
UNDEFINED_OFFSET,
425426
it.returnType,
@@ -467,7 +468,7 @@ class MetaAnnotationSupport(
467468
containerClass.symbol,
468469
containerClass.defaultType
469470
)
470-
return IrConstructorCallImpl.fromSymbolOwner(
471+
return codeQlAnnotationFromSymbolOwner(
471472
UNDEFINED_OFFSET,
472473
UNDEFINED_OFFSET,
473474
repeatableConstructor.returnType,
@@ -493,7 +494,7 @@ class MetaAnnotationSupport(
493494
javaAnnotationDocumented?.declarations?.firstIsInstanceOrNull<IrConstructor>()
494495
?: return null
495496

496-
return IrConstructorCallImpl.fromSymbolOwner(
497+
return codeQlAnnotationFromSymbolOwner(
497498
UNDEFINED_OFFSET,
498499
UNDEFINED_OFFSET,
499500
documentedConstructor.returnType,

java/kotlin-extractor/src/main/kotlin/utils/TypeSubstitution.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ object RawTypeAnnotation {
192192
addConstructor { isPrimary = true }
193193
}
194194
val constructor = annoClass.constructors.single()
195-
IrConstructorCallImpl.fromSymbolOwner(constructor.constructedClassType, constructor.symbol)
195+
codeQlAnnotationFromSymbolOwner(constructor.constructedClassType, constructor.symbol)
196196
}
197197
}
198198

java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_8_0/IrCompat.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import org.jetbrains.kotlin.ir.declarations.IrValueParameter
55
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
66
import org.jetbrains.kotlin.ir.expressions.IrExpression
77
import org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression
8+
import org.jetbrains.kotlin.ir.expressions.impl.*
9+
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
810
import org.jetbrains.kotlin.ir.types.IrType
911
import org.jetbrains.kotlin.ir.types.addAnnotations
1012

@@ -58,3 +60,12 @@ fun codeQlSetAnnotations(container: org.jetbrains.kotlin.ir.declarations.IrMutab
5860
fun IrFunction.codeQlSetDispatchReceiverParameter(param: IrValueParameter?) {
5961
dispatchReceiverParameter = param
6062
}
63+
64+
// In pre-2.4.0, annotations are List<IrConstructorCall> so IrConstructorCallImpl works directly.
65+
fun codeQlAnnotationFromSymbolOwner(
66+
startOffset: Int, endOffset: Int, type: IrType, symbol: IrConstructorSymbol, typeArgumentsCount: Int
67+
): IrConstructorCall =
68+
IrConstructorCallImpl.fromSymbolOwner(startOffset, endOffset, type, symbol, typeArgumentsCount)
69+
70+
fun codeQlAnnotationFromSymbolOwner(type: IrType, symbol: IrConstructorSymbol): IrConstructorCall =
71+
IrConstructorCallImpl.fromSymbolOwner(type, symbol)

java/kotlin-extractor/src/main/kotlin/utils/versions/v_2_4_0/IrCompat.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import org.jetbrains.kotlin.ir.expressions.IrAnnotation
88
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
99
import org.jetbrains.kotlin.ir.expressions.IrExpression
1010
import org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression
11+
import org.jetbrains.kotlin.ir.expressions.impl.IrAnnotationImpl
12+
import org.jetbrains.kotlin.ir.expressions.impl.fromSymbolOwner
13+
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
1114
import org.jetbrains.kotlin.ir.types.IrType
1215
import org.jetbrains.kotlin.ir.types.addAnnotations
1316

@@ -93,3 +96,13 @@ fun IrFunction.codeQlSetDispatchReceiverParameter(param: IrValueParameter?) {
9396
parameters = mutableParams
9497
}
9598

99+
// In 2.4.0, annotation lists require IrAnnotation instances.
100+
// Use IrAnnotationImpl.fromSymbolOwner instead of IrConstructorCallImpl.fromSymbolOwner.
101+
fun codeQlAnnotationFromSymbolOwner(
102+
startOffset: Int, endOffset: Int, type: IrType, symbol: IrConstructorSymbol, typeArgumentsCount: Int
103+
): IrConstructorCall =
104+
IrAnnotationImpl.fromSymbolOwner(startOffset, endOffset, type, symbol, typeArgumentsCount)
105+
106+
fun codeQlAnnotationFromSymbolOwner(type: IrType, symbol: IrConstructorSymbol): IrConstructorCall =
107+
IrAnnotationImpl.fromSymbolOwner(type, symbol)
108+

0 commit comments

Comments
 (0)