Fix race in IDE: inject proper storage manager for type parameters

With NO_LOCKS strategy we can easily end up in a situation when
 constraint system for a generic call is built incorrectly,
 producing flaky errors (or don't produce errors at all)

 Now proper storage manager is injected for all cases except:
 - IR
 - Codegen
 - Serialization plugin
 - Fake local objects

 Most likely, NO_LOCKS strategy for these cases is fine as at that point
 the compiler works in one thread

 #KT-34786 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2019-11-25 17:12:28 +03:00
parent 399667a434
commit eb73650209
21 changed files with 103 additions and 40 deletions
@@ -21,8 +21,6 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl
import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.DescriptorUtils
import java.util.*
open class AbstractAccessorForFunctionDescriptor(
containingDeclaration: DeclarationDescriptor,
@@ -34,7 +32,9 @@ open class AbstractAccessorForFunctionDescriptor(
val copy = TypeParameterDescriptorImpl.createForFurtherModification(
this, it.annotations, it.isReified,
it.variance, it.name,
it.index, SourceElement.NO_SOURCE)
it.index, SourceElement.NO_SOURCE,
it.storageManager
)
for (upperBound in it.upperBounds) {
copy.addUpperBound(upperBound)
}
@@ -76,7 +76,8 @@ fun CallableMemberDescriptor.createTypeParameterWithNewName(
descriptor.variance,
Name.identifier(newName),
descriptor.index,
descriptor.source
descriptor.source,
descriptor.storageManager
)
descriptor.upperBounds.forEach {
newDescriptor.addUpperBound(it)
@@ -51,7 +51,8 @@ public class JavaResolverUtils {
typeParameter.getVariance(),
typeParameter.getName(),
typeParameter.getIndex(),
SourceElement.NO_SOURCE
SourceElement.NO_SOURCE,
typeParameter.getStorageManager()
)
);
}
@@ -472,7 +472,8 @@ public class DescriptorResolver {
}
return null;
},
supertypeLoopsResolver
supertypeLoopsResolver,
storageManager
);
trace.record(BindingContext.TYPE_PARAMETER, typeParameter, typeParameterDescriptor);
return typeParameterDescriptor;
@@ -36,7 +36,7 @@ import org.jetbrains.kotlin.types.expressions.OperatorConventions
import org.jetbrains.kotlin.utils.Printer
import java.util.*
class DynamicCallableDescriptors(storageManager: StorageManager, builtIns: KotlinBuiltIns) {
class DynamicCallableDescriptors(private val storageManager: StorageManager, builtIns: KotlinBuiltIns) {
val dynamicType by storageManager.createLazyValue {
createDynamicType(builtIns)
@@ -150,7 +150,8 @@ class DynamicCallableDescriptors(storageManager: StorageManager, builtIns: Kotli
false,
Variance.INVARIANT,
Name.identifier("T$index"),
index
index,
storageManager
)
}
@@ -55,6 +55,7 @@ import org.jetbrains.kotlin.resolve.calls.tasks.TracingStrategy;
import org.jetbrains.kotlin.resolve.calls.util.CallMaker;
import org.jetbrains.kotlin.resolve.descriptorUtil.AnnotationsForResolveKt;
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue;
import org.jetbrains.kotlin.storage.StorageManager;
import org.jetbrains.kotlin.types.*;
import org.jetbrains.kotlin.types.typeUtil.TypeUtilsKt;
@@ -97,15 +98,18 @@ public class ControlStructureTypingUtils {
private final CallResolver callResolver;
private final DataFlowAnalyzer dataFlowAnalyzer;
private final ModuleDescriptor moduleDescriptor;
private final StorageManager storageManager;
public ControlStructureTypingUtils(
@NotNull CallResolver callResolver,
@NotNull DataFlowAnalyzer dataFlowAnalyzer,
@NotNull ModuleDescriptor moduleDescriptor
@NotNull ModuleDescriptor moduleDescriptor,
@NotNull StorageManager storageManager
) {
this.callResolver = callResolver;
this.dataFlowAnalyzer = dataFlowAnalyzer;
this.moduleDescriptor = moduleDescriptor;
this.storageManager = storageManager;
}
/*package*/ ResolvedCall<FunctionDescriptor> resolveSpecialConstructionAsCall(
@@ -202,7 +206,7 @@ public class ControlStructureTypingUtils {
TypeParameterDescriptor typeParameter = TypeParameterDescriptorImpl.createWithDefaultBound(
function, Annotations.Companion.getEMPTY(), false, Variance.INVARIANT,
construct.getSpecialTypeParameterName(), 0);
construct.getSpecialTypeParameterName(), 0, storageManager);
KotlinType type = typeParameter.getDefaultType();
KotlinType nullableType = TypeUtils.makeNullable(type);
@@ -31,6 +31,7 @@ import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.types.*
class IrBuiltIns(
@@ -91,7 +92,7 @@ class IrBuiltIns(
SourceElement.NO_SOURCE
).apply {
typeParameterDescriptor = TypeParameterDescriptorImpl.createWithDefaultBound(
this, Annotations.EMPTY, true, Variance.INVARIANT, Name.identifier("T0"), 0
this, Annotations.EMPTY, true, Variance.INVARIANT, Name.identifier("T0"), 0, LockBasedStorageManager.NO_LOCKS
)
valueParameterDescriptor = ValueParameterDescriptorImpl(
@@ -153,7 +154,8 @@ class IrBuiltIns(
SourceElement.NO_SOURCE
).apply {
typeParameterDescriptor = TypeParameterDescriptorImpl.createForFurtherModification(
this, Annotations.EMPTY, false, Variance.INVARIANT, Name.identifier("T0"), 0, SourceElement.NO_SOURCE
this, Annotations.EMPTY, false, Variance.INVARIANT, Name.identifier("T0"),
0, SourceElement.NO_SOURCE, LockBasedStorageManager.NO_LOCKS
).apply {
addUpperBound(any)
setInitialized()
@@ -288,6 +288,7 @@ open class WrappedTypeParameterDescriptor(
override fun getDefaultType() = _defaultType
override fun getStorageManager() = LockBasedStorageManager.NO_LOCKS
override fun getContainingDeclaration() = (owner.parent as IrDeclaration).descriptor
@@ -37,6 +37,7 @@ import org.jetbrains.kotlin.resolve.TypeResolver;
import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil;
import org.jetbrains.kotlin.resolve.scopes.*;
import org.jetbrains.kotlin.resolve.scopes.utils.ScopeUtilsKt;
import org.jetbrains.kotlin.storage.LockBasedStorageManager;
import org.jetbrains.kotlin.test.ConfigurationKind;
import org.jetbrains.kotlin.test.DummyTraces;
import org.jetbrains.kotlin.test.KotlinTestWithEnvironment;
@@ -77,7 +78,8 @@ public class TypeUnifierTest extends KotlinTestWithEnvironment {
private TypeParameterDescriptor createTypeVariable(String name) {
return TypeParameterDescriptorImpl.createWithDefaultBound(
module, Annotations.Companion.getEMPTY(), false, Variance.INVARIANT, Name.identifier(name), 0
module, Annotations.Companion.getEMPTY(), false, Variance.INVARIANT,
Name.identifier(name), 0, LockBasedStorageManager.NO_LOCKS
);
}