@@ -964,17 +964,24 @@ open class KotlinUsesExtractor(
964964 when {
965965 t.hasAnnotation(jvmWildcardAnnotation) -> true
966966 ! addByDefault -> false
967- t.hasAnnotation(jvmWildcardSuppressionAnnotation) -> false
968967 // If a Java declaration specifies a variance, introduce it even if it's pointless (e.g. ? extends FinalClass, or ? super Object)
969968 javaVariance == v -> true
970969 v == Variance .IN_VARIANCE -> ! (t.isNullableAny() || t.isAny())
971970 v == Variance .OUT_VARIANCE -> extendsAdditionAllowed(t)
972971 else -> false
973972 }
974973
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+
975981 private fun addJavaLoweringArgumentWildcards (p : IrTypeParameter , t : IrTypeArgument , addByDefault : Boolean , javaType : JavaType ? ): IrTypeArgument =
976982 (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)
978985 // Note javaVariance == null means we don't have a Java type to conform to -- for example if this is a Kotlin source definition.
979986 val javaVariance = javaType?.let { jType ->
980987 when (jType) {
@@ -989,7 +996,7 @@ open class KotlinUsesExtractor(
989996 // For example, Java might declare f(Comparable<CharSequence> cs), in which case we shouldn't add a `? super ...`
990997 // wildcard. Note if javaType is unknown (e.g. this is a Kotlin source element), we assume wildcards should be added.
991998 (javaVariance == null || javaVariance == p.variance) &&
992- wildcardAdditionAllowed(p.variance, it.type, addByDefault , javaVariance))
999+ wildcardAdditionAllowed(p.variance, it.type, newAddByDefault , javaVariance))
9931000 p.variance
9941001 else
9951002 it.variance
@@ -1008,12 +1015,13 @@ open class KotlinUsesExtractor(
10081015
10091016 fun addJavaLoweringWildcards (t : IrType , addByDefault : Boolean , javaType : JavaType ? ): IrType =
10101017 (t as ? IrSimpleType )?.let {
1018+ val newAddByDefault = getWildcardSuppressionDirective(t)?.not () ? : addByDefault
10111019 val typeParams = it.classOrNull?.owner?.typeParameters ? : return t
10121020 val newArgs = typeParams.zip(it.arguments).mapIndexed { idx, pair ->
10131021 addJavaLoweringArgumentWildcards(
10141022 pair.first,
10151023 pair.second,
1016- addByDefault ,
1024+ newAddByDefault ,
10171025 javaType?.let { jt -> getJavaTypeArgument(jt, idx) }
10181026 )
10191027 }
@@ -1061,7 +1069,7 @@ open class KotlinUsesExtractor(
10611069 classTypeArgsIncludingOuterClasses,
10621070 overridesCollectionsMethodWithAlteredParameterTypes(f),
10631071 getJavaCallable(f),
1064- ! hasWildcardSuppressionAnnotation (f)
1072+ ! getInnermostWildcardSupppressionAnnotation (f)
10651073 )
10661074
10671075 /*
@@ -1220,10 +1228,11 @@ open class KotlinUsesExtractor(
12201228 else -> null
12211229 }
12221230
1223- fun hasWildcardSuppressionAnnotation (d : IrDeclaration ) =
1224- d.hasAnnotation(jvmWildcardSuppressionAnnotation) ||
1231+ fun getInnermostWildcardSupppressionAnnotation (d : IrDeclaration ) =
1232+ getWildcardSuppressionDirective(d) ? :
12251233 // 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
12271236
12281237 /* *
12291238 * Class to hold labels for generated classes around local functions, lambdas, function references, and property references.
0 commit comments