From a4adfb43d4adebadf5cf368de787d8e266baebeb Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Wed, 7 Dec 2016 17:15:19 +0300 Subject: [PATCH] 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 --- .../synthetic/SamAdapterFunctionsScope.kt | 16 ++-------- .../types/CompositionTypeSubstitution.kt | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+), 13 deletions(-) create mode 100644 core/descriptors/src/org/jetbrains/kotlin/types/CompositionTypeSubstitution.kt diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt index 825f745361d..8ff0a9775d0 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt @@ -100,7 +100,7 @@ class SamAdapterFunctionsScope( override var baseDescriptorForSynthetic: FunctionDescriptor by Delegates.notNull() private set - private var toSourceFunctionTypeParameters: Map? = null + private lateinit var fromSourceFunctionTypeParameters: Map companion object { fun create(sourceFunction: FunctionDescriptor): MyFunctionDescriptor { @@ -125,7 +125,7 @@ class SamAdapterFunctionsScope( val typeParameters = ArrayList(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() - 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 } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/CompositionTypeSubstitution.kt b/core/descriptors/src/org/jetbrains/kotlin/types/CompositionTypeSubstitution.kt new file mode 100644 index 00000000000..42d1e044c9e --- /dev/null +++ b/core/descriptors/src/org/jetbrains/kotlin/types/CompositionTypeSubstitution.kt @@ -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 +) : DelegatedTypeSubstitution(outer) { + + override fun get(key: KotlinType) = inner[key.constructor.declarationDescriptor]?.let { outer[it.defaultType] } ?: outer[key] +}