From c1144f35f1d11bd37a7e7f8971fdd0d31766a305 Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Mon, 6 May 2019 17:29:07 +0300 Subject: [PATCH] [Misc] Move TypeAliasExpander to core --- .../kotlin/resolve/DescriptorResolver.java | 15 ------ .../kotlin/resolve/calls/CandidateResolver.kt | 2 +- .../kotlin/types}/TypeAliasExpander.kt | 52 ++++++++++++------- .../kotlin/types}/TypeAliasExpansion.kt | 19 ++----- .../TypeAliasExpansionReportStrategy.kt | 20 ++----- 5 files changed, 41 insertions(+), 67 deletions(-) rename {compiler/frontend/src/org/jetbrains/kotlin/resolve => core/descriptors/src/org/jetbrains/kotlin/types}/TypeAliasExpander.kt (87%) rename {compiler/frontend/src/org/jetbrains/kotlin/resolve => core/descriptors/src/org/jetbrains/kotlin/types}/TypeAliasExpansion.kt (65%) rename {compiler/frontend/src/org/jetbrains/kotlin/resolve => core/descriptors/src/org/jetbrains/kotlin/types}/TypeAliasExpansionReportStrategy.kt (69%) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java index 1ceb21ad25c..7e04011764b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java @@ -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( @NotNull LexicalScope scope, @NotNull BindingTrace trace, diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CandidateResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CandidateResolver.kt index b1a57958b15..8995bd7e99d 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CandidateResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CandidateResolver.kt @@ -693,7 +693,7 @@ class CandidateResolver( val typeParameter = typeParameters[i] val substitutedTypeArgument = substitutedTypeProjection.type val unsubstitutedTypeArgument = unsubstitutedType.arguments[i].type - DescriptorResolver.checkBoundsInTypeAlias( + TypeAliasExpander.checkBoundsInTypeAlias( reportStrategy, unsubstitutedTypeArgument, substitutedTypeArgument, diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeAliasExpander.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeAliasExpander.kt similarity index 87% rename from compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeAliasExpander.kt rename to core/descriptors/src/org/jetbrains/kotlin/types/TypeAliasExpander.kt index dad93fb3fe7..436d736e516 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeAliasExpander.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeAliasExpander.kt @@ -1,27 +1,16 @@ /* - * 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. + * 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. */ -package org.jetbrains.kotlin.resolve +package org.jetbrains.kotlin.types import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.annotations.composeAnnotations 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.requiresTypeAliasExpansion @@ -49,7 +38,10 @@ class TypeAliasExpander( recursionDepth: Int, withAbbreviatedType: Boolean ): 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 expandedType = expandedProjection.type.asSimpleType() @@ -205,7 +197,8 @@ class TypeAliasExpander( 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( nestedExpansion, type.annotations, @@ -257,7 +250,7 @@ class TypeAliasExpander( val unsubstitutedArgument = unsubstitutedType.arguments[i] val typeParameter = unsubstitutedType.constructor.parameters[i] if (shouldCheckBounds) { - DescriptorResolver.checkBoundsInTypeAlias( + checkBoundsInTypeAlias( reportStrategy, unsubstitutedArgument.type, substitutedArgument.type, @@ -272,12 +265,33 @@ class TypeAliasExpander( companion object { 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) { if (recursionDepth > MAX_RECURSION_DEPTH) { 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) } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeAliasExpansion.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeAliasExpansion.kt similarity index 65% rename from compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeAliasExpansion.kt rename to core/descriptors/src/org/jetbrains/kotlin/types/TypeAliasExpansion.kt index f67a1ab2fbe..a8172d29c0b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeAliasExpansion.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeAliasExpansion.kt @@ -1,25 +1,12 @@ /* - * 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. + * 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. */ -package org.jetbrains.kotlin.resolve +package org.jetbrains.kotlin.types import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor -import org.jetbrains.kotlin.types.TypeConstructor -import org.jetbrains.kotlin.types.TypeProjection class TypeAliasExpansion private constructor( val parent: TypeAliasExpansion?, diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeAliasExpansionReportStrategy.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeAliasExpansionReportStrategy.kt similarity index 69% rename from compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeAliasExpansionReportStrategy.kt rename to core/descriptors/src/org/jetbrains/kotlin/types/TypeAliasExpansionReportStrategy.kt index 515e7fa4e6c..9b029b47990 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeAliasExpansionReportStrategy.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeAliasExpansionReportStrategy.kt @@ -1,25 +1,13 @@ /* - * 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. + * 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. */ -package org.jetbrains.kotlin.resolve +package org.jetbrains.kotlin.types import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor -import org.jetbrains.kotlin.types.KotlinType interface TypeAliasExpansionReportStrategy { fun wrongNumberOfTypeArguments(typeAlias: TypeAliasDescriptor, numberOfParameters: Int) @@ -54,4 +42,4 @@ interface TypeAliasExpansionReportStrategy { override fun repeatedAnnotation(annotation: AnnotationDescriptor) {} } -} \ No newline at end of file +}