Handle changes to inline functions/property accessors with @JvmNames

If we detect a change in an inline function `foo` with @JvmName
`fooJvmName`, we have two options:
   1. Report that function `foo` has changed
   2. Report that method `fooJvmName` has changed

Similarly, if we detect a change in an inline property accessor with
JvmName `getFoo` of property `foo`, we have two options:
   1. Report that property `foo` has changed
   2. Report that property accessor `getFoo` has changed

The compiler is guaranteed to generate `LookupSymbol`s corresponding to
option 1 when referencing inline functions/property accessors, but it is
not guaranteed to generate `LookupSymbol`s corresponding to option 2.
(Currently the compiler seems to support option 2 for *inline*
functions/property accessors, but that may change.)

Therefore, we will choose option 1 as it is cleaner and safer.

^KT-54144 In progress

Small cleanup in IncrementalCompilerRunner

 - Add comment for closing caches
 - Rename providedChangedFiles to changedFiles
 - Tiny clean up in `performWorkBeforeCompilation`
 - Count directories to delete in debug logs

^KT-53015 In progress

Extract KotlinClassInfo to a separate class

to reduce the size of IncrementalJvmCache and prepare for the next
change.

^KT-54144 In progress

Ignore inline functions that are not found in the bytecode

^KT-54144 In progress

Add unit test for handling `@JvmName`s

Test: KotlinOnlyClasspathChangesComputerTest
             #testFunctionsAndPropertyAccessorsWithJvmNames
^KT-54144 Fixed

Update unit tests for handling `@JvmName`s

In a previous commit, we made a behavior change for inline property
accessors: The existing behavior is that if the implementation of an
inline getter has changed, only usages of the getter will be impacted
but not usages of the setter (and vice versa).

After that previous commit, usages of *both* the getter and setter will
now be impacted (i.e., we might compile slightly more files). This is
because a change to either the getter or the setter will now be
considered a change to the property, which will help simplify our change
analysis.

This commit updates the relevant unit tests to reflect the new behavior.

Test: Updated Incremental*TestGenerated.PureKotlin#testInlinePropertyInClass
      and Incremental*TestGenerated.PureKotlin#testInlinePropertyOnTopLevel

^KT-54144 Fixed
This commit is contained in:
Hung Nguyen
2022-10-13 19:32:14 +01:00
committed by nataliya.valtman
parent e1e07d2094
commit cdbbead157
21 changed files with 515 additions and 366 deletions
@@ -24,27 +24,44 @@ import org.jetbrains.kotlin.metadata.deserialization.NameResolver
import org.jetbrains.kotlin.metadata.deserialization.TypeTable
import org.jetbrains.kotlin.metadata.deserialization.getExtensionOrNull
import org.jetbrains.kotlin.metadata.jvm.JvmProtoBuf
import org.jetbrains.kotlin.metadata.jvm.JvmProtoBuf.JvmMethodSignature
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmMemberSignature
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmProtoBufUtil
import org.jetbrains.kotlin.serialization.deserialization.ProtoEnumFlags
import org.jetbrains.kotlin.serialization.deserialization.descriptorVisibility
fun inlineFunctionsAndAccessors(header: KotlinClassHeader): List<JvmMemberSignature.Method> {
sealed interface InlineFunctionOrAccessor {
val jvmMethodSignature: JvmMemberSignature.Method
}
data class InlineFunction(
override val jvmMethodSignature: JvmMemberSignature.Method,
/** The Kotlin name of the function. It may be different from the JVM name of the function if [JvmName] is used. */
val kotlinFunctionName: String
) : InlineFunctionOrAccessor
data class InlinePropertyAccessor(
override val jvmMethodSignature: JvmMemberSignature.Method,
/** The name of the property that this property accessor belongs to. */
val propertyName: String
) : InlineFunctionOrAccessor
fun inlineFunctionsAndAccessors(header: KotlinClassHeader, excludePrivateMembers: Boolean = false): List<InlineFunctionOrAccessor> {
val data = header.data ?: return emptyList()
val strings = header.strings ?: return emptyList()
return when (header.kind) {
KotlinClassHeader.Kind.CLASS -> {
val (nameResolver, classProto) = JvmProtoBufUtil.readClassDataFrom(data, strings)
inlineFunctions(classProto.functionList, nameResolver, classProto.typeTable) +
inlineAccessors(classProto.propertyList, nameResolver)
inlineFunctions(classProto.functionList, nameResolver, classProto.typeTable, excludePrivateMembers) +
inlinePropertyAccessors(classProto.propertyList, nameResolver, excludePrivateMembers)
}
KotlinClassHeader.Kind.FILE_FACADE,
KotlinClassHeader.Kind.MULTIFILE_CLASS_PART -> {
val (nameResolver, packageProto) = JvmProtoBufUtil.readPackageDataFrom(data, strings)
inlineFunctions(packageProto.functionList, nameResolver, packageProto.typeTable) +
inlineAccessors(packageProto.propertyList, nameResolver)
inlineFunctions(packageProto.functionList, nameResolver, packageProto.typeTable, excludePrivateMembers) +
inlinePropertyAccessors(packageProto.propertyList, nameResolver, excludePrivateMembers)
}
else -> emptyList()
}
@@ -53,40 +70,51 @@ fun inlineFunctionsAndAccessors(header: KotlinClassHeader): List<JvmMemberSignat
private fun inlineFunctions(
functions: List<ProtoBuf.Function>,
nameResolver: NameResolver,
protoTypeTable: ProtoBuf.TypeTable
): List<JvmMemberSignature.Method> {
protoTypeTable: ProtoBuf.TypeTable,
excludePrivateFunctions: Boolean = false
): List<InlineFunction> {
val typeTable = TypeTable(protoTypeTable)
return functions.filter { Flags.IS_INLINE.get(it.flags) }.mapNotNull {
JvmProtoBufUtil.getJvmMethodSignature(it, nameResolver, typeTable)
}
return functions
.filter { Flags.IS_INLINE.get(it.flags) && (!excludePrivateFunctions || !isPrivate(it.flags)) }
.mapNotNull { inlineFunction ->
JvmProtoBufUtil.getJvmMethodSignature(inlineFunction, nameResolver, typeTable)?.let {
InlineFunction(jvmMethodSignature = it, kotlinFunctionName = nameResolver.getString(inlineFunction.name))
}
}
}
fun inlineAccessors(
private fun inlinePropertyAccessors(
properties: List<ProtoBuf.Property>,
nameResolver: NameResolver,
excludePrivateAccessors: Boolean = false
): List<JvmMemberSignature.Method> {
val inlineAccessors = mutableListOf<JvmMethodSignature>()
fun isInline(flags: Int) = Flags.IS_INLINE_ACCESSOR.get(flags)
fun isPrivate(flags: Int) = DescriptorVisibilities.isPrivate(ProtoEnumFlags.descriptorVisibility(Flags.VISIBILITY.get(flags)))
): List<InlinePropertyAccessor> {
val inlineAccessors = mutableListOf<InlinePropertyAccessor>()
properties.forEach { property ->
val propertySignature = property.getExtensionOrNull(JvmProtoBuf.propertySignature) ?: return@forEach
if (property.hasGetterFlags() && isInline(property.getterFlags)) {
if (!(excludePrivateAccessors && isPrivate(property.getterFlags))) {
inlineAccessors.add(propertySignature.getter)
}
if (property.hasGetterFlags() && Flags.IS_INLINE_ACCESSOR.get(property.getterFlags)
&& (!excludePrivateAccessors || !isPrivate(property.getterFlags))
) {
val getter = propertySignature.getter
inlineAccessors.add(
InlinePropertyAccessor(
JvmMemberSignature.Method(name = nameResolver.getString(getter.name), desc = nameResolver.getString(getter.desc)),
propertyName = nameResolver.getString(property.name)
)
)
}
if (property.hasSetterFlags() && isInline(property.setterFlags)) {
if (!(excludePrivateAccessors && isPrivate(property.setterFlags))) {
inlineAccessors.add(propertySignature.setter)
}
if (property.hasSetterFlags() && Flags.IS_INLINE_ACCESSOR.get(property.setterFlags)
&& (!excludePrivateAccessors || !isPrivate(property.setterFlags))
) {
val setter = propertySignature.setter
inlineAccessors.add(
InlinePropertyAccessor(
JvmMemberSignature.Method(name = nameResolver.getString(setter.name), desc = nameResolver.getString(setter.desc)),
propertyName = nameResolver.getString(property.name)
)
)
}
}
return inlineAccessors.map {
JvmMemberSignature.Method(name = nameResolver.getString(it.name), desc = nameResolver.getString(it.desc))
}
return inlineAccessors
}
private fun isPrivate(flags: Int) = DescriptorVisibilities.isPrivate(ProtoEnumFlags.descriptorVisibility(Flags.VISIBILITY.get(flags)))