Skip to content

Commit

Permalink
Fix binary compatibility for inlined local delegated properies
Browse files Browse the repository at this point in the history
 #KT-54650 Fixed
  • Loading branch information
udalov committed Oct 31, 2022
1 parent 90b3e21 commit 0b49dc2
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ package org.jetbrains.kotlin.backend.common.lower
import org.jetbrains.kotlin.backend.common.BodyLoweringPass
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.descriptors.synthesizedString
import org.jetbrains.kotlin.backend.common.ir.*
import org.jetbrains.kotlin.backend.common.lower.inline.isInlineParameter
import org.jetbrains.kotlin.backend.common.runOnFilePostfix
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.DescriptorVisibility
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.*
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.builders.declarations.buildConstructor
import org.jetbrains.kotlin.ir.builders.declarations.buildFun
import org.jetbrains.kotlin.ir.builders.declarations.buildValueParameter
Expand All @@ -25,7 +26,9 @@ import org.jetbrains.kotlin.ir.symbols.IrFieldSymbol
import org.jetbrains.kotlin.ir.symbols.IrValueParameterSymbol
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrFieldSymbolImpl
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.transformStatement
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.defaultType
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
Expand Down Expand Up @@ -71,6 +74,7 @@ class LocalDeclarationsLowering(
val localNameSanitizer: (String) -> String = { it },
val visibilityPolicy: VisibilityPolicy = VisibilityPolicy.DEFAULT,
val suggestUniqueNames: Boolean = true, // When `true` appends a `$#index` suffix to lifted declaration names
val compatibilityModeForInlinedLocalDelegatedPropertyAccessors: Boolean = false, // Keep old names because of KT-49030
val forceFieldsForInlineCaptures: Boolean = false, // See `LocalClassContext`
private val postLocalDeclarationLoweringCallback: ((IntermediateDatastructures) -> Unit)? = null
) :
Expand Down Expand Up @@ -589,8 +593,16 @@ class LocalDeclarationsLowering(
val declarationName = localNameSanitizer(declaration.name.asString())
localFunctions[declaration]?.let {
val baseName = if (declaration.name.isSpecial) "lambda" else declarationName
if (it.index >= 0)
return if (suggestUniqueNames) "$baseName\$${it.index}" else baseName
if (it.index >= 0) {
if (!suggestUniqueNames) return baseName

val separator = if (
compatibilityModeForInlinedLocalDelegatedPropertyAccessors &&
declaration.origin == IrDeclarationOrigin.DELEGATED_PROPERTY_ACCESSOR &&
container is IrFunction && container.isInline
) "-" else "$"
return "$baseName$separator${it.index}"
}
}

return declarationName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ internal val localDeclarationsPhase = makeIrFilePhase(
private fun scopedVisibility(inInlineFunctionScope: Boolean): DescriptorVisibility =
if (inInlineFunctionScope) DescriptorVisibilities.PUBLIC else JavaDescriptorVisibilities.PACKAGE_VISIBILITY
},
compatibilityModeForInlinedLocalDelegatedPropertyAccessors = true,
forceFieldsForInlineCaptures = true,
postLocalDeclarationLoweringCallback = context.localDeclarationsLoweringData?.let {
{ data ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This test checks that we use the `...$lambda-0` method name, instead of `...$lambda$0`, for extracted accessors of local delegated properties.
// It's important to keep these names until we fix the binary compatibility issue KT-49030.

operator fun String.getValue(thisRef: Any?, prop: Any?): String = this
operator fun String.setValue(thisRef: Any?, prop: Any?, value: String) {}

inline fun foo(): String {
var x by "OK"
return x
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@kotlin.Metadata
public final class LocalDelegatedPropertyKt$foo$x$1 {
// source: 'localDelegatedProperty.kt'
public final static field INSTANCE: kotlin.reflect.KMutableProperty0
static method <clinit>(): void
method <init>(): void
public method getName(): java.lang.String
public method getOwner(): kotlin.reflect.KDeclarationContainer
public method getSignature(): java.lang.String
}

@kotlin.Metadata
public final class LocalDelegatedPropertyKt {
// source: 'localDelegatedProperty.kt'
synthetic final static field $$delegatedProperties: kotlin.reflect.KProperty[]
static method <clinit>(): void
public final static @org.jetbrains.annotations.NotNull method foo(): java.lang.String
public final static @org.jetbrains.annotations.NotNull method getValue(@org.jetbrains.annotations.NotNull p0: java.lang.String, @org.jetbrains.annotations.Nullable p1: java.lang.Object, @org.jetbrains.annotations.Nullable p2: java.lang.Object): java.lang.String
public final static method setValue(@org.jetbrains.annotations.NotNull p0: java.lang.String, @org.jetbrains.annotations.Nullable p1: java.lang.Object, @org.jetbrains.annotations.Nullable p2: java.lang.Object, @org.jetbrains.annotations.NotNull p3: java.lang.String): void
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@kotlin.Metadata
public final class LocalDelegatedPropertyKt {
// source: 'localDelegatedProperty.kt'
synthetic final static field $$delegatedProperties: kotlin.reflect.KProperty[]
static method <clinit>(): void
public synthetic final static method access$foo$lambda-0(p0: java.lang.String): java.lang.String
private final static method foo$lambda-0(p0: java.lang.String): java.lang.String
private final static method foo$lambda-1(p0: java.lang.String, p1: java.lang.String): void
public final static @org.jetbrains.annotations.NotNull method foo(): java.lang.String
public final static @org.jetbrains.annotations.NotNull method getValue(@org.jetbrains.annotations.NotNull p0: java.lang.String, @org.jetbrains.annotations.Nullable p1: java.lang.Object, @org.jetbrains.annotations.Nullable p2: java.lang.Object): java.lang.String
public final static method setValue(@org.jetbrains.annotations.NotNull p0: java.lang.String, @org.jetbrains.annotations.Nullable p1: java.lang.Object, @org.jetbrains.annotations.Nullable p2: java.lang.Object, @org.jetbrains.annotations.NotNull p3: java.lang.String): void
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0b49dc2

Please sign in to comment.