Skip to content

Commit 70e5cf7

Browse files
authored
Merge pull request #9547 from smowton/smowton/fix/constructor-wildcard-arguments
Kotlin: Fix wildcard introduction vs. constructor parameters
2 parents fceea04 + a9f4388 commit 70e5cf7

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import org.jetbrains.kotlin.ir.types.*
2121
import org.jetbrains.kotlin.ir.util.*
2222
import org.jetbrains.kotlin.load.java.sources.JavaSourceElement
2323
import org.jetbrains.kotlin.load.java.structure.JavaClass
24+
import org.jetbrains.kotlin.load.java.structure.JavaMethod
2425
import org.jetbrains.kotlin.load.java.structure.JavaTypeParameter
26+
import org.jetbrains.kotlin.load.java.structure.JavaTypeParameterListOwner
2527
import org.jetbrains.kotlin.name.FqName
2628
import org.jetbrains.kotlin.types.Variance
2729
import org.jetbrains.kotlin.util.OperatorNameConventions
@@ -514,7 +516,7 @@ open class KotlinFileExtractor(
514516
else
515517
null
516518
} ?: vp.type
517-
val javaType = ((vp.parent as? IrFunction)?.let { getJavaMethod(it) })?.valueParameters?.getOrNull(idx)?.type
519+
val javaType = (vp.parent as? IrFunction)?.let { getJavaCallable(it)?.let { jCallable -> getJavaValueParameterType(jCallable, idx) } }
518520
val typeWithWildcards = addJavaLoweringWildcards(maybeErasedType, !hasWildcardSuppressionAnnotation(vp), javaType)
519521
val substitutedType = typeSubstitution?.let { it(typeWithWildcards, TypeContext.OTHER, pluginContext) } ?: typeWithWildcards
520522
val id = useValueParameter(vp, parent)
@@ -691,8 +693,8 @@ open class KotlinFileExtractor(
691693
with("function", f) {
692694
DeclarationStackAdjuster(f).use {
693695

694-
val javaMethod = getJavaMethod(f)
695-
getFunctionTypeParameters(f).mapIndexed { idx, tp -> extractTypeParameter(tp, idx, javaMethod?.typeParameters?.getOrNull(idx)) }
696+
val javaCallable = getJavaCallable(f)
697+
getFunctionTypeParameters(f).mapIndexed { idx, tp -> extractTypeParameter(tp, idx, (javaCallable as? JavaTypeParameterListOwner)?.typeParameters?.getOrNull(idx)) }
696698

697699
val id =
698700
idOverride
@@ -726,7 +728,7 @@ open class KotlinFileExtractor(
726728

727729
val paramsSignature = allParamTypes.joinToString(separator = ",", prefix = "(", postfix = ")") { it.javaResult.signature!! }
728730

729-
val adjustedReturnType = addJavaLoweringWildcards(getAdjustedReturnType(f), false, javaMethod?.returnType)
731+
val adjustedReturnType = addJavaLoweringWildcards(getAdjustedReturnType(f), false, (javaCallable as? JavaMethod)?.returnType)
730732
val substReturnType = typeSubstitution?.let { it(adjustedReturnType, TypeContext.RETURN, pluginContext) } ?: adjustedReturnType
731733

732734
val locId = locOverride ?: getLocation(f, classTypeArgsIncludingOuterClasses)

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import org.jetbrains.kotlin.backend.common.ir.allOverridden
99
import org.jetbrains.kotlin.backend.common.ir.isFinalClass
1010
import org.jetbrains.kotlin.backend.common.lower.parents
1111
import org.jetbrains.kotlin.backend.common.lower.parentsWithSelf
12-
import org.jetbrains.kotlin.backend.jvm.ir.getJvmNameFromAnnotation
1312
import org.jetbrains.kotlin.backend.jvm.ir.propertyIfAccessor
1413
import org.jetbrains.kotlin.builtins.StandardNames
1514
import org.jetbrains.kotlin.descriptors.*
@@ -996,7 +995,7 @@ open class KotlinUsesExtractor(
996995
getFunctionTypeParameters(f),
997996
classTypeArgsIncludingOuterClasses,
998997
overridesCollectionsMethodWithAlteredParameterTypes(f),
999-
getJavaMethod(f),
998+
getJavaCallable(f),
1000999
!hasWildcardSuppressionAnnotation(f)
10011000
)
10021001

@@ -1028,7 +1027,7 @@ open class KotlinUsesExtractor(
10281027
// parameter erasure to match the way this class will appear to an external consumer of the .class file.
10291028
overridesCollectionsMethod: Boolean,
10301029
// The Java signature of this callable, if known.
1031-
javaSignature: JavaMethod?,
1030+
javaSignature: JavaMember?,
10321031
// If true, Java wildcards implied by Kotlin type parameter variance should be added by default to this function's value parameters' types.
10331032
// (Return-type wildcard addition is always off by default)
10341033
addParameterWildcardsByDefault: Boolean,
@@ -1056,7 +1055,7 @@ open class KotlinUsesExtractor(
10561055
// If this has happened, erase the type again to get the correct Java signature.
10571056
val maybeAmendedForCollections = if (overridesCollectionsMethod) eraseCollectionsMethodParameterType(it.value.type, name, it.index) else it.value.type
10581057
// Add any wildcard types that the Kotlin compiler would add in the Java lowering of this function:
1059-
val withAddedWildcards = addJavaLoweringWildcards(maybeAmendedForCollections, addParameterWildcardsByDefault, javaSignature?.let { sig -> sig.valueParameters[it.index].type })
1058+
val withAddedWildcards = addJavaLoweringWildcards(maybeAmendedForCollections, addParameterWildcardsByDefault, javaSignature?.let { sig -> getJavaValueParameterType(sig, it.index) })
10601059
// Now substitute any class type parameters in:
10611060
val maybeSubbed = withAddedWildcards.substituteTypeAndArguments(substitutionMap, TypeContext.OTHER, pluginContext)
10621061
// Finally, mimic the Java extractor's behaviour by naming functions with type parameters for their erased types;
@@ -1103,7 +1102,13 @@ open class KotlinUsesExtractor(
11031102
}
11041103

11051104
@OptIn(ObsoleteDescriptorBasedAPI::class)
1106-
fun getJavaMethod(f: IrFunction) = (f.descriptor.source as? JavaSourceElement)?.javaElement as? JavaMethod
1105+
fun getJavaCallable(f: IrFunction) = (f.descriptor.source as? JavaSourceElement)?.javaElement as? JavaMember
1106+
1107+
fun getJavaValueParameterType(m: JavaMember, idx: Int) = when(m) {
1108+
is JavaMethod -> m.valueParameters[idx].type
1109+
is JavaConstructor -> m.valueParameters[idx].type
1110+
else -> null
1111+
}
11071112

11081113
fun hasWildcardSuppressionAnnotation(d: IrDeclaration) =
11091114
d.hasAnnotation(jvmWildcardSuppressionAnnotaton) ||

0 commit comments

Comments
 (0)