Support correct way of writing/loading version requirement table

Tests are added in subsequent commits.

KT-25120 Fixed
This commit is contained in:
Alexander Udalov
2018-07-12 13:59:42 +02:00
parent 4122021090
commit cfc0f5e453
11 changed files with 67 additions and 12 deletions
@@ -24,10 +24,7 @@ import org.jetbrains.kotlin.descriptors.deserialization.ClassDescriptorFactory
import org.jetbrains.kotlin.descriptors.deserialization.PlatformDependentDeclarationFilter
import org.jetbrains.kotlin.incremental.components.LookupTracker
import org.jetbrains.kotlin.metadata.ProtoBuf
import org.jetbrains.kotlin.metadata.deserialization.BinaryVersion
import org.jetbrains.kotlin.metadata.deserialization.NameResolver
import org.jetbrains.kotlin.metadata.deserialization.TypeTable
import org.jetbrains.kotlin.metadata.deserialization.VersionRequirementTable
import org.jetbrains.kotlin.metadata.deserialization.*
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.protobuf.ExtensionRegistryLite
import org.jetbrains.kotlin.resolve.constants.ConstantValue
@@ -96,9 +93,12 @@ class DeserializationContext(
typeParameterProtos: List<ProtoBuf.TypeParameter>,
nameResolver: NameResolver = this.nameResolver,
typeTable: TypeTable = this.typeTable,
versionRequirementTable: VersionRequirementTable = this.versionRequirementTable,
metadataVersion: BinaryVersion = this.metadataVersion
): DeserializationContext = DeserializationContext(
components, nameResolver, descriptor, typeTable, versionRequirementTable, metadataVersion, this.containerSource,
components, nameResolver, descriptor, typeTable,
if (isVersionRequirementTableWrittenCorrectly(metadataVersion)) versionRequirementTable else this.versionRequirementTable,
metadataVersion, this.containerSource,
parentTypeDeserializer = this.typeDeserializer, typeParameters = typeParameterProtos
)
}
@@ -45,7 +45,10 @@ class DeserializedClassDescriptor(
private val visibility = ProtoEnumFlags.visibility(Flags.VISIBILITY.get(classProto.flags))
private val kind = ProtoEnumFlags.classKind(Flags.CLASS_KIND.get(classProto.flags))
val c = outerContext.childContext(this, classProto.typeParameterList, nameResolver, TypeTable(classProto.typeTable), metadataVersion)
val c = outerContext.childContext(
this, classProto.typeParameterList, nameResolver, TypeTable(classProto.typeTable),
VersionRequirementTable.create(classProto.versionRequirementTable), metadataVersion
)
private val staticScope = if (kind == ClassKind.ENUM_CLASS) StaticScopeForKotlinEnum(c.storageManager, this) else MemberScope.Empty
private val typeConstructor = DeserializedClassTypeConstructor()
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. 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.metadata.deserialization
// The purpose of utilities in this file is to support different behavior in deserialization according to the given binary file's version.
//
// For example, if we find a bug in serialization/deserialization and would like to fix it _remaining compatible_ with two latest versions
// of Kotlin, we can use methods of this class to fix deserialization of the "future" binaries, and later (in the next major version)
// fix the bug in serialization when the binary version advances to the value supported in the first bug fix.
/**
* Before Kotlin 1.4, version requirements for nested classes were deserialized incorrectly: the version requirement table was loaded from
* the outermost class and passed to the nested classes and their members, even though indices of their version requirements were pointing
* to the other table stored in the nested class (which was not read by deserialization). See KT-25120 for more information
*/
fun isVersionRequirementTableWrittenCorrectly(version: BinaryVersion): Boolean =
isKotlin1Dot4OrLater(version)
private fun isKotlin1Dot4OrLater(version: BinaryVersion): Boolean {
// All metadata versions (JVM, JS, common) will be advanced to 1.4.0 in Kotlin 1.4
return version.major == 1 && version.minor >= 4
}