From e21c73229c435bc2a5f4b3918805e66a2a482b4a Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Mon, 12 Mar 2018 20:14:18 +0100 Subject: [PATCH] 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 --- .../deserialization/protoTypeTableUtil.kt | 117 +++++++++--------- 1 file changed, 58 insertions(+), 59 deletions(-) diff --git a/core/metadata/src/org/jetbrains/kotlin/metadata/deserialization/protoTypeTableUtil.kt b/core/metadata/src/org/jetbrains/kotlin/metadata/deserialization/protoTypeTableUtil.kt index 437eb14b9d0..aa51369a1d8 100644 --- a/core/metadata/src/org/jetbrains/kotlin/metadata/deserialization/protoTypeTableUtil.kt +++ b/core/metadata/src/org/jetbrains/kotlin/metadata/deserialization/protoTypeTableUtil.kt @@ -18,87 +18,86 @@ package org.jetbrains.kotlin.metadata.deserialization import org.jetbrains.kotlin.metadata.ProtoBuf -fun ProtoBuf.Class.supertypes(typeTable: TypeTable): List { - 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 = + 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 = + upperBoundList.takeIf(Collection<*>::isNotEmpty) ?: upperBoundIdList.map { typeTable[it] } -fun ProtoBuf.TypeParameter.upperBounds(typeTable: TypeTable): List { - 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") +}