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:
Alexander Udalov
2018-03-12 20:14:18 +01:00
parent 0a78fe8ae3
commit e21c73229c
@@ -18,87 +18,86 @@ package org.jetbrains.kotlin.metadata.deserialization
import org.jetbrains.kotlin.metadata.ProtoBuf import org.jetbrains.kotlin.metadata.ProtoBuf
fun ProtoBuf.Class.supertypes(typeTable: TypeTable): List<ProtoBuf.Type> { // TODO: return null and report a diagnostic instead of throwing exceptions
return supertypeList.takeIf(Collection<*>::isNotEmpty) ?: supertypeIdList.map { typeTable[it] }
}
fun ProtoBuf.Type.Argument.type(typeTable: TypeTable): ProtoBuf.Type? { fun ProtoBuf.Class.supertypes(typeTable: TypeTable): List<ProtoBuf.Type> =
return when { supertypeList.takeIf(Collection<*>::isNotEmpty) ?: supertypeIdList.map { typeTable[it] }
fun ProtoBuf.Type.Argument.type(typeTable: TypeTable): ProtoBuf.Type? = when {
hasType() -> type hasType() -> type
hasTypeId() -> typeTable[typeId] hasTypeId() -> typeTable[typeId]
else -> null else -> null
}
} }
fun ProtoBuf.Type.flexibleUpperBound(typeTable: TypeTable): ProtoBuf.Type? { fun ProtoBuf.Type.flexibleUpperBound(typeTable: TypeTable): ProtoBuf.Type? = when {
return when {
hasFlexibleUpperBound() -> flexibleUpperBound hasFlexibleUpperBound() -> flexibleUpperBound
hasFlexibleUpperBoundId() -> typeTable[flexibleUpperBoundId] hasFlexibleUpperBoundId() -> typeTable[flexibleUpperBoundId]
else -> null else -> null
}
} }
fun ProtoBuf.TypeParameter.upperBounds(typeTable: TypeTable): List<ProtoBuf.Type> { fun ProtoBuf.TypeParameter.upperBounds(typeTable: TypeTable): List<ProtoBuf.Type> =
return upperBoundList.takeIf(Collection<*>::isNotEmpty) ?: upperBoundIdList.map { typeTable[it] } upperBoundList.takeIf(Collection<*>::isNotEmpty) ?: upperBoundIdList.map { typeTable[it] }
}
fun ProtoBuf.Function.returnType(typeTable: TypeTable): ProtoBuf.Type { fun ProtoBuf.Function.returnType(typeTable: TypeTable): ProtoBuf.Type = when {
return if (hasReturnType()) returnType else typeTable[returnTypeId] hasReturnType() -> returnType
hasReturnTypeId() -> typeTable[returnTypeId]
else -> error("No returnType in ProtoBuf.Function")
} }
fun ProtoBuf.Function.hasReceiver(): Boolean = hasReceiverType() || hasReceiverTypeId() fun ProtoBuf.Function.hasReceiver(): Boolean = hasReceiverType() || hasReceiverTypeId()
fun ProtoBuf.Function.receiverType(typeTable: TypeTable): ProtoBuf.Type? { fun ProtoBuf.Function.receiverType(typeTable: TypeTable): ProtoBuf.Type? = when {
return when {
hasReceiverType() -> receiverType hasReceiverType() -> receiverType
hasReceiverTypeId() -> typeTable[receiverTypeId] hasReceiverTypeId() -> typeTable[receiverTypeId]
else -> null else -> null
}
} }
fun ProtoBuf.Property.returnType(typeTable: TypeTable): ProtoBuf.Type { fun ProtoBuf.Property.returnType(typeTable: TypeTable): ProtoBuf.Type = when {
return if (hasReturnType()) returnType else typeTable[returnTypeId] hasReturnType() -> returnType
hasReturnTypeId() -> typeTable[returnTypeId]
else -> error("No returnType in ProtoBuf.Property")
} }
fun ProtoBuf.Property.hasReceiver(): Boolean = hasReceiverType() || hasReceiverTypeId() fun ProtoBuf.Property.hasReceiver(): Boolean = hasReceiverType() || hasReceiverTypeId()
fun ProtoBuf.Property.receiverType(typeTable: TypeTable): ProtoBuf.Type? { fun ProtoBuf.Property.receiverType(typeTable: TypeTable): ProtoBuf.Type? = when {
return when {
hasReceiverType() -> receiverType hasReceiverType() -> receiverType
hasReceiverTypeId() -> typeTable[receiverTypeId] hasReceiverTypeId() -> typeTable[receiverTypeId]
else -> null else -> null
}
} }
fun ProtoBuf.ValueParameter.type(typeTable: TypeTable): ProtoBuf.Type { fun ProtoBuf.ValueParameter.type(typeTable: TypeTable): ProtoBuf.Type = when {
return if (hasType()) type else typeTable[typeId] hasType() -> type
hasTypeId() -> typeTable[typeId]
else -> error("No type in ProtoBuf.ValueParameter")
} }
fun ProtoBuf.ValueParameter.varargElementType(typeTable: TypeTable): ProtoBuf.Type? { fun ProtoBuf.ValueParameter.varargElementType(typeTable: TypeTable): ProtoBuf.Type? = when {
return when {
hasVarargElementType() -> varargElementType hasVarargElementType() -> varargElementType
hasVarargElementTypeId() -> typeTable[varargElementTypeId] hasVarargElementTypeId() -> typeTable[varargElementTypeId]
else -> null else -> null
}
} }
fun ProtoBuf.Type.outerType(typeTable: TypeTable): ProtoBuf.Type? { fun ProtoBuf.Type.outerType(typeTable: TypeTable): ProtoBuf.Type? = when {
return when {
hasOuterType() -> outerType hasOuterType() -> outerType
hasOuterTypeId() -> typeTable[outerTypeId] hasOuterTypeId() -> typeTable[outerTypeId]
else -> null else -> null
}
} }
fun ProtoBuf.Type.abbreviatedType(typeTable: TypeTable): ProtoBuf.Type? = fun ProtoBuf.Type.abbreviatedType(typeTable: TypeTable): ProtoBuf.Type? = when {
when {
hasAbbreviatedType() -> abbreviatedType hasAbbreviatedType() -> abbreviatedType
hasAbbreviatedTypeId() -> typeTable[abbreviatedTypeId] hasAbbreviatedTypeId() -> typeTable[abbreviatedTypeId]
else -> null else -> null
} }
fun ProtoBuf.TypeAlias.underlyingType(typeTable: TypeTable): ProtoBuf.Type = fun ProtoBuf.TypeAlias.underlyingType(typeTable: TypeTable): ProtoBuf.Type = when {
if (hasUnderlyingTypeId()) typeTable[underlyingTypeId] else underlyingType hasUnderlyingType() -> underlyingType
hasUnderlyingTypeId() -> typeTable[underlyingTypeId]
else -> error("No underlyingType in ProtoBuf.TypeAlias")
}
fun ProtoBuf.TypeAlias.expandedType(typeTable: TypeTable): ProtoBuf.Type = fun ProtoBuf.TypeAlias.expandedType(typeTable: TypeTable): ProtoBuf.Type = when {
if (hasExpandedTypeId()) typeTable[expandedTypeId] else expandedType hasExpandedType() -> expandedType
hasExpandedTypeId() -> typeTable[expandedTypeId]
else -> error("No expandedType in ProtoBuf.TypeAlias")
}