@@ -964,17 +964,24 @@ open class KotlinUsesExtractor(
964
964
when {
965
965
t.hasAnnotation(jvmWildcardAnnotation) -> true
966
966
! addByDefault -> false
967
- t.hasAnnotation(jvmWildcardSuppressionAnnotation) -> false
968
967
// If a Java declaration specifies a variance, introduce it even if it's pointless (e.g. ? extends FinalClass, or ? super Object)
969
968
javaVariance == v -> true
970
969
v == Variance .IN_VARIANCE -> ! (t.isNullableAny() || t.isAny())
971
970
v == Variance .OUT_VARIANCE -> extendsAdditionAllowed(t)
972
971
else -> false
973
972
}
974
973
974
+ // Returns true if `t` has `@JvmSuppressWildcards` or `@JvmSuppressWildcards(true)`,
975
+ // false if it has `@JvmSuppressWildcards(false)`,
976
+ // and null if the annotation is not present.
977
+ @Suppress(" UNCHECKED_CAST" )
978
+ private fun getWildcardSuppressionDirective (t : IrAnnotationContainer ) =
979
+ t.getAnnotation(jvmWildcardSuppressionAnnotation)?.let { (it.getValueArgument(0 ) as ? IrConst <Boolean >)?.value ? : true }
980
+
975
981
private fun addJavaLoweringArgumentWildcards (p : IrTypeParameter , t : IrTypeArgument , addByDefault : Boolean , javaType : JavaType ? ): IrTypeArgument =
976
982
(t as ? IrTypeProjection )?.let {
977
- val newBase = addJavaLoweringWildcards(it.type, addByDefault, javaType)
983
+ val newAddByDefault = getWildcardSuppressionDirective(it.type)?.not () ? : addByDefault
984
+ val newBase = addJavaLoweringWildcards(it.type, newAddByDefault, javaType)
978
985
// Note javaVariance == null means we don't have a Java type to conform to -- for example if this is a Kotlin source definition.
979
986
val javaVariance = javaType?.let { jType ->
980
987
when (jType) {
@@ -989,7 +996,7 @@ open class KotlinUsesExtractor(
989
996
// For example, Java might declare f(Comparable<CharSequence> cs), in which case we shouldn't add a `? super ...`
990
997
// wildcard. Note if javaType is unknown (e.g. this is a Kotlin source element), we assume wildcards should be added.
991
998
(javaVariance == null || javaVariance == p.variance) &&
992
- wildcardAdditionAllowed(p.variance, it.type, addByDefault , javaVariance))
999
+ wildcardAdditionAllowed(p.variance, it.type, newAddByDefault , javaVariance))
993
1000
p.variance
994
1001
else
995
1002
it.variance
@@ -1008,12 +1015,13 @@ open class KotlinUsesExtractor(
1008
1015
1009
1016
fun addJavaLoweringWildcards (t : IrType , addByDefault : Boolean , javaType : JavaType ? ): IrType =
1010
1017
(t as ? IrSimpleType )?.let {
1018
+ val newAddByDefault = getWildcardSuppressionDirective(t)?.not () ? : addByDefault
1011
1019
val typeParams = it.classOrNull?.owner?.typeParameters ? : return t
1012
1020
val newArgs = typeParams.zip(it.arguments).mapIndexed { idx, pair ->
1013
1021
addJavaLoweringArgumentWildcards(
1014
1022
pair.first,
1015
1023
pair.second,
1016
- addByDefault ,
1024
+ newAddByDefault ,
1017
1025
javaType?.let { jt -> getJavaTypeArgument(jt, idx) }
1018
1026
)
1019
1027
}
@@ -1061,7 +1069,7 @@ open class KotlinUsesExtractor(
1061
1069
classTypeArgsIncludingOuterClasses,
1062
1070
overridesCollectionsMethodWithAlteredParameterTypes(f),
1063
1071
getJavaCallable(f),
1064
- ! hasWildcardSuppressionAnnotation (f)
1072
+ ! getInnermostWildcardSupppressionAnnotation (f)
1065
1073
)
1066
1074
1067
1075
/*
@@ -1220,10 +1228,11 @@ open class KotlinUsesExtractor(
1220
1228
else -> null
1221
1229
}
1222
1230
1223
- fun hasWildcardSuppressionAnnotation (d : IrDeclaration ) =
1224
- d.hasAnnotation(jvmWildcardSuppressionAnnotation) ||
1231
+ fun getInnermostWildcardSupppressionAnnotation (d : IrDeclaration ) =
1232
+ getWildcardSuppressionDirective(d) ? :
1225
1233
// Note not using `parentsWithSelf` as that only works if `d` is an IrDeclarationParent
1226
- d.parents.any { (it as ? IrAnnotationContainer )?.hasAnnotation(jvmWildcardSuppressionAnnotation) == true }
1234
+ d.parents.filterIsInstance<IrAnnotationContainer >().mapNotNull { getWildcardSuppressionDirective(it) }.firstOrNull() ? :
1235
+ false
1227
1236
1228
1237
/* *
1229
1238
* Class to hold labels for generated classes around local functions, lambdas, function references, and property references.
0 commit comments