[FE] Change resolution scheme

This commit is contained in:
Anastasiya Shadrina
2021-06-17 01:10:54 +07:00
committed by TeamCityServer
parent 1f6746dc74
commit 37495bcba0
39 changed files with 455 additions and 113 deletions
@@ -1198,6 +1198,7 @@ public interface Errors {
// Context receivers
DiagnosticFactory1<KtElement, String> NO_CONTEXT_RECEIVER = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<KtElement, String> MULTIPLE_ARGUMENTS_APPLICABLE_FOR_CONTEXT_RECEIVER = DiagnosticFactory1.create(ERROR);
// Error sets
ImmutableSet<? extends DiagnosticFactory<?>> UNRESOLVED_REFERENCE_DIAGNOSTICS = ImmutableSet.of(
@@ -1089,6 +1089,7 @@ public class DefaultErrorMessages {
MAP.put(CONTRACT_NOT_ALLOWED, "{0}", TO_STRING);
MAP.put(NO_CONTEXT_RECEIVER, "No required context receiver found: {0}", TO_STRING);
MAP.put(MULTIPLE_ARGUMENTS_APPLICABLE_FOR_CONTEXT_RECEIVER, "Multiple arguments applicable for context receiver: {0}", TO_STRING);
MAP.setImmutable();
@@ -162,7 +162,7 @@ public class BodyResolver {
descriptor, localContext != null ? localContext.inferenceSession : null
),
scope -> new LexicalScopeImpl(
scope, descriptor, scope.isOwnerDescriptorAccessibleByLabel(), scope.getImplicitReceivers(),
scope, descriptor, scope.isOwnerDescriptorAccessibleByLabel(), scope.getImplicitReceiver(), scope.getContextReceiversGroup(),
LexicalScopeKind.CONSTRUCTOR_HEADER
),
localContext
@@ -439,7 +439,7 @@ public class BodyResolver {
) {
// Initializing a scope will report errors if any.
new LexicalScopeImpl(
scopeForConstructorResolution, descriptor, true, Collections.emptyList(), LexicalScopeKind.CLASS_HEADER,
scopeForConstructorResolution, descriptor, true, null, Collections.emptyList(), LexicalScopeKind.CLASS_HEADER,
new TraceBasedLocalRedeclarationChecker(trace, overloadChecker),
new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {
@Override
@@ -780,8 +780,8 @@ public class BodyResolver {
LexicalScope originalScope,
ConstructorDescriptor unsubstitutedPrimaryConstructor
) {
return new LexicalScopeImpl(originalScope, unsubstitutedPrimaryConstructor, false, Collections.emptyList(),
LexicalScopeKind.DEFAULT_VALUE, LocalRedeclarationChecker.DO_NOTHING.INSTANCE,
return new LexicalScopeImpl(originalScope, unsubstitutedPrimaryConstructor, false, null,
Collections.emptyList(), LexicalScopeKind.DEFAULT_VALUE, LocalRedeclarationChecker.DO_NOTHING.INSTANCE,
handler -> {
for (ValueParameterDescriptor valueParameter : unsubstitutedPrimaryConstructor.getValueParameters()) {
handler.addVariableDescriptor(valueParameter);
@@ -863,12 +863,7 @@ public class BodyResolver {
LexicalScope accessorDeclaringScope = c.getDeclaringScope(accessor);
assert accessorDeclaringScope != null : "Scope for accessor " + accessor.getText() + " should exists";
LexicalScope headerScope = ScopeUtils.makeScopeForPropertyHeader(accessorDeclaringScope, descriptor);
List<ReceiverParameterDescriptor> implicitReceivers = new ArrayList<>();
ReceiverParameterDescriptor extensionReceiverParameter = descriptor.getExtensionReceiverParameter();
if (extensionReceiverParameter != null) {
implicitReceivers.add(extensionReceiverParameter);
}
return new LexicalScopeImpl(headerScope, descriptor, true, implicitReceivers,
return new LexicalScopeImpl(headerScope, descriptor, true, descriptor.getExtensionReceiverParameter(), descriptor.getContextReceiverParameters(),
LexicalScopeKind.PROPERTY_ACCESSOR_BODY);
}
@@ -1029,7 +1024,7 @@ public class BodyResolver {
KtProperty property = (KtProperty) function.getParent();
SourceElement propertySourceElement = KotlinSourceElementKt.toSourceElement(property);
SyntheticFieldDescriptor fieldDescriptor = new SyntheticFieldDescriptor(accessorDescriptor, propertySourceElement);
innerScope = new LexicalScopeImpl(innerScope, functionDescriptor, true, Collections.emptyList(),
innerScope = new LexicalScopeImpl(innerScope, functionDescriptor, true, null, Collections.emptyList(),
LexicalScopeKind.PROPERTY_ACCESSOR_BODY,
LocalRedeclarationChecker.DO_NOTHING.INSTANCE, handler -> {
handler.addVariableDescriptor(fieldDescriptor);
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.resolve;
import com.google.common.collect.Lists;
import kotlin.Unit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.descriptors.*;
@@ -25,7 +24,6 @@ import org.jetbrains.kotlin.resolve.scopes.*;
import org.jetbrains.kotlin.types.*;
import org.jetbrains.kotlin.types.typeUtil.TypeUtilsKt;
import java.util.ArrayList;
import java.util.List;
public class FunctionDescriptorUtil {
@@ -67,20 +65,9 @@ public class FunctionDescriptorUtil {
@NotNull FunctionDescriptor descriptor,
@NotNull LocalRedeclarationChecker redeclarationChecker
) {
List<ReceiverParameterDescriptor> implicitReceivers = new ArrayList<>();
ReceiverParameterDescriptor extensionReceiverParameter = descriptor.getExtensionReceiverParameter();
if (descriptor.getExtensionReceiverParameter() != null) {
implicitReceivers.add(extensionReceiverParameter);
}
List<ReceiverParameterDescriptor> contextReceiverParameters = descriptor.getContextReceiverParameters();
if (!contextReceiverParameters.isEmpty()) {
implicitReceivers.addAll(
Lists.reverse(contextReceiverParameters)
);
}
return new LexicalScopeImpl(
outerScope, descriptor, true, implicitReceivers,
LexicalScopeKind.FUNCTION_INNER_SCOPE, redeclarationChecker,
outerScope, descriptor, true, descriptor.getExtensionReceiverParameter(),
descriptor.getContextReceiverParameters(), LexicalScopeKind.FUNCTION_INNER_SCOPE, redeclarationChecker,
handler -> {
for (TypeParameterDescriptor typeParameter : descriptor.getTypeParameters()) {
handler.addClassifierDescriptor(typeParameter);
@@ -102,6 +102,15 @@ class DiagnosticReporterByTrackingStrategy(
)
)
}
MultipleArgumentsApplicableForContextReceiver::class.java -> {
val callElement = psiKotlinCall.psiCall.callElement
trace.report(
MULTIPLE_ARGUMENTS_APPLICABLE_FOR_CONTEXT_RECEIVER.on(
callElement,
(diagnostic as MultipleArgumentsApplicableForContextReceiver).receiverDescriptor.value.toString()
)
)
}
}
}
@@ -49,7 +49,8 @@ object DslScopeViolationCallChecker : CallChecker {
) {
val receiversUntilOneFromTheCall =
context.scope.parentsWithSelf
.flatMap { (it as? LexicalScope)?.implicitReceivers ?: emptyList() }
.filterIsInstance<LexicalScope>()
.flatMap { listOfNotNull(it.implicitReceiver) + it.contextReceiversGroup }
.map { it.value }
.takeWhile { it != callImplicitReceiver }.toList()
@@ -366,8 +366,13 @@ class NewResolutionOldInference(
) : ImplicitScopeTower {
private val cache = HashMap<ReceiverValue, ReceiverValueWithSmartCastInfo>()
override fun getImplicitReceivers(scope: LexicalScope): List<ReceiverValueWithSmartCastInfo> =
scope.implicitReceivers.map { cache.getOrPut(it.value) { resolutionContext.transformToReceiverWithSmartCastInfo(it.value) } }
override fun getImplicitReceiver(scope: LexicalScope): ReceiverValueWithSmartCastInfo? =
scope.implicitReceiver?.value?.let {
cache.getOrPut(it) { resolutionContext.transformToReceiverWithSmartCastInfo(it) }
}
override fun getContextReceivers(scope: LexicalScope): List<ReceiverValueWithSmartCastInfo> =
scope.contextReceiversGroup.map { cache.getOrPut(it.value) { resolutionContext.transformToReceiverWithSmartCastInfo(it.value) } }
override fun getNameForGivenImportAlias(name: Name): Name? =
(resolutionContext.call.callElement.containingFile as? KtFile)?.getNameForGivenImportAlias(name)
@@ -400,8 +400,16 @@ class PSICallResolver(
override val implicitsResolutionFilter: ImplicitsExtensionsResolutionFilter get() = this@PSICallResolver.implicitsResolutionFilter
private val cache = HashMap<ReceiverParameterDescriptor, ReceiverValueWithSmartCastInfo>()
override fun getImplicitReceivers(scope: LexicalScope): List<ReceiverValueWithSmartCastInfo> =
scope.implicitReceivers.map { cache.getOrPut(it) { context.transformToReceiverWithSmartCastInfo(it.value) } }
override fun getImplicitReceiver(scope: LexicalScope): ReceiverValueWithSmartCastInfo? {
val implicitReceiver = scope.implicitReceiver ?: return null
return cache.getOrPut(implicitReceiver) {
context.transformToReceiverWithSmartCastInfo(implicitReceiver.value)
}
}
override fun getContextReceivers(scope: LexicalScope): List<ReceiverValueWithSmartCastInfo> =
scope.contextReceiversGroup.map { cache.getOrPut(it) { context.transformToReceiverWithSmartCastInfo(it.value) } }
override fun getNameForGivenImportAlias(name: Name): Name? =
(context.call.callElement.containingFile as? KtFile)?.getNameForGivenImportAlias(name)
@@ -33,7 +33,7 @@ class ClassResolutionScopesSupport(
private val getOuterScope: () -> LexicalScope
) {
private fun scopeWithGenerics(parent: LexicalScope): LexicalScopeImpl {
return LexicalScopeImpl(parent, classDescriptor, false, emptyList(), LexicalScopeKind.CLASS_HEADER) {
return LexicalScopeImpl(parent, classDescriptor, false, null, emptyList(), LexicalScopeKind.CLASS_HEADER) {
classDescriptor.declaredTypeParameters.forEach { addClassifierDescriptor(it) }
}
}
@@ -67,7 +67,8 @@ class ClassResolutionScopesSupport(
scopeWithGenerics,
classDescriptor,
true,
classDescriptor.contextReceivers + classDescriptor.thisAsReceiverParameter,
classDescriptor.thisAsReceiverParameter,
classDescriptor.contextReceivers,
LexicalScopeKind.CLASS_MEMBER_SCOPE
)
}
@@ -94,7 +95,8 @@ class ClassResolutionScopesSupport(
val lexicalChainedScope = LexicalChainedScope.create(
parentForNewScope, ownerDescriptor,
isOwnerDescriptorAccessibleByLabel = false,
implicitReceivers = listOfNotNull(companionObjectDescriptor?.thisAsReceiverParameter),
implicitReceiver = companionObjectDescriptor?.thisAsReceiverParameter,
contextReceiversGroup = emptyList(),
kind = LexicalScopeKind.CLASS_INHERITANCE,
classDescriptor.staticScope,
classDescriptor.unsubstitutedInnerClassesScope,
@@ -144,6 +146,7 @@ fun scopeForInitializerResolution(
classDescriptor.scopeForMemberDeclarationResolution,
parentDescriptor,
false,
null,
emptyList(),
LexicalScopeKind.CLASS_INITIALIZER
) {
@@ -50,7 +50,7 @@ class ValueParameterResolver(
inferenceSession: InferenceSession?
) {
val scopeForDefaultValue =
LexicalScopeImpl(declaringScope, declaringScope.ownerDescriptor, false, listOf(), LexicalScopeKind.DEFAULT_VALUE)
LexicalScopeImpl(declaringScope, declaringScope.ownerDescriptor, false, null, listOf(), LexicalScopeKind.DEFAULT_VALUE)
val contextForDefaultValue = ExpressionTypingContext.newContext(
trace, scopeForDefaultValue, dataFlowInfo, TypeUtils.NO_EXPECTED_TYPE,