From 06340252293944dfc855e0687fe73a34edae0b3b Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Tue, 6 Jun 2017 00:06:36 +0300 Subject: [PATCH] [NI] Propagate known type substitutor to resolution part This commit fixes 'functionNtoStringGeneric' and 'superConstructor' --- .../kotlin/resolve/calls/KotlinCallResolver.kt | 3 ++- .../resolve/calls/components/ResolutionParts.kt | 13 +++++++++++-- .../inference/model/ConstraintPositionAndErrors.kt | 6 +++++- .../resolve/calls/model/KotlinResolverContext.kt | 4 +--- .../resolve/calls/model/ResolutionCandidate.kt | 6 ++++-- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/KotlinCallResolver.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/KotlinCallResolver.kt index 46dc066a5f1..d0ecb925797 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/KotlinCallResolver.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/KotlinCallResolver.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * Copyright 2010-2017 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. @@ -73,6 +73,7 @@ class KotlinCallResolver( it.dispatchReceiver?.let { ReceiverExpressionKotlinCallArgument(it) }, null, it.descriptor, + it.knownTypeParametersResultingSubstitutor, listOf() ) } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt index 9ac47cc51dc..36de3568e89 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt @@ -22,13 +22,15 @@ import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemOperation import org.jetbrains.kotlin.resolve.calls.inference.components.FreshVariableNewTypeSubstitutor import org.jetbrains.kotlin.resolve.calls.inference.model.DeclaredUpperBoundConstraintPosition import org.jetbrains.kotlin.resolve.calls.inference.model.ExplicitTypeParameterConstraintPosition +import org.jetbrains.kotlin.resolve.calls.inference.model.KnownTypeParameterConstraintPosition import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableFromCallableDescriptor import org.jetbrains.kotlin.resolve.calls.inference.substitute import org.jetbrains.kotlin.resolve.calls.model.* import org.jetbrains.kotlin.resolve.calls.smartcasts.getReceiverValueWithSmartCast import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind.* import org.jetbrains.kotlin.resolve.calls.tower.ResolutionCandidateApplicability -import org.jetbrains.kotlin.resolve.calls.tower.ResolutionCandidateApplicability.* +import org.jetbrains.kotlin.resolve.calls.tower.ResolutionCandidateApplicability.INAPPLICABLE +import org.jetbrains.kotlin.resolve.calls.tower.ResolutionCandidateApplicability.RUNTIME_ERROR import org.jetbrains.kotlin.resolve.calls.tower.VisibilityError import org.jetbrains.kotlin.types.TypeConstructor import org.jetbrains.kotlin.types.TypeSubstitutor @@ -119,7 +121,7 @@ internal object CreateDescriptorWithFreshTypeVariables : ResolutionPart { } // optimization - if (typeArgumentMappingByOriginal == NoExplicitArguments) { + if (typeArgumentMappingByOriginal == NoExplicitArguments && knownTypeParametersResultingSubstitutor == null) { descriptorWithFreshTypes = candidateDescriptor.substitute(toFreshVariables) csBuilder.simplify().let { assert(it.isEmpty) { "Substitutor should be empty: $it, call: $kotlinCall" } } return emptyList() @@ -128,6 +130,13 @@ internal object CreateDescriptorWithFreshTypeVariables : ResolutionPart { val typeParameters = candidateDescriptor.typeParameters for (index in typeParameters.indices) { val typeParameter = typeParameters[index] + val knownTypeArgument = knownTypeParametersResultingSubstitutor?.substitute(typeParameter.defaultType) + if (knownTypeArgument != null) { + val freshVariable = toFreshVariables.freshVariables[index] + csBuilder.addEqualityConstraint(freshVariable.defaultType, knownTypeArgument.unwrap(), KnownTypeParameterConstraintPosition(knownTypeArgument)) + continue + } + val typeArgument = typeArgumentMappingByOriginal.getTypeArgument(typeParameter) if (typeArgument is SimpleTypeArgument) { diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrors.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrors.kt index a2503300367..66ae5f9a41c 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrors.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrors.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * Copyright 2010-2017 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. @@ -19,6 +19,7 @@ package org.jetbrains.kotlin.resolve.calls.inference.model import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.resolve.calls.model.* import org.jetbrains.kotlin.resolve.calls.tower.ResolutionCandidateApplicability +import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.UnwrappedType @@ -39,6 +40,9 @@ class ArgumentConstraintPosition(val argument: KotlinCallArgument) : ConstraintP class FixVariableConstraintPosition(val variable: NewTypeVariable) : ConstraintPosition() { override fun toString() = "Fix variable $variable" } +class KnownTypeParameterConstraintPosition(val typeArgument: KotlinType) : ConstraintPosition() { + override fun toString() = "TypeArgument $typeArgument" +} class IncorporationConstraintPosition(val from: ConstraintPosition, val initialConstraint: InitialConstraint) : ConstraintPosition() { override fun toString() = "Incorporate $initialConstraint from position $from" diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinResolverContext.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinResolverContext.kt index 75d621a15d6..fb0fb090346 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinResolverContext.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinResolverContext.kt @@ -18,8 +18,6 @@ package org.jetbrains.kotlin.resolve.calls.model import org.jetbrains.kotlin.builtins.ReflectionTypes import org.jetbrains.kotlin.descriptors.FunctionDescriptor -import org.jetbrains.kotlin.resolve.calls.components.CheckArguments -import org.jetbrains.kotlin.resolve.calls.components.KotlinResolutionCallbacks import org.jetbrains.kotlin.resolve.calls.components.* import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintInjector import org.jetbrains.kotlin.resolve.calls.inference.components.ResultTypeResolver @@ -78,7 +76,7 @@ class SimpleCandidateFactory(val callContext: KotlinCallContext, val kotlinCall: } return SimpleKotlinResolutionCandidate(callContext, kotlinCall, explicitReceiverKind, dispatchArgumentReceiver, extensionArgumentReceiver, - towerCandidate.descriptor, towerCandidate.diagnostics) + towerCandidate.descriptor, null, towerCandidate.diagnostics) } } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolutionCandidate.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolutionCandidate.kt index c0710e855a0..720527b393a 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolutionCandidate.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolutionCandidate.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * Copyright 2010-2017 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. @@ -29,6 +29,7 @@ import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind import org.jetbrains.kotlin.resolve.calls.tower.Candidate import org.jetbrains.kotlin.resolve.calls.tower.ResolutionCandidateStatus import org.jetbrains.kotlin.resolve.calls.tower.isSuccess +import org.jetbrains.kotlin.types.TypeSubstitutor import org.jetbrains.kotlin.utils.SmartList import java.util.* @@ -111,6 +112,7 @@ open class SimpleKotlinResolutionCandidate( val dispatchReceiverArgument: SimpleKotlinCallArgument?, val extensionReceiver: SimpleKotlinCallArgument?, val candidateDescriptor: CallableDescriptor, + val knownTypeParametersResultingSubstitutor: TypeSubstitutor?, initialDiagnostics: Collection ) : AbstractSimpleKotlinResolutionCandidate(NewConstraintSystemImpl(callContext.constraintInjector, callContext.resultTypeResolver), initialDiagnostics) { val csBuilder: ConstraintSystemBuilder get() = constraintSystem.getBuilder() @@ -139,7 +141,7 @@ class ErrorKotlinResolutionCandidate( dispatchReceiverArgument: SimpleKotlinCallArgument?, extensionReceiver: SimpleKotlinCallArgument?, candidateDescriptor: CallableDescriptor -) : SimpleKotlinResolutionCandidate(callContext, kotlinCall, explicitReceiverKind, dispatchReceiverArgument, extensionReceiver, candidateDescriptor, listOf()) { +) : SimpleKotlinResolutionCandidate(callContext, kotlinCall, explicitReceiverKind, dispatchReceiverArgument, extensionReceiver, candidateDescriptor, null, listOf()) { override val resolutionSequence: List get() = emptyList() init {