Write additional type parameters for DefaultImpls methods, fix for KT-11121: BadClassFile exception for interface implemented generic properties
#KT-11121 Fixed
This commit is contained in:
@@ -1059,10 +1059,24 @@ public class KotlinTypeMapper {
|
||||
writeVoidReturn(sw);
|
||||
}
|
||||
else {
|
||||
writeFormalTypeParameters(getDirectMember(f).getTypeParameters(), sw);
|
||||
CallableMemberDescriptor directMember = getDirectMember(f);
|
||||
KotlinType thisIfNeeded = null;
|
||||
if (OwnerKind.DEFAULT_IMPLS == kind) {
|
||||
ReceiverTypeAndTypeParameters receiverTypeAndTypeParameters = TypeMapperUtilsKt.patchTypeParametersForDefaultImplMethod(directMember);
|
||||
writeFormalTypeParameters(CollectionsKt.plus(receiverTypeAndTypeParameters.getTypeParameters(), directMember.getTypeParameters()), sw);
|
||||
thisIfNeeded = receiverTypeAndTypeParameters.getReceiverType();
|
||||
}
|
||||
else {
|
||||
writeFormalTypeParameters(directMember.getTypeParameters(), sw);
|
||||
if (isAccessor(f) && f.getDispatchReceiverParameter() != null) {
|
||||
thisIfNeeded = ((ClassDescriptor) f.getContainingDeclaration()).getDefaultType();
|
||||
}
|
||||
}
|
||||
|
||||
sw.writeParametersStart();
|
||||
writeThisIfNeeded(f, kind, sw);
|
||||
if (thisIfNeeded != null) {
|
||||
writeParameter(sw, JvmMethodParameterKind.THIS, thisIfNeeded, f);
|
||||
}
|
||||
|
||||
ReceiverParameterDescriptor receiverParameter = f.getExtensionReceiverParameter();
|
||||
if (receiverParameter != null) {
|
||||
@@ -1216,33 +1230,6 @@ public class KotlinTypeMapper {
|
||||
return sw.makeJavaGenericSignature();
|
||||
}
|
||||
|
||||
private void writeThisIfNeeded(
|
||||
@NotNull CallableMemberDescriptor descriptor,
|
||||
@NotNull OwnerKind kind,
|
||||
@NotNull JvmSignatureWriter sw
|
||||
) {
|
||||
ClassDescriptor thisType;
|
||||
if (kind == OwnerKind.DEFAULT_IMPLS) {
|
||||
thisType = getTraitImplThisParameterClass((ClassDescriptor) descriptor.getContainingDeclaration());
|
||||
}
|
||||
else if (isAccessor(descriptor) && descriptor.getDispatchReceiverParameter() != null) {
|
||||
thisType = (ClassDescriptor) descriptor.getContainingDeclaration();
|
||||
}
|
||||
else return;
|
||||
|
||||
writeParameter(sw, JvmMethodParameterKind.THIS, thisType.getDefaultType(), descriptor);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static ClassDescriptor getTraitImplThisParameterClass(@NotNull ClassDescriptor traitDescriptor) {
|
||||
for (ClassDescriptor descriptor : DescriptorUtils.getSuperclassDescriptors(traitDescriptor)) {
|
||||
if (descriptor.getKind() != ClassKind.INTERFACE) {
|
||||
return descriptor;
|
||||
}
|
||||
}
|
||||
return traitDescriptor;
|
||||
}
|
||||
|
||||
public void writeFormalTypeParameters(@NotNull List<TypeParameterDescriptor> typeParameters, @NotNull JvmSignatureWriter sw) {
|
||||
if (sw.skipGenericSignature()) return;
|
||||
for (TypeParameterDescriptor typeParameter : typeParameters) {
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.codegen.state
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.typeUtil.asTypeProjection
|
||||
|
||||
class ReceiverTypeAndTypeParameters(val receiverType: KotlinType, val typeParameters: List<TypeParameterDescriptor>)
|
||||
|
||||
fun patchTypeParametersForDefaultImplMethod(function: CallableMemberDescriptor): ReceiverTypeAndTypeParameters {
|
||||
val classDescriptor = function.containingDeclaration as ClassDescriptor
|
||||
val functionTypeParameterNames = function.typeParameters.map { it.name.asString() }
|
||||
val interfaceTypeParameters = classDescriptor.declaredTypeParameters
|
||||
val conflictedTypeParameters = interfaceTypeParameters.filter { it.name.asString() in functionTypeParameterNames }
|
||||
|
||||
if (conflictedTypeParameters.isEmpty())
|
||||
return ReceiverTypeAndTypeParameters(classDescriptor.defaultType, interfaceTypeParameters)
|
||||
|
||||
val existingNames = (functionTypeParameterNames + interfaceTypeParameters.map { it.name.asString() }).toMutableSet()
|
||||
|
||||
val mappingForInterfaceTypeParameters = conflictedTypeParameters.associateBy ({ it }) {
|
||||
typeParameter ->
|
||||
|
||||
val newNamePrefix = typeParameter.name.asString() + "_I"
|
||||
val newName = newNamePrefix + generateSequence(1) { x -> x + 1 }.first {
|
||||
index -> (newNamePrefix + index) !in existingNames
|
||||
}
|
||||
|
||||
existingNames.add(newName)
|
||||
function.createTypeParameterWithNewName(typeParameter, newName)
|
||||
}
|
||||
|
||||
val substitution = TypeConstructorSubstitution.createByParametersMap(mappingForInterfaceTypeParameters.mapValues {
|
||||
it.value.defaultType.asTypeProjection()
|
||||
})
|
||||
|
||||
val substitutor = TypeSubstitutor.create(substitution)
|
||||
|
||||
val additionalTypeParameters = interfaceTypeParameters.map { typeParameter ->
|
||||
mappingForInterfaceTypeParameters[typeParameter] ?: typeParameter
|
||||
}
|
||||
var resultTypeParameters = mutableListOf<TypeParameterDescriptor>()
|
||||
DescriptorSubstitutor.substituteTypeParameters(additionalTypeParameters, substitution, classDescriptor, resultTypeParameters)
|
||||
|
||||
return ReceiverTypeAndTypeParameters(substitutor.substitute(classDescriptor.defaultType, Variance.INVARIANT)!!, resultTypeParameters)
|
||||
}
|
||||
|
||||
fun CallableMemberDescriptor.createTypeParameterWithNewName(descriptor: TypeParameterDescriptor, newName: String): TypeParameterDescriptorImpl {
|
||||
val newDescriptor = TypeParameterDescriptorImpl.createForFurtherModification(
|
||||
this,
|
||||
descriptor.annotations,
|
||||
descriptor.isReified,
|
||||
descriptor.variance,
|
||||
Name.identifier(newName),
|
||||
descriptor.index,
|
||||
descriptor.source)
|
||||
descriptor.upperBounds.forEach {
|
||||
newDescriptor.addUpperBound(it)
|
||||
}
|
||||
newDescriptor.setInitialized()
|
||||
return newDescriptor
|
||||
}
|
||||
Reference in New Issue
Block a user