Throw exceptions explicitly in protoTypeTableUtil
Previously, if both type and typeId messages were missing (for example, ProtoBuf.Function.returnType/returnTypeId) because of some bug, the behavior was unpredictable because a type with id 0 from the type table would be returned, which could be a completely irrelevant type. This is an incorrect situation and we should report a diagnostic instead. Temporarily throw an exception instead, since this only affects how the compiler works on bad metadata
This commit is contained in:
+58
-59
@@ -18,87 +18,86 @@ package org.jetbrains.kotlin.metadata.deserialization
|
||||
|
||||
import org.jetbrains.kotlin.metadata.ProtoBuf
|
||||
|
||||
fun ProtoBuf.Class.supertypes(typeTable: TypeTable): List<ProtoBuf.Type> {
|
||||
return supertypeList.takeIf(Collection<*>::isNotEmpty) ?: supertypeIdList.map { typeTable[it] }
|
||||
// TODO: return null and report a diagnostic instead of throwing exceptions
|
||||
|
||||
fun ProtoBuf.Class.supertypes(typeTable: TypeTable): List<ProtoBuf.Type> =
|
||||
supertypeList.takeIf(Collection<*>::isNotEmpty) ?: supertypeIdList.map { typeTable[it] }
|
||||
|
||||
fun ProtoBuf.Type.Argument.type(typeTable: TypeTable): ProtoBuf.Type? = when {
|
||||
hasType() -> type
|
||||
hasTypeId() -> typeTable[typeId]
|
||||
else -> null
|
||||
}
|
||||
|
||||
fun ProtoBuf.Type.Argument.type(typeTable: TypeTable): ProtoBuf.Type? {
|
||||
return when {
|
||||
hasType() -> type
|
||||
hasTypeId() -> typeTable[typeId]
|
||||
else -> null
|
||||
}
|
||||
fun ProtoBuf.Type.flexibleUpperBound(typeTable: TypeTable): ProtoBuf.Type? = when {
|
||||
hasFlexibleUpperBound() -> flexibleUpperBound
|
||||
hasFlexibleUpperBoundId() -> typeTable[flexibleUpperBoundId]
|
||||
else -> null
|
||||
}
|
||||
|
||||
fun ProtoBuf.Type.flexibleUpperBound(typeTable: TypeTable): ProtoBuf.Type? {
|
||||
return when {
|
||||
hasFlexibleUpperBound() -> flexibleUpperBound
|
||||
hasFlexibleUpperBoundId() -> typeTable[flexibleUpperBoundId]
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
fun ProtoBuf.TypeParameter.upperBounds(typeTable: TypeTable): List<ProtoBuf.Type> =
|
||||
upperBoundList.takeIf(Collection<*>::isNotEmpty) ?: upperBoundIdList.map { typeTable[it] }
|
||||
|
||||
fun ProtoBuf.TypeParameter.upperBounds(typeTable: TypeTable): List<ProtoBuf.Type> {
|
||||
return upperBoundList.takeIf(Collection<*>::isNotEmpty) ?: upperBoundIdList.map { typeTable[it] }
|
||||
}
|
||||
|
||||
fun ProtoBuf.Function.returnType(typeTable: TypeTable): ProtoBuf.Type {
|
||||
return if (hasReturnType()) returnType else typeTable[returnTypeId]
|
||||
fun ProtoBuf.Function.returnType(typeTable: TypeTable): ProtoBuf.Type = when {
|
||||
hasReturnType() -> returnType
|
||||
hasReturnTypeId() -> typeTable[returnTypeId]
|
||||
else -> error("No returnType in ProtoBuf.Function")
|
||||
}
|
||||
|
||||
fun ProtoBuf.Function.hasReceiver(): Boolean = hasReceiverType() || hasReceiverTypeId()
|
||||
|
||||
fun ProtoBuf.Function.receiverType(typeTable: TypeTable): ProtoBuf.Type? {
|
||||
return when {
|
||||
hasReceiverType() -> receiverType
|
||||
hasReceiverTypeId() -> typeTable[receiverTypeId]
|
||||
else -> null
|
||||
}
|
||||
fun ProtoBuf.Function.receiverType(typeTable: TypeTable): ProtoBuf.Type? = when {
|
||||
hasReceiverType() -> receiverType
|
||||
hasReceiverTypeId() -> typeTable[receiverTypeId]
|
||||
else -> null
|
||||
}
|
||||
|
||||
fun ProtoBuf.Property.returnType(typeTable: TypeTable): ProtoBuf.Type {
|
||||
return if (hasReturnType()) returnType else typeTable[returnTypeId]
|
||||
fun ProtoBuf.Property.returnType(typeTable: TypeTable): ProtoBuf.Type = when {
|
||||
hasReturnType() -> returnType
|
||||
hasReturnTypeId() -> typeTable[returnTypeId]
|
||||
else -> error("No returnType in ProtoBuf.Property")
|
||||
}
|
||||
|
||||
fun ProtoBuf.Property.hasReceiver(): Boolean = hasReceiverType() || hasReceiverTypeId()
|
||||
|
||||
fun ProtoBuf.Property.receiverType(typeTable: TypeTable): ProtoBuf.Type? {
|
||||
return when {
|
||||
hasReceiverType() -> receiverType
|
||||
hasReceiverTypeId() -> typeTable[receiverTypeId]
|
||||
else -> null
|
||||
}
|
||||
fun ProtoBuf.Property.receiverType(typeTable: TypeTable): ProtoBuf.Type? = when {
|
||||
hasReceiverType() -> receiverType
|
||||
hasReceiverTypeId() -> typeTable[receiverTypeId]
|
||||
else -> null
|
||||
}
|
||||
|
||||
fun ProtoBuf.ValueParameter.type(typeTable: TypeTable): ProtoBuf.Type {
|
||||
return if (hasType()) type else typeTable[typeId]
|
||||
fun ProtoBuf.ValueParameter.type(typeTable: TypeTable): ProtoBuf.Type = when {
|
||||
hasType() -> type
|
||||
hasTypeId() -> typeTable[typeId]
|
||||
else -> error("No type in ProtoBuf.ValueParameter")
|
||||
}
|
||||
|
||||
fun ProtoBuf.ValueParameter.varargElementType(typeTable: TypeTable): ProtoBuf.Type? {
|
||||
return when {
|
||||
hasVarargElementType() -> varargElementType
|
||||
hasVarargElementTypeId() -> typeTable[varargElementTypeId]
|
||||
else -> null
|
||||
}
|
||||
fun ProtoBuf.ValueParameter.varargElementType(typeTable: TypeTable): ProtoBuf.Type? = when {
|
||||
hasVarargElementType() -> varargElementType
|
||||
hasVarargElementTypeId() -> typeTable[varargElementTypeId]
|
||||
else -> null
|
||||
}
|
||||
|
||||
fun ProtoBuf.Type.outerType(typeTable: TypeTable): ProtoBuf.Type? {
|
||||
return when {
|
||||
hasOuterType() -> outerType
|
||||
hasOuterTypeId() -> typeTable[outerTypeId]
|
||||
else -> null
|
||||
}
|
||||
fun ProtoBuf.Type.outerType(typeTable: TypeTable): ProtoBuf.Type? = when {
|
||||
hasOuterType() -> outerType
|
||||
hasOuterTypeId() -> typeTable[outerTypeId]
|
||||
else -> null
|
||||
}
|
||||
|
||||
fun ProtoBuf.Type.abbreviatedType(typeTable: TypeTable): ProtoBuf.Type? =
|
||||
when {
|
||||
hasAbbreviatedType() -> abbreviatedType
|
||||
hasAbbreviatedTypeId() -> typeTable[abbreviatedTypeId]
|
||||
else -> null
|
||||
}
|
||||
fun ProtoBuf.Type.abbreviatedType(typeTable: TypeTable): ProtoBuf.Type? = when {
|
||||
hasAbbreviatedType() -> abbreviatedType
|
||||
hasAbbreviatedTypeId() -> typeTable[abbreviatedTypeId]
|
||||
else -> null
|
||||
}
|
||||
|
||||
fun ProtoBuf.TypeAlias.underlyingType(typeTable: TypeTable): ProtoBuf.Type =
|
||||
if (hasUnderlyingTypeId()) typeTable[underlyingTypeId] else underlyingType
|
||||
fun ProtoBuf.TypeAlias.underlyingType(typeTable: TypeTable): ProtoBuf.Type = when {
|
||||
hasUnderlyingType() -> underlyingType
|
||||
hasUnderlyingTypeId() -> typeTable[underlyingTypeId]
|
||||
else -> error("No underlyingType in ProtoBuf.TypeAlias")
|
||||
}
|
||||
|
||||
fun ProtoBuf.TypeAlias.expandedType(typeTable: TypeTable): ProtoBuf.Type =
|
||||
if (hasExpandedTypeId()) typeTable[expandedTypeId] else expandedType
|
||||
fun ProtoBuf.TypeAlias.expandedType(typeTable: TypeTable): ProtoBuf.Type = when {
|
||||
hasExpandedType() -> expandedType
|
||||
hasExpandedTypeId() -> typeTable[expandedTypeId]
|
||||
else -> error("No expandedType in ProtoBuf.TypeAlias")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user