Split stub types into stub type for subtyping and for builder inference and use them in the proper way

This commit is contained in:
Victor Petukhov
2021-05-11 17:14:05 +03:00
parent 703a353d2e
commit 5a11450d77
18 changed files with 97 additions and 101 deletions
@@ -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)
@@ -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<TypeProjection>
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
}
@@ -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<TypeProjection>
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
}
@@ -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 }
@@ -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()
}