Skip to content

Commit

Permalink
JVM KT-49613 don't generate indy reference to protected constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
dnpetrov committed Nov 10, 2021
1 parent a3820d4 commit 46af453
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 3 deletions.

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

Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private class TypeOperatorLowering(private val context: JvmBackendContext) : Fil
targetRef: IrFunctionReference
): IrCall {
fun fail(message: String): Nothing =
throw AssertionError("$message, irFunRef:\n${targetRef.dump()}")
throw AssertionError("$message, targetRef:\n${targetRef.dump()}")

val dynamicCallArguments = ArrayList<IrExpression>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ internal class LambdaMetafactoryArgumentsBuilder(
if (implFun.isInline)
return null

if (implFun is IrConstructor && DescriptorVisibilities.isPrivate(implFun.visibility)) {
if (isConstructorRequiringAccessor(implFun)) {
// Kotlin generates constructor accessors differently from Java.
// TODO more precise accessibility check (see SyntheticAccessorLowering::isAccessible)
return null
}

Expand Down Expand Up @@ -138,6 +137,15 @@ internal class LambdaMetafactoryArgumentsBuilder(
return getLambdaMetafactoryArgsOrNullInner(reference, samMethod, samType, implFun)
}

private fun isConstructorRequiringAccessor(implFun: IrFunction): Boolean {
if (implFun !is IrConstructor) return false
// We don't do exact accessibility check here:
// constructor will be called by a class generated by LambdaMetafactory at runtime.
val visibility = implFun.visibility
return visibility == DescriptorVisibilities.PROTECTED ||
DescriptorVisibilities.isPrivate(visibility)
}

private val javaIoSerializableFqn =
FqName("java.io").child(Name.identifier("Serializable"))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// TARGET_BACKEND: JVM
// JVM_TARGET: 1.8
// SAM_CONVERSIONS: INDY
// FILE: kt49613.kt

interface GetStep {
fun get(): Any
}

class Outer protected constructor(val ok: Any) {
constructor(): this("xxx")

val obj = object : GetStep {
override fun get() = Step(::Outer)
}
}

fun box(): String {
val s = Outer().obj.get() as Step
val t = s.step("OK") as Outer
return t.ok as String
}

// FILE: Step.java

public interface Step {
Object step(String string);
}

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.

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

0 comments on commit 46af453

Please sign in to comment.