Use doSubstitute when enhacing types of Java method
Before this commit old type parameters were inserted into new descriptor,
and that broke a simple contract: desc.child.getContainingDeclaration() == desc.
We use `doSubstitute` here because it does exactly what we need:
1. creates full copy of descriptor
2. copies method's type parameters (with new containing declaration) and properly substitute to them in value parameters, return type and etc.
But we had to customize `doSubstitute`: add some parameters like `newReturnType`
NOTE: Strange testData change.
(Mutable)List<in T!>! after substitution becomes MutableList<in T!>..List<*>?.
But it's not wrong because List<in T> behaves exactly as List<*>, and the same happens when substituing Java class scope:
public class A<E> {
<T> void foo(List<? super T> x) {}
}
Kotlin:
A.foo(), type of first value parameter --- (Mutable)List<in T!>
A<String>().foo(), type of first value parameter --- MutableList<in T!>..List<*>?
This commit is contained in:
+1
@@ -101,6 +101,7 @@ public class JavaConstructorDescriptor extends ConstructorDescriptorImpl impleme
|
||||
@NotNull JetType enhancedReturnType
|
||||
) {
|
||||
JavaConstructorDescriptor enhanced = createSubstitutedCopy(getContainingDeclaration(), getOriginal(), getKind());
|
||||
// We do not use doSubstitute here as in JavaMethodDescriptor.enhance because type parameters of constructor belongs to class
|
||||
enhanced.initialize(
|
||||
enhancedReceiverType,
|
||||
getDispatchReceiverParameter(),
|
||||
|
||||
+14
-9
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations;
|
||||
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl;
|
||||
import org.jetbrains.kotlin.name.Name;
|
||||
import org.jetbrains.kotlin.types.JetType;
|
||||
import org.jetbrains.kotlin.types.TypeSubstitutor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -100,16 +101,20 @@ public class JavaMethodDescriptor extends SimpleFunctionDescriptorImpl implement
|
||||
@NotNull List<JetType> enhancedValueParametersTypes,
|
||||
@NotNull JetType enhancedReturnType
|
||||
) {
|
||||
JavaMethodDescriptor enhancedMethod = createSubstitutedCopy(getContainingDeclaration(), getOriginal(), getKind());
|
||||
enhancedMethod.initialize(
|
||||
enhancedReceiverType,
|
||||
getDispatchReceiverParameter(),
|
||||
getTypeParameters(),
|
||||
DescriptorsPackage.createEnhancedValueParameters(enhancedValueParametersTypes, getValueParameters(), enhancedMethod),
|
||||
enhancedReturnType,
|
||||
getModality(),
|
||||
getVisibility()
|
||||
List<ValueParameterDescriptor> enhancedValueParameters =
|
||||
DescriptorsPackage.createEnhancedValueParameters(enhancedValueParametersTypes, getValueParameters(), this);
|
||||
|
||||
// We use `doSubstitute` here because it does exactly what we need:
|
||||
// 1. creates full copy of descriptor
|
||||
// 2. copies method's type parameters (with new containing declaration) and properly substitute to them in value parameters, return type and etc.
|
||||
JavaMethodDescriptor enhancedMethod = (JavaMethodDescriptor) doSubstitute(
|
||||
TypeSubstitutor.EMPTY, getContainingDeclaration(), getModality(), getVisibility(), getOriginal(),
|
||||
/* copyOverrides = */ false, getKind(),
|
||||
enhancedValueParameters, enhancedReceiverType, enhancedReturnType
|
||||
);
|
||||
|
||||
assert enhancedMethod != null : "null after substitution while enhancing " + toString();
|
||||
|
||||
for (FunctionDescriptor overridden : getOverriddenDescriptors()) {
|
||||
enhancedMethod.addOverriddenDescriptor(overridden);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user