[Misc] Move TypeAliasExpander to core

This commit is contained in:
Denis Zharkov
2019-05-06 17:29:07 +03:00
committed by Dmitry Savvinov
parent 7ba3f7e599
commit c1144f35f1
5 changed files with 41 additions and 67 deletions
@@ -1352,21 +1352,6 @@ public class DescriptorResolver {
} }
} }
public static void checkBoundsInTypeAlias(
@NotNull TypeAliasExpansionReportStrategy reportStrategy,
@NotNull KotlinType unsubstitutedArgument,
@NotNull KotlinType typeArgument,
@NotNull TypeParameterDescriptor typeParameterDescriptor,
@NotNull TypeSubstitutor substitutor
) {
for (KotlinType bound : typeParameterDescriptor.getUpperBounds()) {
KotlinType substitutedBound = substitutor.safeSubstitute(bound, Variance.INVARIANT);
if (!KotlinTypeChecker.DEFAULT.isSubtypeOf(typeArgument, substitutedBound)) {
reportStrategy.boundsViolationInSubstitution(substitutedBound, unsubstitutedArgument, typeArgument, typeParameterDescriptor);
}
}
}
public static boolean checkHasOuterClassInstance( public static boolean checkHasOuterClassInstance(
@NotNull LexicalScope scope, @NotNull LexicalScope scope,
@NotNull BindingTrace trace, @NotNull BindingTrace trace,
@@ -693,7 +693,7 @@ class CandidateResolver(
val typeParameter = typeParameters[i] val typeParameter = typeParameters[i]
val substitutedTypeArgument = substitutedTypeProjection.type val substitutedTypeArgument = substitutedTypeProjection.type
val unsubstitutedTypeArgument = unsubstitutedType.arguments[i].type val unsubstitutedTypeArgument = unsubstitutedType.arguments[i].type
DescriptorResolver.checkBoundsInTypeAlias( TypeAliasExpander.checkBoundsInTypeAlias(
reportStrategy, reportStrategy,
unsubstitutedTypeArgument, unsubstitutedTypeArgument,
substitutedTypeArgument, substitutedTypeArgument,
@@ -1,27 +1,16 @@
/* /*
* Copyright 2010-2016 JetBrains s.r.o. * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
* 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.resolve package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.annotations.composeAnnotations import org.jetbrains.kotlin.descriptors.annotations.composeAnnotations
import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.typeUtil.containsTypeAliasParameters import org.jetbrains.kotlin.types.typeUtil.containsTypeAliasParameters
import org.jetbrains.kotlin.types.typeUtil.requiresTypeAliasExpansion import org.jetbrains.kotlin.types.typeUtil.requiresTypeAliasExpansion
@@ -49,7 +38,10 @@ class TypeAliasExpander(
recursionDepth: Int, recursionDepth: Int,
withAbbreviatedType: Boolean withAbbreviatedType: Boolean
): SimpleType { ): SimpleType {
val underlyingProjection = TypeProjectionImpl(Variance.INVARIANT, typeAliasExpansion.descriptor.underlyingType) val underlyingProjection = TypeProjectionImpl(
Variance.INVARIANT,
typeAliasExpansion.descriptor.underlyingType
)
val expandedProjection = expandTypeProjection(underlyingProjection, typeAliasExpansion, null, recursionDepth) val expandedProjection = expandTypeProjection(underlyingProjection, typeAliasExpansion, null, recursionDepth)
val expandedType = expandedProjection.type.asSimpleType() val expandedType = expandedProjection.type.asSimpleType()
@@ -205,7 +197,8 @@ class TypeAliasExpander(
expandTypeProjection(typeAliasArgument, typeAliasExpansion, typeConstructor.parameters[i], recursionDepth + 1) expandTypeProjection(typeAliasArgument, typeAliasExpansion, typeConstructor.parameters[i], recursionDepth + 1)
} }
val nestedExpansion = TypeAliasExpansion.create(typeAliasExpansion, typeDescriptor, expandedArguments) val nestedExpansion =
TypeAliasExpansion.create(typeAliasExpansion, typeDescriptor, expandedArguments)
val nestedExpandedType = expandRecursively( val nestedExpandedType = expandRecursively(
nestedExpansion, type.annotations, nestedExpansion, type.annotations,
@@ -257,7 +250,7 @@ class TypeAliasExpander(
val unsubstitutedArgument = unsubstitutedType.arguments[i] val unsubstitutedArgument = unsubstitutedType.arguments[i]
val typeParameter = unsubstitutedType.constructor.parameters[i] val typeParameter = unsubstitutedType.constructor.parameters[i]
if (shouldCheckBounds) { if (shouldCheckBounds) {
DescriptorResolver.checkBoundsInTypeAlias( checkBoundsInTypeAlias(
reportStrategy, reportStrategy,
unsubstitutedArgument.type, unsubstitutedArgument.type,
substitutedArgument.type, substitutedArgument.type,
@@ -272,12 +265,33 @@ class TypeAliasExpander(
companion object { companion object {
private const val MAX_RECURSION_DEPTH = 100 private const val MAX_RECURSION_DEPTH = 100
fun checkBoundsInTypeAlias(
reportStrategy: TypeAliasExpansionReportStrategy,
unsubstitutedArgument: KotlinType,
typeArgument: KotlinType,
typeParameterDescriptor: TypeParameterDescriptor,
substitutor: TypeSubstitutor
) {
for (bound in typeParameterDescriptor.upperBounds) {
val substitutedBound = substitutor.safeSubstitute(bound, Variance.INVARIANT)
if (!KotlinTypeChecker.DEFAULT.isSubtypeOf(typeArgument, substitutedBound)) {
reportStrategy.boundsViolationInSubstitution(
substitutedBound,
unsubstitutedArgument,
typeArgument,
typeParameterDescriptor
)
}
}
}
private fun assertRecursionDepth(recursionDepth: Int, typeAliasDescriptor: TypeAliasDescriptor) { private fun assertRecursionDepth(recursionDepth: Int, typeAliasDescriptor: TypeAliasDescriptor) {
if (recursionDepth > MAX_RECURSION_DEPTH) { if (recursionDepth > MAX_RECURSION_DEPTH) {
throw AssertionError("Too deep recursion while expanding type alias ${typeAliasDescriptor.name}") throw AssertionError("Too deep recursion while expanding type alias ${typeAliasDescriptor.name}")
} }
} }
val NON_REPORTING = TypeAliasExpander(TypeAliasExpansionReportStrategy.DO_NOTHING, false) val NON_REPORTING =
TypeAliasExpander(TypeAliasExpansionReportStrategy.DO_NOTHING, false)
} }
} }
@@ -1,25 +1,12 @@
/* /*
* Copyright 2010-2016 JetBrains s.r.o. * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
* 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.resolve package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.types.TypeConstructor
import org.jetbrains.kotlin.types.TypeProjection
class TypeAliasExpansion private constructor( class TypeAliasExpansion private constructor(
val parent: TypeAliasExpansion?, val parent: TypeAliasExpansion?,
@@ -1,25 +1,13 @@
/* /*
* Copyright 2010-2016 JetBrains s.r.o. * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
* 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.resolve package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.types.KotlinType
interface TypeAliasExpansionReportStrategy { interface TypeAliasExpansionReportStrategy {
fun wrongNumberOfTypeArguments(typeAlias: TypeAliasDescriptor, numberOfParameters: Int) fun wrongNumberOfTypeArguments(typeAlias: TypeAliasDescriptor, numberOfParameters: Int)
@@ -54,4 +42,4 @@ interface TypeAliasExpansionReportStrategy {
override fun repeatedAnnotation(annotation: AnnotationDescriptor) {} override fun repeatedAnnotation(annotation: AnnotationDescriptor) {}
} }
} }