Minor. Refine substitution for SAM adapters
Introduce CompositionTypeSubstitution instead of manual composition of two substitutions. This change is necessary for the later changes where original substitution will be able to contain outer type parameters #KT-11128 In Progress
This commit is contained in:
+3
-13
@@ -100,7 +100,7 @@ class SamAdapterFunctionsScope(
|
||||
override var baseDescriptorForSynthetic: FunctionDescriptor by Delegates.notNull()
|
||||
private set
|
||||
|
||||
private var toSourceFunctionTypeParameters: Map<TypeParameterDescriptor, TypeParameterDescriptor>? = null
|
||||
private lateinit var fromSourceFunctionTypeParameters: Map<TypeParameterDescriptor, TypeParameterDescriptor>
|
||||
|
||||
companion object {
|
||||
fun create(sourceFunction: FunctionDescriptor): MyFunctionDescriptor {
|
||||
@@ -125,7 +125,7 @@ class SamAdapterFunctionsScope(
|
||||
val typeParameters = ArrayList<TypeParameterDescriptor>(sourceTypeParams.size)
|
||||
val typeSubstitutor = DescriptorSubstitutor.substituteTypeParameters(sourceTypeParams, TypeSubstitution.EMPTY, descriptor, typeParameters)
|
||||
|
||||
descriptor.toSourceFunctionTypeParameters = typeParameters.zip(sourceTypeParams).toMap()
|
||||
descriptor.fromSourceFunctionTypeParameters = sourceTypeParams.zip(typeParameters).toMap()
|
||||
|
||||
val returnType = typeSubstitutor.safeSubstitute(sourceFunction.returnType!!, Variance.INVARIANT)
|
||||
val receiverType = typeSubstitutor.safeSubstitute(ownerClass.defaultType, Variance.INVARIANT)
|
||||
@@ -169,20 +169,10 @@ class SamAdapterFunctionsScope(
|
||||
original as MyFunctionDescriptor
|
||||
assert(original.original == original) { "original in doSubstitute should have no other original" }
|
||||
|
||||
val substitutionMap = HashMap<TypeConstructor, TypeProjection>()
|
||||
for (typeParameter in original.typeParameters) {
|
||||
val typeProjection = configuration.substitution[typeParameter.defaultType] ?: continue
|
||||
val sourceTypeParameter = original.toSourceFunctionTypeParameters!![typeParameter]!!
|
||||
substitutionMap[sourceTypeParameter.typeConstructor] = typeProjection
|
||||
|
||||
}
|
||||
|
||||
val sourceFunctionSubstitutor =
|
||||
TypeConstructorSubstitution.createByConstructorsMap(
|
||||
substitutionMap, configuration.substitution.approximateCapturedTypes()).buildSubstitutor()
|
||||
CompositionTypeSubstitution(configuration.substitution, fromSourceFunctionTypeParameters).buildSubstitutor()
|
||||
|
||||
descriptor.baseDescriptorForSynthetic = original.baseDescriptorForSynthetic.substitute(sourceFunctionSubstitutor) ?: return null
|
||||
|
||||
return descriptor
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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.types
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
||||
|
||||
// Composition works as it's expected: Composition(outer, inner)(type) = outer(inner(type))
|
||||
// Note that when a substitution returns null it means that type shouldn't be changed (i.e. it works like the identity function in that case)
|
||||
// Currently it's only works if the inner substitution is a mapping from type parameter to another type parameter.
|
||||
// Otherwise composition semantics become a little bit complicated.
|
||||
class CompositionTypeSubstitution(
|
||||
private val outer: TypeSubstitution, private val inner: Map<TypeParameterDescriptor, TypeParameterDescriptor>
|
||||
) : DelegatedTypeSubstitution(outer) {
|
||||
|
||||
override fun get(key: KotlinType) = inner[key.constructor.declarationDescriptor]?.let { outer[it.defaultType] } ?: outer[key]
|
||||
}
|
||||
Reference in New Issue
Block a user