rr/vd/KT-57514

Revert "Create SimpleFunctionDescriptorImpl under nonCancelableSection"

This reverts commit d959b3a289b24487dba2e36da94a34a0dbb4661e.

Revert "Create PropertyDescriptorImpl under nonCancelableSection"

This reverts commit 362e8c2151c8e18009463fd51b995cce27ac8eb5.

Merge-request: KT-MR-9772
Merged-by: Vladimir Dolzhenko <Vladimir.Dolzhenko@jetbrains.com>
This commit is contained in:
Vladimir Dolzhenko
2023-04-29 06:39:59 +00:00
committed by Space Team
parent ec3e13a091
commit adc5fe6e4e
3 changed files with 382 additions and 429 deletions
@@ -19,8 +19,6 @@ package org.jetbrains.kotlin.resolve;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.intellij.openapi.diagnostic.ControlFlowException;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import kotlin.Pair;
@@ -317,89 +315,81 @@ public class DescriptorResolver {
@NotNull Annotations additionalAnnotations,
@Nullable InferenceSession inferenceSession
) {
try {
KotlinType varargElementType = null;
KotlinType variableType = type;
if (valueParameter.hasModifier(VARARG_KEYWORD)) {
varargElementType = type;
variableType = getVarargParameterType(type);
}
Annotations valueParameterAnnotations = resolveValueParameterAnnotations(scope, valueParameter, trace, additionalAnnotations);
KtDestructuringDeclaration destructuringDeclaration = valueParameter.getDestructuringDeclaration();
Function0<List<VariableDescriptor>> destructuringVariables;
if (destructuringDeclaration != null) {
if (!languageVersionSettings.supportsFeature(LanguageFeature.DestructuringLambdaParameters)) {
trace.report(Errors.UNSUPPORTED_FEATURE.on(valueParameter,
TuplesKt.to(LanguageFeature.DestructuringLambdaParameters, languageVersionSettings)));
}
destructuringVariables = () -> {
ReceiverParameterDescriptor dispatchReceiver = owner.getDispatchReceiverParameter();
assert dispatchReceiver == null || dispatchReceiver.getContainingDeclaration() instanceof ScriptDescriptor
: "Destructuring declarations are only be parsed for lambdas, and they must not have a dispatch receiver";
LexicalScope scopeForDestructuring =
ScopeUtilsKt.createScopeForDestructuring(scope, owner.getExtensionReceiverParameter());
List<VariableDescriptor> result =
destructuringDeclarationResolver.resolveLocalVariablesFromDestructuringDeclaration(
scope,
destructuringDeclaration, new TransientReceiver(type), /* initializer = */ null,
ExpressionTypingContext.newContext(
trace, scopeForDestructuring, DataFlowInfoFactory.EMPTY, TypeUtils.NO_EXPECTED_TYPE,
languageVersionSettings, dataFlowValueFactory, inferenceSession
)
);
modifiersChecker.withTrace(trace).checkModifiersForDestructuringDeclaration(destructuringDeclaration);
return result;
};
}
else {
destructuringVariables = null;
}
Name parameterName;
if (destructuringDeclaration == null) {
// NB: val/var for parameter is only allowed in primary constructors where single underscore names are still prohibited.
// The problem with val/var is that when lazy resolve try to find their descriptor, it searches through the member scope
// of containing class where, it can not find a descriptor with special name.
// Thus, to preserve behavior, we don't use a special name for val/var.
parameterName = !valueParameter.hasValOrVar() && UnderscoreUtilKt.isSingleUnderscore(valueParameter)
? Name.special("<anonymous parameter " + index + ">")
: KtPsiUtil.safeName(valueParameter.getName());
}
else {
parameterName = Name.special("<name for destructuring parameter " + index + ">");
}
ValueParameterDescriptorImpl valueParameterDescriptor = ValueParameterDescriptorImpl.createWithDestructuringDeclarations(
owner,
null,
index,
valueParameterAnnotations,
parameterName,
variableType,
valueParameter.hasDefaultValue(),
valueParameter.hasModifier(CROSSINLINE_KEYWORD),
valueParameter.hasModifier(NOINLINE_KEYWORD),
varargElementType,
KotlinSourceElementKt.toSourceElement(valueParameter),
destructuringVariables
);
trace.record(BindingContext.VALUE_PARAMETER, valueParameter, valueParameterDescriptor);
return valueParameterDescriptor;
KotlinType varargElementType = null;
KotlinType variableType = type;
if (valueParameter.hasModifier(VARARG_KEYWORD)) {
varargElementType = type;
variableType = getVarargParameterType(type);
}
catch (Exception e) {
if (e instanceof ControlFlowException) {
throw new IllegalStateException("Method should be run under nonCancelableSection", e);
Annotations valueParameterAnnotations = resolveValueParameterAnnotations(scope, valueParameter, trace, additionalAnnotations);
KtDestructuringDeclaration destructuringDeclaration = valueParameter.getDestructuringDeclaration();
Function0<List<VariableDescriptor>> destructuringVariables;
if (destructuringDeclaration != null) {
if (!languageVersionSettings.supportsFeature(LanguageFeature.DestructuringLambdaParameters)) {
trace.report(Errors.UNSUPPORTED_FEATURE.on(valueParameter,
TuplesKt.to(LanguageFeature.DestructuringLambdaParameters, languageVersionSettings)));
}
throw e;
destructuringVariables = () -> {
ReceiverParameterDescriptor dispatchReceiver = owner.getDispatchReceiverParameter();
assert dispatchReceiver == null || dispatchReceiver.getContainingDeclaration() instanceof ScriptDescriptor
: "Destructuring declarations are only be parsed for lambdas, and they must not have a dispatch receiver";
LexicalScope scopeForDestructuring =
ScopeUtilsKt.createScopeForDestructuring(scope, owner.getExtensionReceiverParameter());
List<VariableDescriptor> result =
destructuringDeclarationResolver.resolveLocalVariablesFromDestructuringDeclaration(
scope,
destructuringDeclaration, new TransientReceiver(type), /* initializer = */ null,
ExpressionTypingContext.newContext(
trace, scopeForDestructuring, DataFlowInfoFactory.EMPTY, TypeUtils.NO_EXPECTED_TYPE,
languageVersionSettings, dataFlowValueFactory, inferenceSession
)
);
modifiersChecker.withTrace(trace).checkModifiersForDestructuringDeclaration(destructuringDeclaration);
return result;
};
}
else {
destructuringVariables = null;
}
Name parameterName;
if (destructuringDeclaration == null) {
// NB: val/var for parameter is only allowed in primary constructors where single underscore names are still prohibited.
// The problem with val/var is that when lazy resolve try to find their descriptor, it searches through the member scope
// of containing class where, it can not find a descriptor with special name.
// Thus, to preserve behavior, we don't use a special name for val/var.
parameterName = !valueParameter.hasValOrVar() && UnderscoreUtilKt.isSingleUnderscore(valueParameter)
? Name.special("<anonymous parameter " + index + ">")
: KtPsiUtil.safeName(valueParameter.getName());
}
else {
parameterName = Name.special("<name for destructuring parameter " + index + ">");
}
ValueParameterDescriptorImpl valueParameterDescriptor = ValueParameterDescriptorImpl.createWithDestructuringDeclarations(
owner,
null,
index,
valueParameterAnnotations,
parameterName,
variableType,
valueParameter.hasDefaultValue(),
valueParameter.hasModifier(CROSSINLINE_KEYWORD),
valueParameter.hasModifier(NOINLINE_KEYWORD),
varargElementType,
KotlinSourceElementKt.toSourceElement(valueParameter),
destructuringVariables
);
trace.record(BindingContext.VALUE_PARAMETER, valueParameter, valueParameterDescriptor);
return valueParameterDescriptor;
}
@NotNull
@@ -923,150 +913,148 @@ public class DescriptorResolver {
annotationSplitter.getOtherAnnotations())
);
return ProgressManager.getInstance().computeInNonCancelableSection(() -> {
PropertyDescriptorImpl propertyDescriptor = PropertyDescriptorImpl.create(
container,
propertyAnnotations,
modality,
visibility,
isVar,
KtPsiUtil.safeName(variableDeclaration.getName()),
CallableMemberDescriptor.Kind.DECLARATION,
KotlinSourceElementKt.toSourceElement(variableDeclaration),
modifierList != null && modifierList.hasModifier(KtTokens.LATEINIT_KEYWORD),
modifierList != null && modifierList.hasModifier(KtTokens.CONST_KEYWORD),
modifierList != null && PsiUtilsKt.hasExpectModifier(modifierList) && container instanceof PackageFragmentDescriptor ||
container instanceof ClassDescriptor && ((ClassDescriptor) container).isExpect(),
modifierList != null && PsiUtilsKt.hasActualModifier(modifierList),
modifierList != null && modifierList.hasModifier(KtTokens.EXTERNAL_KEYWORD),
propertyInfo.getHasDelegate()
);
PropertyDescriptorImpl propertyDescriptor = PropertyDescriptorImpl.create(
container,
propertyAnnotations,
modality,
visibility,
isVar,
KtPsiUtil.safeName(variableDeclaration.getName()),
CallableMemberDescriptor.Kind.DECLARATION,
KotlinSourceElementKt.toSourceElement(variableDeclaration),
modifierList != null && modifierList.hasModifier(KtTokens.LATEINIT_KEYWORD),
modifierList != null && modifierList.hasModifier(KtTokens.CONST_KEYWORD),
modifierList != null && PsiUtilsKt.hasExpectModifier(modifierList) && container instanceof PackageFragmentDescriptor ||
container instanceof ClassDescriptor && ((ClassDescriptor) container).isExpect(),
modifierList != null && PsiUtilsKt.hasActualModifier(modifierList),
modifierList != null && modifierList.hasModifier(KtTokens.EXTERNAL_KEYWORD),
propertyInfo.getHasDelegate()
);
List<TypeParameterDescriptorImpl> typeParameterDescriptors;
LexicalScope scopeForDeclarationResolutionWithTypeParameters;
LexicalScope scopeForInitializerResolutionWithTypeParameters;
KotlinType receiverType = null;
List<TypeParameterDescriptorImpl> typeParameterDescriptors;
LexicalScope scopeForDeclarationResolutionWithTypeParameters;
LexicalScope scopeForInitializerResolutionWithTypeParameters;
KotlinType receiverType = null;
{
List<KtTypeParameter> typeParameters = variableDeclaration.getTypeParameters();
if (typeParameters.isEmpty()) {
scopeForDeclarationResolutionWithTypeParameters = scopeForDeclarationResolution;
scopeForInitializerResolutionWithTypeParameters = scopeForInitializerResolution;
typeParameterDescriptors = Collections.emptyList();
}
else {
LexicalWritableScope writableScopeForDeclarationResolution = new LexicalWritableScope(
scopeForDeclarationResolution, container, false, new TraceBasedLocalRedeclarationChecker(trace, overloadChecker),
LexicalScopeKind.PROPERTY_HEADER);
LexicalWritableScope writableScopeForInitializerResolution = new LexicalWritableScope(
scopeForInitializerResolution, container, false, LocalRedeclarationChecker.DO_NOTHING.INSTANCE,
LexicalScopeKind.PROPERTY_HEADER);
typeParameterDescriptors = resolveTypeParametersForDescriptor(
propertyDescriptor,
scopeForDeclarationResolution, typeParameters, trace);
for (TypeParameterDescriptor descriptor : typeParameterDescriptors) {
writableScopeForDeclarationResolution.addClassifierDescriptor(descriptor);
writableScopeForInitializerResolution.addClassifierDescriptor(descriptor);
}
writableScopeForDeclarationResolution.freeze();
writableScopeForInitializerResolution.freeze();
resolveGenericBounds(variableDeclaration, propertyDescriptor, writableScopeForDeclarationResolution, typeParameterDescriptors, trace);
scopeForDeclarationResolutionWithTypeParameters = writableScopeForDeclarationResolution;
scopeForInitializerResolutionWithTypeParameters = writableScopeForInitializerResolution;
}
{
List<KtTypeParameter> typeParameters = variableDeclaration.getTypeParameters();
if (typeParameters.isEmpty()) {
scopeForDeclarationResolutionWithTypeParameters = scopeForDeclarationResolution;
scopeForInitializerResolutionWithTypeParameters = scopeForInitializerResolution;
typeParameterDescriptors = Collections.emptyList();
}
else {
LexicalWritableScope writableScopeForDeclarationResolution = new LexicalWritableScope(
scopeForDeclarationResolution, container, false, new TraceBasedLocalRedeclarationChecker(trace, overloadChecker),
LexicalScopeKind.PROPERTY_HEADER);
LexicalWritableScope writableScopeForInitializerResolution = new LexicalWritableScope(
scopeForInitializerResolution, container, false, LocalRedeclarationChecker.DO_NOTHING.INSTANCE,
LexicalScopeKind.PROPERTY_HEADER);
typeParameterDescriptors = resolveTypeParametersForDescriptor(
propertyDescriptor,
scopeForDeclarationResolution, typeParameters, trace);
for (TypeParameterDescriptor descriptor : typeParameterDescriptors) {
writableScopeForDeclarationResolution.addClassifierDescriptor(descriptor);
writableScopeForInitializerResolution.addClassifierDescriptor(descriptor);
}
writableScopeForDeclarationResolution.freeze();
writableScopeForInitializerResolution.freeze();
resolveGenericBounds(variableDeclaration, propertyDescriptor, writableScopeForDeclarationResolution, typeParameterDescriptors, trace);
scopeForDeclarationResolutionWithTypeParameters = writableScopeForDeclarationResolution;
scopeForInitializerResolutionWithTypeParameters = writableScopeForInitializerResolution;
}
}
KtTypeReference receiverTypeRef = variableDeclaration.getReceiverTypeReference();
ReceiverParameterDescriptor receiverDescriptor = null;
KtTypeReference receiverTypeRef = variableDeclaration.getReceiverTypeReference();
ReceiverParameterDescriptor receiverDescriptor = null;
if (receiverTypeRef != null) {
receiverType = typeResolver.resolveType(scopeForDeclarationResolutionWithTypeParameters, receiverTypeRef, trace, true);
AnnotationSplitter splitter = new AnnotationSplitter(storageManager, receiverType.getAnnotations(), EnumSet.of(RECEIVER));
receiverDescriptor = DescriptorFactory.createExtensionReceiverParameterForCallable(
propertyDescriptor, receiverType, splitter.getAnnotationsForTarget(RECEIVER)
);
}
List<KtContextReceiver> contextReceivers = variableDeclaration.getContextReceivers();
List<ReceiverParameterDescriptor> contextReceiverDescriptors = IntStream.range(0, contextReceivers.size()).mapToObj(index -> {
KtContextReceiver contextReceiver = contextReceivers.get(index);
KtTypeReference typeReference = contextReceiver.typeReference();
if (typeReference == null) {
return null;
}
KotlinType type = typeResolver.resolveType(scopeForDeclarationResolutionWithTypeParameters, typeReference, trace, true);
AnnotationSplitter splitter = new AnnotationSplitter(storageManager, type.getAnnotations(), EnumSet.of(RECEIVER));
return DescriptorFactory.createContextReceiverParameterForCallable(
propertyDescriptor, type, contextReceiver.labelNameAsName(), splitter.getAnnotationsForTarget(RECEIVER), index
);
}).collect(Collectors.toList());
if (languageVersionSettings.supportsFeature(LanguageFeature.ContextReceivers)) {
Multimap<String, ReceiverParameterDescriptor> nameToReceiverMap = HashMultimap.create();
if (receiverTypeRef != null) {
receiverType = typeResolver.resolveType(scopeForDeclarationResolutionWithTypeParameters, receiverTypeRef, trace, true);
AnnotationSplitter splitter = new AnnotationSplitter(storageManager, receiverType.getAnnotations(), EnumSet.of(RECEIVER));
receiverDescriptor = DescriptorFactory.createExtensionReceiverParameterForCallable(
propertyDescriptor, receiverType, splitter.getAnnotationsForTarget(RECEIVER)
);
String receiverName = receiverTypeRef.nameForReceiverLabel();
if (receiverName != null) {
nameToReceiverMap.put(receiverName, receiverDescriptor);
}
}
List<KtContextReceiver> contextReceivers = variableDeclaration.getContextReceivers();
List<ReceiverParameterDescriptor> contextReceiverDescriptors = IntStream.range(0, contextReceivers.size()).mapToObj(index -> {
KtContextReceiver contextReceiver = contextReceivers.get(index);
KtTypeReference typeReference = contextReceiver.typeReference();
if (typeReference == null) {
return null;
for (int i = 0; i < contextReceivers.size(); i++) {
String contextReceiverName = contextReceivers.get(i).name();
if (contextReceiverName != null) {
nameToReceiverMap.put(contextReceiverName, contextReceiverDescriptors.get(i));
}
KotlinType type = typeResolver.resolveType(scopeForDeclarationResolutionWithTypeParameters, typeReference, trace, true);
AnnotationSplitter splitter = new AnnotationSplitter(storageManager, type.getAnnotations(), EnumSet.of(RECEIVER));
return DescriptorFactory.createContextReceiverParameterForCallable(
propertyDescriptor, type, contextReceiver.labelNameAsName(), splitter.getAnnotationsForTarget(RECEIVER), index
);
}).collect(Collectors.toList());
if (languageVersionSettings.supportsFeature(LanguageFeature.ContextReceivers)) {
Multimap<String, ReceiverParameterDescriptor> nameToReceiverMap = HashMultimap.create();
if (receiverTypeRef != null) {
String receiverName = receiverTypeRef.nameForReceiverLabel();
if (receiverName != null) {
nameToReceiverMap.put(receiverName, receiverDescriptor);
}
}
for (int i = 0; i < contextReceivers.size(); i++) {
String contextReceiverName = contextReceivers.get(i).name();
if (contextReceiverName != null) {
nameToReceiverMap.put(contextReceiverName, contextReceiverDescriptors.get(i));
}
}
trace.record(DESCRIPTOR_TO_CONTEXT_RECEIVER_MAP, propertyDescriptor, nameToReceiverMap);
}
trace.record(DESCRIPTOR_TO_CONTEXT_RECEIVER_MAP, propertyDescriptor, nameToReceiverMap);
}
LexicalScope scopeForInitializer = ScopeUtils.makeScopeForPropertyInitializer(scopeForInitializerResolutionWithTypeParameters, propertyDescriptor);
KotlinType propertyType = propertyInfo.getVariableType();
KotlinType typeIfKnown = propertyType != null ? propertyType : variableTypeAndInitializerResolver.resolveTypeNullable(
propertyDescriptor, scopeForInitializer,
variableDeclaration, dataFlowInfo, inferenceSession,
trace, /* local = */ false
);
LexicalScope scopeForInitializer = ScopeUtils.makeScopeForPropertyInitializer(scopeForInitializerResolutionWithTypeParameters, propertyDescriptor);
KotlinType propertyType = propertyInfo.getVariableType();
KotlinType typeIfKnown = propertyType != null ? propertyType : variableTypeAndInitializerResolver.resolveTypeNullable(
propertyDescriptor, scopeForInitializer,
variableDeclaration, dataFlowInfo, inferenceSession,
trace, /* local = */ false
);
PropertyGetterDescriptorImpl getter = resolvePropertyGetterDescriptor(
scopeForDeclarationResolutionWithTypeParameters,
variableDeclaration,
propertyDescriptor,
annotationSplitter,
trace,
typeIfKnown,
propertyInfo.getPropertyGetter(),
propertyInfo.getHasDelegate(),
inferenceSession
);
PropertyGetterDescriptorImpl getter = resolvePropertyGetterDescriptor(
scopeForDeclarationResolutionWithTypeParameters,
variableDeclaration,
propertyDescriptor,
annotationSplitter,
trace,
typeIfKnown,
propertyInfo.getPropertyGetter(),
propertyInfo.getHasDelegate(),
inferenceSession
);
KotlinType type = typeIfKnown != null ? typeIfKnown : getter.getReturnType();
KotlinType type = typeIfKnown != null ? typeIfKnown : getter.getReturnType();
assert type != null : "At least getter type must be initialized via resolvePropertyGetterDescriptor";
assert type != null : "At least getter type must be initialized via resolvePropertyGetterDescriptor";
variableTypeAndInitializerResolver.setConstantForVariableIfNeeded(
propertyDescriptor, scopeForInitializer, variableDeclaration, dataFlowInfo, type, inferenceSession, trace
);
variableTypeAndInitializerResolver.setConstantForVariableIfNeeded(
propertyDescriptor, scopeForInitializer, variableDeclaration, dataFlowInfo, type, inferenceSession, trace
);
propertyDescriptor.setType(type, typeParameterDescriptors, getDispatchReceiverParameterIfNeeded(container), receiverDescriptor,
contextReceiverDescriptors);
propertyDescriptor.setType(type, typeParameterDescriptors, getDispatchReceiverParameterIfNeeded(container), receiverDescriptor,
contextReceiverDescriptors);
PropertySetterDescriptor setter = resolvePropertySetterDescriptor(
scopeForDeclarationResolutionWithTypeParameters,
variableDeclaration,
propertyDescriptor,
annotationSplitter,
trace,
propertyInfo.getPropertySetter(),
propertyInfo.getHasDelegate(),
inferenceSession
);
PropertySetterDescriptor setter = resolvePropertySetterDescriptor(
scopeForDeclarationResolutionWithTypeParameters,
variableDeclaration,
propertyDescriptor,
annotationSplitter,
trace,
propertyInfo.getPropertySetter(),
propertyInfo.getHasDelegate(),
inferenceSession
);
propertyDescriptor.initialize(
getter, setter,
new FieldDescriptorImpl(annotationSplitter.getAnnotationsForTarget(FIELD), propertyDescriptor),
new FieldDescriptorImpl(annotationSplitter.getAnnotationsForTarget(PROPERTY_DELEGATE_FIELD), propertyDescriptor)
);
trace.record(BindingContext.VARIABLE, variableDeclaration, propertyDescriptor);
return propertyDescriptor;
});
propertyDescriptor.initialize(
getter, setter,
new FieldDescriptorImpl(annotationSplitter.getAnnotationsForTarget(FIELD), propertyDescriptor),
new FieldDescriptorImpl(annotationSplitter.getAnnotationsForTarget(PROPERTY_DELEGATE_FIELD), propertyDescriptor)
);
trace.record(BindingContext.VARIABLE, variableDeclaration, propertyDescriptor);
return propertyDescriptor;
}
@NotNull
@@ -17,8 +17,6 @@
package org.jetbrains.kotlin.resolve
import com.google.common.collect.HashMultimap
import com.intellij.openapi.diagnostic.ControlFlowException
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.util.ThrowableComputable
import com.intellij.psi.PsiElement
import com.intellij.util.AstLoadingFilter
@@ -73,7 +71,6 @@ import org.jetbrains.kotlin.types.isError
import org.jetbrains.kotlin.types.typeUtil.replaceAnnotations
import java.util.*
class FunctionDescriptorResolver(
private val typeResolver: TypeResolver,
private val descriptorResolver: DescriptorResolver,
@@ -132,21 +129,19 @@ class FunctionDescriptorResolver(
CallableMemberDescriptor.Kind.DECLARATION,
function.toSourceElement()
)
return computeInNonCancelableSection {
initializeFunctionDescriptorAndExplicitReturnType(
containingDescriptor,
scope,
function,
functionDescriptor,
trace,
expectedFunctionType,
dataFlowInfo,
inferenceSession
)
initializeFunctionReturnTypeBasedOnFunctionBody(scope, function, functionDescriptor, trace, dataFlowInfo, inferenceSession)
BindingContextUtils.recordFunctionDeclarationToDescriptor(trace, function, functionDescriptor)
functionDescriptor
}
initializeFunctionDescriptorAndExplicitReturnType(
containingDescriptor,
scope,
function,
functionDescriptor,
trace,
expectedFunctionType,
dataFlowInfo,
inferenceSession
)
initializeFunctionReturnTypeBasedOnFunctionBody(scope, function, functionDescriptor, trace, dataFlowInfo, inferenceSession)
BindingContextUtils.recordFunctionDeclarationToDescriptor(trace, function, functionDescriptor)
return functionDescriptor
}
private fun initializeFunctionReturnTypeBasedOnFunctionBody(
@@ -187,124 +182,117 @@ class FunctionDescriptorResolver(
dataFlowInfo: DataFlowInfo,
inferenceSession: InferenceSession?
) {
try {
val headerScope = LexicalWritableScope(
scope, functionDescriptor, true,
TraceBasedLocalRedeclarationChecker(trace, overloadChecker), LexicalScopeKind.FUNCTION_HEADER
val headerScope = LexicalWritableScope(
scope, functionDescriptor, true,
TraceBasedLocalRedeclarationChecker(trace, overloadChecker), LexicalScopeKind.FUNCTION_HEADER
)
val typeParameterDescriptors =
descriptorResolver.resolveTypeParametersForDescriptor(functionDescriptor, headerScope, scope, function.typeParameters, trace)
descriptorResolver.resolveGenericBounds(function, functionDescriptor, headerScope, typeParameterDescriptors, trace)
val receiverTypeRef = function.receiverTypeReference
val receiverType =
if (receiverTypeRef != null) {
typeResolver.resolveType(headerScope, receiverTypeRef, trace, true)
} else {
if (function is KtFunctionLiteral) expectedFunctionType.getReceiverType() else null
}
val contextReceivers = function.contextReceivers
val contextReceiverTypes =
if (function is KtFunctionLiteral) expectedFunctionType.getContextReceiversTypes()
else contextReceivers
.mapNotNull {
val typeReference = it.typeReference() ?: return@mapNotNull null
val type = typeResolver.resolveType(headerScope, typeReference, trace, true)
ContextReceiverTypeWithLabel(type, it.labelNameAsName())
}
val valueParameterDescriptors =
createValueParameterDescriptors(function, functionDescriptor, headerScope, trace, expectedFunctionType, inferenceSession)
headerScope.freeze()
val returnType = function.typeReference?.let { typeResolver.resolveType(headerScope, it, trace, true) }
val visibility = resolveVisibilityFromModifiers(function, getDefaultVisibility(function, container))
val modality = resolveMemberModalityFromModifiers(
function, getDefaultModality(container, visibility, function.hasBody()),
trace.bindingContext, container
)
val contractProvider = getContractProvider(functionDescriptor, trace, scope, dataFlowInfo, function, inferenceSession)
val userData = mutableMapOf<CallableDescriptor.UserDataKey<*>, Any>().apply {
if (contractProvider != null) {
put(ContractProviderKey, contractProvider)
}
if (receiverType != null && expectedFunctionType.functionTypeExpected() && !expectedFunctionType.annotations.isEmpty()) {
put(DslMarkerUtils.FunctionTypeAnnotationsKey, expectedFunctionType.annotations)
}
}
val extensionReceiver = receiverType?.let {
val splitter = AnnotationSplitter(storageManager, it.annotations, EnumSet.of(AnnotationUseSiteTarget.RECEIVER))
DescriptorFactory.createExtensionReceiverParameterForCallable(
functionDescriptor, it, splitter.getAnnotationsForTarget(AnnotationUseSiteTarget.RECEIVER)
)
val typeParameterDescriptors =
descriptorResolver.resolveTypeParametersForDescriptor(functionDescriptor, headerScope, scope, function.typeParameters, trace)
descriptorResolver.resolveGenericBounds(function, functionDescriptor, headerScope, typeParameterDescriptors, trace)
val receiverTypeRef = function.receiverTypeReference
val receiverType =
if (receiverTypeRef != null) {
typeResolver.resolveType(headerScope, receiverTypeRef, trace, true)
} else {
if (function is KtFunctionLiteral) expectedFunctionType.getReceiverType() else null
}
val contextReceivers = function.contextReceivers
val contextReceiverTypes =
if (function is KtFunctionLiteral) expectedFunctionType.getContextReceiversTypes()
else contextReceivers
.mapNotNull {
val typeReference = it.typeReference() ?: return@mapNotNull null
val type = typeResolver.resolveType(headerScope, typeReference, trace, true)
ContextReceiverTypeWithLabel(type, it.labelNameAsName())
}
val valueParameterDescriptors =
createValueParameterDescriptors(function, functionDescriptor, headerScope, trace, expectedFunctionType, inferenceSession)
headerScope.freeze()
val returnType = function.typeReference?.let { typeResolver.resolveType(headerScope, it, trace, true) }
val visibility = resolveVisibilityFromModifiers(function, getDefaultVisibility(function, container))
val modality = resolveMemberModalityFromModifiers(
function, getDefaultModality(container, visibility, function.hasBody()),
trace.bindingContext, container
}
val contextReceiverDescriptors = contextReceiverTypes.mapIndexedNotNull { index, contextReceiver ->
val splitter = AnnotationSplitter(storageManager, contextReceiver.type.annotations, EnumSet.of(AnnotationUseSiteTarget.RECEIVER))
DescriptorFactory.createContextReceiverParameterForCallable(
functionDescriptor,
contextReceiver.type,
contextReceiver.label,
splitter.getAnnotationsForTarget(AnnotationUseSiteTarget.RECEIVER),
index
)
}
val contractProvider = getContractProvider(functionDescriptor, trace, scope, dataFlowInfo, function, inferenceSession)
val userData = mutableMapOf<CallableDescriptor.UserDataKey<*>, Any>().apply {
if (contractProvider != null) {
put(ContractProviderKey, contractProvider)
}
if (receiverType != null && expectedFunctionType.functionTypeExpected() && !expectedFunctionType.annotations.isEmpty()) {
put(DslMarkerUtils.FunctionTypeAnnotationsKey, expectedFunctionType.annotations)
if (languageVersionSettings.supportsFeature(LanguageFeature.ContextReceivers)) {
val labelNameToReceiverMap = HashMultimap.create<String, ReceiverParameterDescriptor>()
if (receiverTypeRef != null && extensionReceiver != null) {
receiverTypeRef.nameForReceiverLabel()?.let {
labelNameToReceiverMap.put(it, extensionReceiver)
}
}
val extensionReceiver = receiverType?.let {
val splitter = AnnotationSplitter(storageManager, it.annotations, EnumSet.of(AnnotationUseSiteTarget.RECEIVER))
DescriptorFactory.createExtensionReceiverParameterForCallable(
functionDescriptor, it, splitter.getAnnotationsForTarget(AnnotationUseSiteTarget.RECEIVER)
)
}
val contextReceiverDescriptors = contextReceiverTypes.mapIndexedNotNull { index, contextReceiver ->
val splitter = AnnotationSplitter(storageManager, contextReceiver.type.annotations, EnumSet.of(AnnotationUseSiteTarget.RECEIVER))
DescriptorFactory.createContextReceiverParameterForCallable(
functionDescriptor,
contextReceiver.type,
contextReceiver.label,
splitter.getAnnotationsForTarget(AnnotationUseSiteTarget.RECEIVER),
index
)
}
if (languageVersionSettings.supportsFeature(LanguageFeature.ContextReceivers)) {
val labelNameToReceiverMap = HashMultimap.create<String, ReceiverParameterDescriptor>()
if (receiverTypeRef != null && extensionReceiver != null) {
receiverTypeRef.nameForReceiverLabel()?.let {
labelNameToReceiverMap.put(it, extensionReceiver)
contextReceiverDescriptors.zip(0 until contextReceivers.size).reversed()
.forEach { (contextReceiverDescriptor, i) ->
contextReceivers[i].name()?.let {
labelNameToReceiverMap.put(it, contextReceiverDescriptor)
}
}
contextReceiverDescriptors.zip(0 until contextReceivers.size).reversed()
.forEach { (contextReceiverDescriptor, i) ->
contextReceivers[i].name()?.let {
labelNameToReceiverMap.put(it, contextReceiverDescriptor)
}
}
trace.record(BindingContext.DESCRIPTOR_TO_CONTEXT_RECEIVER_MAP, functionDescriptor, labelNameToReceiverMap)
}
trace.record(BindingContext.DESCRIPTOR_TO_CONTEXT_RECEIVER_MAP, functionDescriptor, labelNameToReceiverMap)
}
functionDescriptor.initialize(
extensionReceiver,
getDispatchReceiverParameterIfNeeded(container),
contextReceiverDescriptors,
typeParameterDescriptors,
valueParameterDescriptors,
returnType,
modality,
visibility,
userData.takeIf { it.isNotEmpty() }
)
functionDescriptor.initialize(
extensionReceiver,
getDispatchReceiverParameterIfNeeded(container),
contextReceiverDescriptors,
typeParameterDescriptors,
valueParameterDescriptors,
returnType,
modality,
visibility,
userData.takeIf { it.isNotEmpty() }
)
functionDescriptor.isOperator = function.hasModifier(KtTokens.OPERATOR_KEYWORD)
functionDescriptor.isInfix = function.hasModifier(KtTokens.INFIX_KEYWORD)
functionDescriptor.isExternal = function.hasModifier(KtTokens.EXTERNAL_KEYWORD)
functionDescriptor.isInline = function.hasModifier(KtTokens.INLINE_KEYWORD)
functionDescriptor.isTailrec = function.hasModifier(KtTokens.TAILREC_KEYWORD)
functionDescriptor.isSuspend = function.hasModifier(KtTokens.SUSPEND_KEYWORD)
functionDescriptor.isExpect = container is PackageFragmentDescriptor && function.hasExpectModifier() ||
container is ClassDescriptor && container.isExpect
functionDescriptor.isActual = function.hasActualModifier()
functionDescriptor.isOperator = function.hasModifier(KtTokens.OPERATOR_KEYWORD)
functionDescriptor.isInfix = function.hasModifier(KtTokens.INFIX_KEYWORD)
functionDescriptor.isExternal = function.hasModifier(KtTokens.EXTERNAL_KEYWORD)
functionDescriptor.isInline = function.hasModifier(KtTokens.INLINE_KEYWORD)
functionDescriptor.isTailrec = function.hasModifier(KtTokens.TAILREC_KEYWORD)
functionDescriptor.isSuspend = function.hasModifier(KtTokens.SUSPEND_KEYWORD)
functionDescriptor.isExpect = container is PackageFragmentDescriptor && function.hasExpectModifier() ||
container is ClassDescriptor && container.isExpect
functionDescriptor.isActual = function.hasActualModifier()
receiverType?.let { ForceResolveUtil.forceResolveAllContents(it.annotations) }
for (valueParameterDescriptor in valueParameterDescriptors) {
ForceResolveUtil.forceResolveAllContents(valueParameterDescriptor.type.annotations)
}
} catch (e: Exception) {
if (e is ControlFlowException) {
throw IllegalStateException("Method should be run under nonCancelableSection", e)
}
throw e
receiverType?.let { ForceResolveUtil.forceResolveAllContents(it.annotations) }
for (valueParameterDescriptor in valueParameterDescriptors) {
ForceResolveUtil.forceResolveAllContents(valueParameterDescriptor.type.annotations)
}
}
@@ -456,6 +444,8 @@ class FunctionDescriptorResolver(
constructorDescriptor.isActual = modifierList?.hasActualModifier() == true ||
// We don't require 'actual' for constructors of actual annotations
classDescriptor.kind == ClassKind.ANNOTATION_CLASS && classDescriptor.isActual
if (declarationToTrace is PsiElement)
trace.record(BindingContext.CONSTRUCTOR, declarationToTrace, constructorDescriptor)
val parameterScope = LexicalWritableScope(
scope,
constructorDescriptor,
@@ -463,27 +453,20 @@ class FunctionDescriptorResolver(
TraceBasedLocalRedeclarationChecker(trace, overloadChecker),
LexicalScopeKind.CONSTRUCTOR_HEADER
)
return computeInNonCancelableSection {
if (declarationToTrace is PsiElement)
trace.record(BindingContext.CONSTRUCTOR, declarationToTrace, constructorDescriptor)
val constructor = constructorDescriptor.initialize(
resolveValueParameters(
constructorDescriptor, parameterScope, valueParameters, trace, null, inferenceSession
),
resolveVisibilityFromModifiers(
modifierList,
DescriptorUtils.getDefaultConstructorVisibility(
classDescriptor,
languageVersionSettings.supportsFeature(LanguageFeature.AllowSealedInheritorsInDifferentFilesOfSamePackage)
)
)
val constructor = constructorDescriptor.initialize(
resolveValueParameters(
constructorDescriptor, parameterScope, valueParameters, trace, null, inferenceSession
),
resolveVisibilityFromModifiers(
modifierList,
DescriptorUtils.getDefaultConstructorVisibility(classDescriptor, languageVersionSettings.supportsFeature(LanguageFeature.AllowSealedInheritorsInDifferentFilesOfSamePackage))
)
constructor.returnType = classDescriptor.defaultType
if (DescriptorUtils.isAnnotationClass(classDescriptor)) {
CompileTimeConstantUtils.checkConstructorParametersType(valueParameters, trace)
}
constructor
)
constructor.returnType = classDescriptor.defaultType
if (DescriptorUtils.isAnnotationClass(classDescriptor)) {
CompileTimeConstantUtils.checkConstructorParametersType(valueParameters, trace)
}
return constructor
}
private fun resolveValueParameters(
@@ -494,67 +477,57 @@ class FunctionDescriptorResolver(
expectedParameterTypes: List<KotlinType>?,
inferenceSession: InferenceSession?
): List<ValueParameterDescriptor> {
try {
val result = ArrayList<ValueParameterDescriptor>()
val result = ArrayList<ValueParameterDescriptor>()
for (i in valueParameters.indices) {
val valueParameter = valueParameters[i]
val typeReference = valueParameter.typeReference
val expectedType = expectedParameterTypes?.let { if (i < it.size) it[i] else null }?.takeUnless { TypeUtils.noExpectedType(it) }
for (i in valueParameters.indices) {
val valueParameter = valueParameters[i]
val typeReference = valueParameter.typeReference
val expectedType = expectedParameterTypes?.let { if (i < it.size) it[i] else null }?.takeUnless { TypeUtils.noExpectedType(it) }
val type: KotlinType
if (typeReference != null) {
type = typeResolver.resolveType(parameterScope, typeReference, trace, true)
if (expectedType != null) {
if (!KotlinTypeChecker.DEFAULT.isSubtypeOf(expectedType, type)) {
trace.report(EXPECTED_PARAMETER_TYPE_MISMATCH.on(valueParameter, expectedType))
}
val type: KotlinType
if (typeReference != null) {
type = typeResolver.resolveType(parameterScope, typeReference, trace, true)
if (expectedType != null) {
if (!KotlinTypeChecker.DEFAULT.isSubtypeOf(expectedType, type)) {
trace.report(EXPECTED_PARAMETER_TYPE_MISMATCH.on(valueParameter, expectedType))
}
}
} else {
type = if (isFunctionLiteral(functionDescriptor) || isFunctionExpression(functionDescriptor)) {
val containsErrorType = TypeUtils.contains(expectedType) { it.isError }
if (expectedType == null || containsErrorType) {
trace.report(CANNOT_INFER_PARAMETER_TYPE.on(valueParameter))
}
expectedType ?: TypeUtils.CANNOT_INFER_FUNCTION_PARAM_TYPE
} else {
type = if (isFunctionLiteral(functionDescriptor) || isFunctionExpression(functionDescriptor)) {
val containsErrorType = TypeUtils.contains(expectedType) { it.isError }
if (expectedType == null || containsErrorType) {
trace.report(CANNOT_INFER_PARAMETER_TYPE.on(valueParameter))
}
expectedType ?: TypeUtils.CANNOT_INFER_FUNCTION_PARAM_TYPE
} else {
trace.report(VALUE_PARAMETER_WITH_NO_TYPE_ANNOTATION.on(valueParameter))
ErrorUtils.createErrorType(ErrorTypeKind.MISSED_TYPE_FOR_PARAMETER, valueParameter.nameAsSafeName.toString())
}
trace.report(VALUE_PARAMETER_WITH_NO_TYPE_ANNOTATION.on(valueParameter))
ErrorUtils.createErrorType(ErrorTypeKind.MISSED_TYPE_FOR_PARAMETER, valueParameter.nameAsSafeName.toString())
}
}
if (functionDescriptor !is ConstructorDescriptor || !functionDescriptor.isPrimary) {
val isConstructor = functionDescriptor is ConstructorDescriptor
with(modifiersChecker.withTrace(trace)) {
checkParameterHasNoValOrVar(
valueParameter,
if (isConstructor) VAL_OR_VAR_ON_SECONDARY_CONSTRUCTOR_PARAMETER else VAL_OR_VAR_ON_FUN_PARAMETER
)
}
if (functionDescriptor !is ConstructorDescriptor || !functionDescriptor.isPrimary) {
val isConstructor = functionDescriptor is ConstructorDescriptor
with(modifiersChecker.withTrace(trace)) {
checkParameterHasNoValOrVar(
valueParameter,
if (isConstructor) VAL_OR_VAR_ON_SECONDARY_CONSTRUCTOR_PARAMETER else VAL_OR_VAR_ON_FUN_PARAMETER
)
}
val valueParameterDescriptor = descriptorResolver.resolveValueParameterDescriptor(
parameterScope, functionDescriptor, valueParameter, i, type, trace, Annotations.EMPTY, inferenceSession
)
// Do not report NAME_SHADOWING for lambda destructured parameters as they may be not fully resolved at this time
ExpressionTypingUtils.checkVariableShadowing(parameterScope, trace, valueParameterDescriptor)
parameterScope.addVariableDescriptor(valueParameterDescriptor)
result.add(valueParameterDescriptor)
}
return result
} catch (e: Exception) {
if (e is ControlFlowException) {
throw IllegalStateException("Method should be run under nonCancelableSection", e)
}
throw e
val valueParameterDescriptor = descriptorResolver.resolveValueParameterDescriptor(
parameterScope, functionDescriptor, valueParameter, i, type, trace, Annotations.EMPTY, inferenceSession
)
// Do not report NAME_SHADOWING for lambda destructured parameters as they may be not fully resolved at this time
ExpressionTypingUtils.checkVariableShadowing(parameterScope, trace, valueParameterDescriptor)
parameterScope.addVariableDescriptor(valueParameterDescriptor)
result.add(valueParameterDescriptor)
}
return result
}
private data class ContextReceiverTypeWithLabel(val type: KotlinType, val label: Name?)
}
private fun <T> computeInNonCancelableSection(action: () -> T): T =
ProgressManager.getInstance().computeInNonCancelableSection<T, Exception>(action)
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.types.expressions
import com.google.common.collect.Lists
import com.intellij.openapi.progress.ProgressManager
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.builtins.*
import org.jetbrains.kotlin.config.LanguageFeature
@@ -210,30 +209,23 @@ internal class FunctionsTypingVisitor(facade: ExpressionTypingInternals) : Expre
context: ExpressionTypingContext
): AnonymousFunctionDescriptor {
val functionLiteral = expression.functionLiteral
val annotations = components.annotationResolver.resolveAnnotationsWithArguments(
context.scope,
expression.getAnnotationEntries(),
context.trace
)
return ProgressManager.getInstance().computeInNonCancelableSection<AnonymousFunctionDescriptor, Exception> {
val functionDescriptor = AnonymousFunctionDescriptor(
context.scope.ownerDescriptor,
annotations,
CallableMemberDescriptor.Kind.DECLARATION, functionLiteral.toSourceElement(),
context.expectedType.isSuspendFunctionType()
).let {
facade.components.typeResolutionInterceptor.interceptFunctionLiteralDescriptor(expression, context, it)
}
components.functionDescriptorResolver.initializeFunctionDescriptorAndExplicitReturnType(
context.scope.ownerDescriptor, context.scope, functionLiteral,
functionDescriptor, context.trace, context.expectedType, context.dataFlowInfo, context.inferenceSession
)
for (parameterDescriptor in functionDescriptor.valueParameters) {
ForceResolveUtil.forceResolveAllContents(parameterDescriptor.annotations)
}
BindingContextUtils.recordFunctionDeclarationToDescriptor(context.trace, functionLiteral, functionDescriptor)
functionDescriptor
val functionDescriptor = AnonymousFunctionDescriptor(
context.scope.ownerDescriptor,
components.annotationResolver.resolveAnnotationsWithArguments(context.scope, expression.getAnnotationEntries(), context.trace),
CallableMemberDescriptor.Kind.DECLARATION, functionLiteral.toSourceElement(),
context.expectedType.isSuspendFunctionType()
).let {
facade.components.typeResolutionInterceptor.interceptFunctionLiteralDescriptor(expression, context, it)
}
components.functionDescriptorResolver.initializeFunctionDescriptorAndExplicitReturnType(
context.scope.ownerDescriptor, context.scope, functionLiteral,
functionDescriptor, context.trace, context.expectedType, context.dataFlowInfo, context.inferenceSession
)
for (parameterDescriptor in functionDescriptor.valueParameters) {
ForceResolveUtil.forceResolveAllContents(parameterDescriptor.annotations)
}
BindingContextUtils.recordFunctionDeclarationToDescriptor(context.trace, functionLiteral, functionDescriptor)
return functionDescriptor
}
private fun KotlinType.isBuiltinFunctionalType() =