From 5a11450d772f1b42f07845c7c6a3ac1755d8cfeb Mon Sep 17 00:00:00 2001 From: Victor Petukhov Date: Tue, 11 May 2021 17:14:05 +0300 Subject: [PATCH] Split stub types into stub type for subtyping and for builder inference and use them in the proper way --- .../kotlin/fir/types/ConeInferenceContext.kt | 5 +- .../kotlin/resolve/jvm/RuntimeAssertions.kt | 2 +- .../inference/BuilderInferenceSession.kt | 8 +- .../calls/inference/CoroutineInferenceUtil.kt | 2 +- .../tower/KotlinResolutionCallbacksImpl.kt | 2 +- .../tower/KotlinToResolvedCallTransformer.kt | 2 +- .../calls/tower/ResolvedAtomCompleter.kt | 2 +- .../types/expressions/DataFlowAnalyzer.java | 2 +- .../DoubleColonExpressionResolver.kt | 6 +- .../model/NewConstraintSystemImpl.kt | 2 +- .../ClassicTypeSystemContextForCS.kt | 7 +- .../calls/components/ExternalComponents.kt | 4 +- .../kotlin/types/model/TypeSystemContext.kt | 2 +- .../kotlin/renderer/DescriptorRendererImpl.kt | 4 +- .../org/jetbrains/kotlin/types/StubType.kt | 75 ------------------- .../org/jetbrains/kotlin/types/StubTypes.kt | 67 +++++++++++++++++ .../org/jetbrains/kotlin/types/TypeUtils.kt | 2 +- .../types/checker/ClassicTypeSystemContext.kt | 4 +- 18 files changed, 97 insertions(+), 101 deletions(-) delete mode 100644 core/descriptors/src/org/jetbrains/kotlin/types/StubType.kt create mode 100644 core/descriptors/src/org/jetbrains/kotlin/types/StubTypes.kt diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt index c2e2906a39b..43bf6cab1b9 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt @@ -251,13 +251,14 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo ) } - override fun createStubType(typeVariable: TypeVariableMarker): StubTypeMarker { + override fun createStubTypeForBuilderInference(typeVariable: TypeVariableMarker): StubTypeMarker { require(typeVariable is ConeTypeVariable) { "$typeVariable should subtype of ${ConeTypeVariable::class.qualifiedName}" } return ConeStubType(typeVariable, ConeNullability.create(typeVariable.defaultType().isMarkedNullable())) } // TODO - override fun createStubTypeForTypeVariablesInSubtyping(typeVariable: TypeVariableMarker) = createStubType(typeVariable) + override fun createStubTypeForTypeVariablesInSubtyping(typeVariable: TypeVariableMarker) = + createStubTypeForBuilderInference(typeVariable) override fun KotlinTypeMarker.removeAnnotations(): KotlinTypeMarker { require(this is ConeKotlinType) diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/RuntimeAssertions.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/RuntimeAssertions.kt index 893378de58a..838e4436df9 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/RuntimeAssertions.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/RuntimeAssertions.kt @@ -103,7 +103,7 @@ object RuntimeAssertionsTypeChecker : AdditionalTypeChecker { expressionTypeWithSmartCast: KotlinType, c: ResolutionContext<*> ) { - if (TypeUtils.noExpectedType(c.expectedType) || c.expectedType is StubType) return + if (TypeUtils.noExpectedType(c.expectedType) || c.expectedType is StubTypeForBuilderInference) return val assertionInfo = RuntimeAssertionInfo.create( c.expectedType, diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/BuilderInferenceSession.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/BuilderInferenceSession.kt index 43262a17fd2..555c90c96f9 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/BuilderInferenceSession.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/BuilderInferenceSession.kt @@ -45,7 +45,7 @@ class BuilderInferenceSession( callComponents: KotlinCallComponents, builtIns: KotlinBuiltIns, private val topLevelCallContext: BasicCallResolutionContext, - private val stubsForPostponedVariables: Map, + private val stubsForPostponedVariables: Map, private val trace: BindingTrace, private val kotlinToResolvedCallTransformer: KotlinToResolvedCallTransformer, private val expressionTypingServices: ExpressionTypingServices, @@ -117,7 +117,7 @@ class BuilderInferenceSession( private fun KotlinType.containsStubType(): Boolean { return this.contains { - it is StubType + it is StubTypeForBuilderInference } } @@ -134,8 +134,8 @@ class BuilderInferenceSession( } private fun anyReceiverContainStubType(descriptor: CallableDescriptor): Boolean { - return descriptor.dispatchReceiverParameter?.type?.contains { it is StubType } == true || - descriptor.extensionReceiverParameter?.type?.contains { it is StubType } == true + return descriptor.dispatchReceiverParameter?.type?.contains { it is StubTypeForBuilderInference } == true || + descriptor.extensionReceiverParameter?.type?.contains { it is StubTypeForBuilderInference } == true } private fun isTopLevelBuilderInferenceCall() = findParentBuildInferenceSession() == null diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceUtil.kt index 476f0acd3b3..60bce8d0a9b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceUtil.kt @@ -296,7 +296,7 @@ class CoroutineInferenceSupport( } } -private fun KotlinType.containsTypeTemplate() = contains { it is TypeTemplate || it is StubType } +private fun KotlinType.containsTypeTemplate() = contains { it is TypeTemplate || it is StubTypeForBuilderInference } fun isApplicableCallForBuilderInference(descriptor: CallableDescriptor, languageVersionSettings: LanguageVersionSettings): Boolean { if (!languageVersionSettings.supportsFeature(LanguageFeature.ExperimentalBuilderInference)) { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt index 1860f079635..9536deec86f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt @@ -90,7 +90,7 @@ class KotlinResolutionCallbacksImpl( parameters: List, expectedReturnType: UnwrappedType?, annotations: Annotations, - stubsForPostponedVariables: Map, + stubsForPostponedVariables: Map, ): ReturnArgumentsAnalysisResult { val psiCallArgument = lambdaArgument.psiCallArgument as PSIFunctionKotlinCallArgument val outerCallContext = psiCallArgument.outerCallContext diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt index 1bd595d3a11..b59600abc03 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt @@ -989,7 +989,7 @@ private fun CallableMemberDescriptor.isNotSimpleCall(): Boolean = it is NewCapturedType || it.constructor is IntegerLiteralTypeConstructor || it is DefinitelyNotNullType || - it is StubType + it is StubTypeForBuilderInference } } ?: false) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt index 453e3783039..0d9f5e8dfc2 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt @@ -415,7 +415,7 @@ class ResolvedAtomCompleter( val rawExtensionReceiver = callableCandidate.extensionReceiver - if (rawExtensionReceiver != null && rawExtensionReceiver.receiver.receiverValue.type.contains { it is StubType }) { + if (rawExtensionReceiver != null && rawExtensionReceiver.receiver.receiverValue.type.contains { it is StubTypeForBuilderInference }) { topLevelTrace.reportDiagnosticOnce(Errors.TYPE_INFERENCE_POSTPONED_VARIABLE_IN_RECEIVER_TYPE.on(callableReferenceExpression)) return } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DataFlowAnalyzer.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DataFlowAnalyzer.java index 526f232c916..6e22a61515b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DataFlowAnalyzer.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DataFlowAnalyzer.java @@ -286,7 +286,7 @@ public class DataFlowAnalyzer { @NotNull Ref hasError, boolean reportErrorForTypeMismatch ) { - if (!noExpectedType(c.expectedType) && TypeUtilsKt.contains(expressionType, (type) -> type instanceof StubType)) { + if (!noExpectedType(c.expectedType) && TypeUtilsKt.contains(expressionType, (type) -> type instanceof StubTypeForBuilderInference)) { if (c.inferenceSession instanceof BuilderInferenceSession) { ((BuilderInferenceSession) c.inferenceSession).addExpectedTypeConstraint(expression, expressionType, c.expectedType); } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DoubleColonExpressionResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DoubleColonExpressionResolver.kt index 0d8222f74e8..764e799d441 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DoubleColonExpressionResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DoubleColonExpressionResolver.kt @@ -114,7 +114,7 @@ class DoubleColonExpressionResolver( } else { val result = resolveDoubleColonLHS(expression, c) - if (c.inferenceSession is BuilderInferenceSession && result?.type?.contains { it is StubType } == true) { + if (c.inferenceSession is BuilderInferenceSession && result?.type?.contains { it is StubTypeForBuilderInference } == true) { c.inferenceSession.addOldCallableReferenceCalls(expression) } @@ -548,7 +548,7 @@ class DoubleColonExpressionResolver( val result = getCallableReferenceType(expression, lhs, resolutionResults, c) val doesSomeExtensionReceiverContainsStubType = resolutionResults != null && resolutionResults.resultingCalls.any { resolvedCall -> - resolvedCall.extensionReceiver?.type?.contains { it is StubType } == true + resolvedCall.extensionReceiver?.type?.contains { it is StubTypeForBuilderInference } == true } if (doesSomeExtensionReceiverContainsStubType) { @@ -558,7 +558,7 @@ class DoubleColonExpressionResolver( val dataFlowInfo = (lhs as? DoubleColonLHS.Expression)?.dataFlowInfo ?: c.dataFlowInfo - if (c.inferenceSession is BuilderInferenceSession && result?.contains { it is StubType } == true) { + if (c.inferenceSession is BuilderInferenceSession && result?.contains { it is StubTypeForBuilderInference } == true) { c.inferenceSession.addOldCallableReferenceCalls(expression) } diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt index 9daa033b5d2..c6c700b502d 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt @@ -497,7 +497,7 @@ class NewConstraintSystemImpl( override fun bindingStubsForPostponedVariables(): Map { checkState(State.BUILDING, State.COMPLETION) // TODO: SUB - return storage.postponedTypeVariables.associateWith { createStubType(it) } + return storage.postponedTypeVariables.associateWith { createStubTypeForBuilderInference(it) } } override fun currentStorage(): ConstraintStorage { diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ClassicTypeSystemContextForCS.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ClassicTypeSystemContextForCS.kt index 21699b7a18a..d1aeac8d9c7 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ClassicTypeSystemContextForCS.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ClassicTypeSystemContextForCS.kt @@ -73,8 +73,11 @@ class ClassicTypeSystemContextForCS(override val builtIns: KotlinBuiltIns) : Typ } } - override fun createStubType(typeVariable: TypeVariableMarker): StubTypeMarker { - return StubType(typeVariable.freshTypeConstructor() as TypeConstructor, typeVariable.defaultType().isMarkedNullable()) + override fun createStubTypeForBuilderInference(typeVariable: TypeVariableMarker): StubTypeMarker { + return StubTypeForBuilderInference( + typeVariable.freshTypeConstructor() as TypeConstructor, + typeVariable.defaultType().isMarkedNullable() + ) } override fun createStubTypeForTypeVariablesInSubtyping(typeVariable: TypeVariableMarker): StubTypeMarker { diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ExternalComponents.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ExternalComponents.kt index 776588e2f1c..6c6a607c37d 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ExternalComponents.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ExternalComponents.kt @@ -18,7 +18,7 @@ import org.jetbrains.kotlin.resolve.calls.results.SimpleConstraintSystem import org.jetbrains.kotlin.resolve.calls.tower.ImplicitScopeTower import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstant import org.jetbrains.kotlin.types.KotlinType -import org.jetbrains.kotlin.types.StubType +import org.jetbrains.kotlin.types.StubTypeForBuilderInference import org.jetbrains.kotlin.types.UnwrappedType // stateless component @@ -73,7 +73,7 @@ interface KotlinResolutionCallbacks { parameters: List, expectedReturnType: UnwrappedType?, // null means, that return type is not proper i.e. it depends on some type variables annotations: Annotations, - stubsForPostponedVariables: Map, + stubsForPostponedVariables: Map, ): ReturnArgumentsAnalysisResult fun bindStubResolvedCallForCandidate(candidate: ResolvedCallAtom) diff --git a/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt b/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt index 9ec2df7c780..d21a33b55d0 100644 --- a/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt +++ b/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt @@ -168,7 +168,7 @@ interface TypeSystemInferenceExtensionContext : TypeSystemContext, TypeSystemBui captureStatus: CaptureStatus ): CapturedTypeMarker - fun createStubType(typeVariable: TypeVariableMarker): StubTypeMarker + fun createStubTypeForBuilderInference(typeVariable: TypeVariableMarker): StubTypeMarker fun createStubTypeForTypeVariablesInSubtyping(typeVariable: TypeVariableMarker): StubTypeMarker fun KotlinTypeMarker.removeAnnotations(): KotlinTypeMarker diff --git a/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt b/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt index 32ed0829084..f4fab61f076 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt @@ -249,7 +249,7 @@ internal class DescriptorRendererImpl( } append(renderTypeArguments(type.arguments)) } - type is StubType -> append(type.originalTypeVariable.toString()) + type is StubTypeForBuilderInference -> append(type.originalTypeVariable.toString()) else -> renderTypeConstructorAndArguments(type) } @@ -290,7 +290,7 @@ internal class DescriptorRendererImpl( is TypeParameterDescriptor, is ClassDescriptor, is TypeAliasDescriptor -> renderClassifierName(cd) null -> { if (typeConstructor is IntersectionTypeConstructor) { - typeConstructor.makeDebugNameForIntersectionType { if (it is StubType) it.originalTypeVariable else it } + typeConstructor.makeDebugNameForIntersectionType { if (it is StubTypeForBuilderInference) it.originalTypeVariable else it } } else typeConstructor.toString() } else -> error("Unexpected classifier: " + cd::class.java) diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/StubType.kt b/core/descriptors/src/org/jetbrains/kotlin/types/StubType.kt deleted file mode 100644 index 1f9abad468b..00000000000 --- a/core/descriptors/src/org/jetbrains/kotlin/types/StubType.kt +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright 2010-2018 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.types - -import org.jetbrains.kotlin.descriptors.annotations.Annotations -import org.jetbrains.kotlin.resolve.scopes.MemberScope -import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner -import org.jetbrains.kotlin.types.model.StubTypeMarker -import org.jetbrains.kotlin.types.refinement.TypeRefinement - -// This type is used as a stub for postponed type variables, which are important for coroutine inference -class StubType( - originalTypeVariable: TypeConstructor, - isMarkedNullable: Boolean, - constructor: TypeConstructor = ErrorUtils.createErrorTypeConstructor("Constructor for non fixed type: $originalTypeVariable"), - memberScope: MemberScope = ErrorUtils.createErrorScope("Scope for non fixed type: $originalTypeVariable") -) : AbstractStubType(originalTypeVariable, isMarkedNullable, constructor, memberScope), StubTypeMarker { - override fun materialize(newNullability: Boolean): AbstractStubType { - return StubType(originalTypeVariable, newNullability, constructor, memberScope) - } -} - -class StubTypeForTypeVariablesInSubtyping( - originalTypeVariable: TypeConstructor, - isMarkedNullable: Boolean, - constructor: TypeConstructor = ErrorUtils.createErrorTypeConstructor("Constructor for non fixed type: $originalTypeVariable"), - memberScope: MemberScope = ErrorUtils.createErrorScope("Scope for non fixed type: $originalTypeVariable") -) : AbstractStubType(originalTypeVariable, isMarkedNullable, constructor, memberScope), StubTypeMarker { - override fun materialize(newNullability: Boolean): AbstractStubType { - return StubTypeForTypeVariablesInSubtyping(originalTypeVariable, newNullability, constructor, memberScope) - } -} - -// This type is used as a replacement of type variables for provideDelegate resolve -class StubTypeForProvideDelegateReceiver( - originalTypeVariable: TypeConstructor, - isMarkedNullable: Boolean, - constructor: TypeConstructor = ErrorUtils.createErrorTypeConstructor("Constructor for non fixed type: $originalTypeVariable"), - memberScope: MemberScope = ErrorUtils.createErrorScope("Scope for non fixed type: $originalTypeVariable") -) : AbstractStubType(originalTypeVariable, isMarkedNullable, constructor, memberScope) { - override fun materialize(newNullability: Boolean): StubTypeForProvideDelegateReceiver { - return StubTypeForProvideDelegateReceiver(originalTypeVariable, newNullability, constructor, memberScope) - } -} - -abstract class AbstractStubType( - val originalTypeVariable: TypeConstructor, - override val isMarkedNullable: Boolean, - override val constructor: TypeConstructor, - override val memberScope: MemberScope -) : SimpleType() { - override val arguments: List - get() = emptyList() - - override val annotations: Annotations - get() = Annotations.EMPTY - - override fun replaceAnnotations(newAnnotations: Annotations): SimpleType = this - - override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType { - return if (newNullability == isMarkedNullable) this else materialize(newNullability) - } - - override fun toString(): String { - return "NonFixed: $originalTypeVariable${if (isMarkedNullable) "?" else ""}" - } - - @TypeRefinement - override fun refine(kotlinTypeRefiner: KotlinTypeRefiner) = this - - abstract fun materialize(newNullability: Boolean): AbstractStubType -} \ No newline at end of file diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/StubTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/StubTypes.kt new file mode 100644 index 00000000000..d0ffecb8820 --- /dev/null +++ b/core/descriptors/src/org/jetbrains/kotlin/types/StubTypes.kt @@ -0,0 +1,67 @@ +/* + * Copyright 2010-2018 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.types + +import org.jetbrains.kotlin.descriptors.annotations.Annotations +import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner +import org.jetbrains.kotlin.types.model.StubTypeMarker +import org.jetbrains.kotlin.types.refinement.TypeRefinement + +class StubTypeForBuilderInference( + originalTypeVariable: TypeConstructor, + isMarkedNullable: Boolean, +) : AbstractStubType(originalTypeVariable, isMarkedNullable), StubTypeMarker { + override fun materialize(newNullability: Boolean): AbstractStubType = + StubTypeForBuilderInference(originalTypeVariable, newNullability) + + override fun toString(): String { + // BI means builder inference + return "Stub (BI): $originalTypeVariable${if (isMarkedNullable) "?" else ""}" + } +} + +class StubTypeForTypeVariablesInSubtyping(originalTypeVariable: TypeConstructor, isMarkedNullable: Boolean) : + AbstractStubType(originalTypeVariable, isMarkedNullable), StubTypeMarker { + override fun materialize(newNullability: Boolean): AbstractStubType = + StubTypeForTypeVariablesInSubtyping(originalTypeVariable, newNullability) + + override fun toString(): String { + return "Stub (subtyping): $originalTypeVariable${if (isMarkedNullable) "?" else ""}" + } +} + +// This type is used as a replacement of type variables for provideDelegate resolve +class StubTypeForProvideDelegateReceiver(originalTypeVariable: TypeConstructor, isMarkedNullable: Boolean) : + AbstractStubType(originalTypeVariable, isMarkedNullable) { + override fun materialize(newNullability: Boolean): StubTypeForProvideDelegateReceiver = + StubTypeForProvideDelegateReceiver(originalTypeVariable, newNullability) + + override fun toString(): String { + return "Stub (delegation): $originalTypeVariable${if (isMarkedNullable) "?" else ""}" + } +} + +abstract class AbstractStubType(val originalTypeVariable: TypeConstructor, override val isMarkedNullable: Boolean) : SimpleType() { + override val memberScope = ErrorUtils.createErrorScope("Scope for stub type: $originalTypeVariable") + override val constructor = ErrorUtils.createErrorTypeConstructor("Constructor for stub type: $originalTypeVariable") + + override val arguments: List + get() = emptyList() + + override val annotations: Annotations + get() = Annotations.EMPTY + + override fun replaceAnnotations(newAnnotations: Annotations): SimpleType = this + + override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType { + return if (newNullability == isMarkedNullable) this else materialize(newNullability) + } + + @TypeRefinement + override fun refine(kotlinTypeRefiner: KotlinTypeRefiner) = this + + abstract fun materialize(newNullability: Boolean): AbstractStubType +} \ No newline at end of file diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt index 2f0ba6066cb..954ea780b61 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt @@ -375,4 +375,4 @@ private fun NewCapturedType.unCaptureTopLevelType(): UnwrappedType { return constructor.projection.type.unwrap() } -fun KotlinType.shouldBeUpdated() = contains { it is StubType || it.constructor is TypeVariableTypeConstructorMarker || it.isError } +fun KotlinType.shouldBeUpdated() = contains { it is StubTypeForBuilderInference || it.constructor is TypeVariableTypeConstructorMarker || it.isError } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt b/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt index 052fb12c557..3357979b857 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt @@ -81,7 +81,7 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy override fun SimpleTypeMarker.isStubType(): Boolean { require(this is SimpleType, this::errorMessage) - return this is StubType || this is StubTypeForProvideDelegateReceiver + return this is StubTypeForBuilderInference || this is StubTypeForProvideDelegateReceiver } override fun SimpleTypeMarker.isStubTypeForVariableInSubtyping(): Boolean { @@ -544,7 +544,7 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy errorSupportedOnlyInTypeInference() } - override fun createStubType(typeVariable: TypeVariableMarker): StubTypeMarker { + override fun createStubTypeForBuilderInference(typeVariable: TypeVariableMarker): StubTypeMarker { errorSupportedOnlyInTypeInference() }