Skip to content

Commit 658c7a2

Browse files
committed
Don't extract error expressions when a default value is missing
This is a shortcoming of the Kotlin extractor, but at least this way we don't introduce an inconsistency if the annotation is later seen from .kt source or by the Java extractor
1 parent 5b2834c commit 658c7a2

File tree

3 files changed

+61
-35
lines changed

3 files changed

+61
-35
lines changed

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -582,17 +582,9 @@ open class KotlinFileExtractor(
582582
}
583583
// is IrErrorExpression
584584
// null
585-
else -> {
586-
val eId = tw.getFreshIdLabel<DbErrorexpr>()
587-
val type = useType(v?.type ?: pluginContext.irBuiltIns.unitType)
588-
tw.writeExprs_errorexpr(eId, type.javaResult.id, parent, idx)
589-
tw.writeExprsKotlinType(eId, type.kotlinResult.id)
590-
591-
if (v != null) {
592-
tw.writeHasLocation(eId, tw.getLocation(v))
593-
}
594-
eId
595-
}
585+
// Note: emitting an ErrorExpr here would induce an inconsistency if this annotation is later seen from source or by the Java extractor,
586+
// in both of which cases the real value will get extracted.
587+
else -> null
596588
}
597589
}
598590

java/ql/integration-tests/all-platforms/kotlin/annotation-id-consistency/PrintAst.expected

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -105,30 +105,44 @@ test.kt:
105105
# 0| 0: [TypeAccess] VarargAnnotation
106106
# 13| 2: [Method] x
107107
# 13| 3: [TypeAccess] int[]
108-
# 15| 7: [Class] Annotated
108+
# 15| 7: [Interface] AnnWithDefaults
109+
#-----| -3: (Annotations)
110+
# 0| 1: [Annotation] Retention
111+
# 0| 1: [VarAccess] RetentionPolicy.RUNTIME
112+
# 0| -1: [TypeAccess] RetentionPolicy
113+
# 15| 1: [Method] x
114+
# 15| 3: [TypeAccess] int
115+
# 15| 2: [Method] y
116+
# 15| 3: [TypeAccess] String
117+
# 15| 3: [Method] z
118+
# 15| 3: [TypeAccess] DayOfWeek
119+
# 15| 4: [Method] w
120+
# 15| 3: [TypeAccess] Ann3[]
121+
# 15| 0: [TypeAccess] Ann3
122+
# 17| 8: [Class] Annotated
109123
#-----| -3: (Annotations)
110124
# 0| 1: [Annotation] Container
111125
# 0| 1: [ArrayInit] {...}
112-
# 17| 1: [Annotation] VarargAnnotation
126+
# 19| 1: [Annotation] VarargAnnotation
113127
# 0| 1: [ArrayInit] {...}
114-
# 18| 2: [Annotation] VarargAnnotation
128+
# 20| 2: [Annotation] VarargAnnotation
115129
# 0| 1: [ArrayInit] {...}
116130
# 0| 1: [IntegerLiteral] 1
117-
# 19| 3: [Annotation] VarargAnnotation
131+
# 21| 3: [Annotation] VarargAnnotation
118132
# 0| 1: [ArrayInit] {...}
119133
# 0| 1: [IntegerLiteral] 1
120134
# 0| 1: [IntegerLiteral] 2
121-
# 20| 4: [Annotation] VarargAnnotation
135+
# 22| 4: [Annotation] VarargAnnotation
122136
# 0| 1: [ArrayInit] {...}
123137
# 0| 1: [IntegerLiteral] 1
124138
# 0| 1: [IntegerLiteral] 2
125139
# 0| 1: [IntegerLiteral] 3
126-
# 21| 5: [Annotation] VarargAnnotation
140+
# 23| 5: [Annotation] VarargAnnotation
127141
# 0| 1: [ArrayInit] {...}
128142
# 0| 1: [IntegerLiteral] 1
129143
# 0| 1: [IntegerLiteral] 2
130144
# 0| 1: [IntegerLiteral] 3
131-
# 15| 2: [Annotation] Ann1
145+
# 17| 2: [Annotation] Ann1
132146
# 0| 1: [IntegerLiteral] 1
133147
# 0| 1: [Annotation] Ann2
134148
# 0| 1: [StringLiteral] "Hello"
@@ -150,35 +164,43 @@ test.kt:
150164
# 0| 0: [TypeAccess] int
151165
# 0| 1: [VarAccess] DayOfWeek.MONDAY
152166
# 0| -1: [TypeAccess] DayOfWeek
153-
# 16| 3: [Annotation] GenericAnnotation<>
154-
# 22| 1: [Constructor] Annotated
155-
# 15| 5: [BlockStmt] { ... }
156-
# 15| 0: [SuperConstructorInvocationStmt] super(...)
157-
# 22| 1: [BlockStmt] { ... }
158-
# 24| 8: [Class] AnnotatedUsedByKotlin
167+
# 18| 3: [Annotation] GenericAnnotation<>
168+
# 24| 4: [Annotation] AnnWithDefaults
169+
# 0| 1: [IntegerLiteral] 1
170+
# 0| 1: [StringLiteral] "hello"
171+
# 0| 1: [VarAccess] DayOfWeek.TUESDAY
172+
# 0| -1: [TypeAccess] DayOfWeek
173+
# 0| 1: [ArrayInit] {...}
174+
# 0| 1: [Annotation] Ann3
175+
# 0| 1: [IntegerLiteral] 1
176+
# 25| 1: [Constructor] Annotated
177+
# 17| 5: [BlockStmt] { ... }
178+
# 17| 0: [SuperConstructorInvocationStmt] super(...)
179+
# 25| 1: [BlockStmt] { ... }
180+
# 27| 9: [Class] AnnotatedUsedByKotlin
159181
#-----| -3: (Annotations)
160182
# 0| 1: [Annotation] Container
161183
# 0| 1: [ArrayInit] {...}
162-
# 26| 1: [Annotation] VarargAnnotation
184+
# 29| 1: [Annotation] VarargAnnotation
163185
# 0| 1: [ArrayInit] {...}
164-
# 27| 2: [Annotation] VarargAnnotation
186+
# 30| 2: [Annotation] VarargAnnotation
165187
# 0| 1: [ArrayInit] {...}
166188
# 0| 1: [IntegerLiteral] 1
167-
# 28| 3: [Annotation] VarargAnnotation
189+
# 31| 3: [Annotation] VarargAnnotation
168190
# 0| 1: [ArrayInit] {...}
169191
# 0| 1: [IntegerLiteral] 1
170192
# 0| 1: [IntegerLiteral] 2
171-
# 29| 4: [Annotation] VarargAnnotation
193+
# 32| 4: [Annotation] VarargAnnotation
172194
# 0| 1: [ArrayInit] {...}
173195
# 0| 1: [IntegerLiteral] 1
174196
# 0| 1: [IntegerLiteral] 2
175197
# 0| 1: [IntegerLiteral] 3
176-
# 30| 5: [Annotation] VarargAnnotation
198+
# 33| 5: [Annotation] VarargAnnotation
177199
# 0| 1: [ArrayInit] {...}
178200
# 0| 1: [IntegerLiteral] 1
179201
# 0| 1: [IntegerLiteral] 2
180202
# 0| 1: [IntegerLiteral] 3
181-
# 24| 2: [Annotation] Ann1
203+
# 27| 2: [Annotation] Ann1
182204
# 0| 1: [IntegerLiteral] 1
183205
# 0| 1: [Annotation] Ann2
184206
# 0| 1: [StringLiteral] "Hello"
@@ -200,11 +222,19 @@ test.kt:
200222
# 0| 0: [TypeAccess] int
201223
# 0| 1: [VarAccess] DayOfWeek.MONDAY
202224
# 0| -1: [TypeAccess] DayOfWeek
203-
# 25| 3: [Annotation] GenericAnnotation<>
204-
# 31| 1: [Constructor] AnnotatedUsedByKotlin
205-
# 24| 5: [BlockStmt] { ... }
206-
# 24| 0: [SuperConstructorInvocationStmt] super(...)
207-
# 31| 1: [BlockStmt] { ... }
225+
# 28| 3: [Annotation] GenericAnnotation<>
226+
# 34| 4: [Annotation] AnnWithDefaults
227+
# 0| 1: [IntegerLiteral] 1
228+
# 0| 1: [StringLiteral] "hello"
229+
# 0| 1: [VarAccess] DayOfWeek.TUESDAY
230+
# 0| -1: [TypeAccess] DayOfWeek
231+
# 0| 1: [ArrayInit] {...}
232+
# 0| 1: [Annotation] Ann3
233+
# 0| 1: [IntegerLiteral] 1
234+
# 35| 1: [Constructor] AnnotatedUsedByKotlin
235+
# 27| 5: [BlockStmt] { ... }
236+
# 27| 0: [SuperConstructorInvocationStmt] super(...)
237+
# 35| 1: [BlockStmt] { ... }
208238
# 0| [TypeLiteral] String.class
209239
# 0| 0: [TypeAccess] String
210240
# 0| [ArrayInit] {...}

java/ql/integration-tests/all-platforms/kotlin/annotation-id-consistency/test.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ annotation class GenericAnnotation<T : Any>(val x: KClass<T>, val y: Array<KClas
1212
@Repeatable
1313
annotation class VarargAnnotation(vararg val x: Int) { }
1414

15+
annotation class AnnWithDefaults(val x: Int = 1, val y: String = "hello", val z: DayOfWeek = DayOfWeek.TUESDAY, val w: Array<Ann3> = [Ann3(1)]) { }
16+
1517
@Ann1(1, Ann2("Hello", String::class, intArrayOf(1, 2, 3), arrayOf(Ann3(1), Ann3(2)), arrayOf(String::class, Int::class)), DayOfWeek.MONDAY)
1618
@GenericAnnotation<String>(String::class, arrayOf(String::class, String::class))
1719
@VarargAnnotation
1820
@VarargAnnotation(1)
1921
@VarargAnnotation(1, 2)
2022
@VarargAnnotation(*[1, 2, 3])
2123
@VarargAnnotation(*intArrayOf(1, 2, 3))
24+
@AnnWithDefaults
2225
class Annotated { }
2326

2427
@Ann1(1, Ann2("Hello", String::class, intArrayOf(1, 2, 3), arrayOf(Ann3(1), Ann3(2)), arrayOf(String::class, Int::class)), DayOfWeek.MONDAY)
@@ -28,5 +31,6 @@ class Annotated { }
2831
@VarargAnnotation(1, 2)
2932
@VarargAnnotation(*[1, 2, 3])
3033
@VarargAnnotation(*intArrayOf(1, 2, 3))
34+
@AnnWithDefaults
3135
class AnnotatedUsedByKotlin { }
3236

0 commit comments

Comments
 (0)