Skip to content

Commit

Permalink
Support serialization of built-in ranges as error types
Browse files Browse the repository at this point in the history
This will be needed to move ranges and progressions out of builtins.
Currently they're only used in signatures of "rangeTo" functions on
primitives
  • Loading branch information
udalov committed Apr 26, 2017
1 parent f648f02 commit e2c62cb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,28 @@ package org.jetbrains.kotlin.serialization.builtins

import org.jetbrains.kotlin.builtins.BuiltInSerializerProtocol
import org.jetbrains.kotlin.serialization.KotlinSerializerExtensionBase
import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.UnresolvedType

class BuiltInsSerializerExtension : KotlinSerializerExtensionBase(BuiltInSerializerProtocol) {
private val shortNameToFullName = mapOf(
"IntRange" to "kotlin/ranges/IntRange",
"LongRange" to "kotlin/ranges/LongRange",
"CharRange" to "kotlin/ranges/CharRange"
)

override fun shouldUseTypeTable(): Boolean = true

override fun serializeErrorType(type: KotlinType, builder: ProtoBuf.Type.Builder) {
val unwrapped = type.unwrap()
if (unwrapped !is UnresolvedType) {
throw UnsupportedOperationException("Error types which are not UnresolvedType instances are not supported here: $unwrapped")
}

val fullName = shortNameToFullName[unwrapped.presentableName]
?: throw UnsupportedOperationException("Unsupported unresolved type: $unwrapped")

builder.className = stringTable.getStringIndex(fullName)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class TypeResolver(
val arguments = resolveTypeProjections(
c, ErrorUtils.createErrorType("No type").constructor, qualifierResolutionResult.allProjections
)
result = type(ErrorUtils.createErrorTypeWithArguments(type.getDebugText(), arguments))
result = type(ErrorUtils.createUnresolvedType(type.getDebugText(), arguments))
return
}

Expand Down
13 changes: 12 additions & 1 deletion core/descriptors/src/org/jetbrains/kotlin/types/ErrorType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.resolve.scopes.MemberScope

class ErrorType @JvmOverloads internal constructor(
open class ErrorType @JvmOverloads internal constructor(
override val constructor: TypeConstructor,
override val memberScope: MemberScope,
override val arguments: List<TypeProjection> = emptyList(),
Expand All @@ -36,3 +36,14 @@ class ErrorType @JvmOverloads internal constructor(
override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType =
ErrorType(constructor, memberScope, arguments, newNullability)
}

class UnresolvedType(
val presentableName: String,
constructor: TypeConstructor,
memberScope: MemberScope,
arguments: List<TypeProjection>,
isMarkedNullable: Boolean
) : ErrorType(constructor, memberScope, arguments, isMarkedNullable) {
override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType =
UnresolvedType(presentableName, constructor, memberScope, arguments, newNullability)
}
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,12 @@ public static SimpleType createErrorTypeWithArguments(@NotNull String debugMessa
return new ErrorType(createErrorTypeConstructor(debugMessage), createErrorScope(debugMessage), arguments, false);
}

@NotNull
public static SimpleType createUnresolvedType(@NotNull String presentableName, @NotNull List<TypeProjection> arguments) {
return new UnresolvedType(presentableName, createErrorTypeConstructor(presentableName), createErrorScope(presentableName),
arguments, false);
}

@NotNull
public static TypeConstructor createErrorTypeConstructor(@NotNull String debugMessage) {
return createErrorTypeConstructorWithCustomDebugName("[ERROR : " + debugMessage + "]", ERROR_CLASS);
Expand Down

0 comments on commit e2c62cb

Please sign in to comment.