Fir2Ir: provide fake file-level signatures for toplevel privates

We need to supply signatures for private declarations even when their
containing file is not known. In situations where the FirDeclaration in
question is already known (i.e. where we are never going to serach for
the declaation based on that signature), FirDeclaration itself is
sufficient as a key.
This commit is contained in:
Georgy Bronnikov
2022-05-05 09:54:02 +03:00
committed by Alexander Udalov
parent 8fd29a9fec
commit 7605494f2b
8 changed files with 64 additions and 1 deletions
@@ -18,6 +18,7 @@ import org.jetbrains.kotlin.fir.declarations.utils.visibility
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag
import org.jetbrains.kotlin.fir.visitors.FirVisitor
import org.jetbrains.kotlin.ir.util.IdSignature
import org.jetbrains.kotlin.name.FqName
// @NoMutableState -- we'll restore this annotation once we get rid of withFileSignature().
class FirBasedSignatureComposer(override val mangler: FirMangler) : Fir2IrSignatureComposer {
@@ -122,7 +123,7 @@ class FirBasedSignatureComposer(override val mangler: FirMangler) : Fir2IrSignat
else -> error("Unsupported FIR declaration in signature composer: ${declaration.render()}")
}
return if (isTopLevelPrivate(declaration) || forceTopLevelPrivate) {
val fileSig = fileSignature ?: return null
val fileSig = fileSignature ?: declaration.fakeFileSignature(publicSignature)
IdSignature.CompositeSignature(fileSig, publicSignature)
} else
publicSignature
@@ -181,6 +182,13 @@ class FirBasedSignatureComposer(override val mangler: FirMangler) : Fir2IrSignat
private fun isTopLevelPrivate(declaration: FirDeclaration): Boolean =
declaration.symbol.getOwnerLookupTag() == null && declaration is FirMemberDeclaration && declaration.visibility == Visibilities.Private
// We only need file signatures to distinguish between declarations with the same fqName across different files,
// so FirDeclaration itself is an appropriate id.
private fun FirDeclaration.fakeFileSignature(commonSignature: IdSignature.CommonSignature) =
IdSignature.FileSignature(
this, FqName(commonSignature.packageFqName + "." + commonSignature.declarationFqName), "<unknown>"
)
private fun FirProperty.getterOrDefault() =
getter ?: FirDefaultPropertyGetter(
source = null,
@@ -7415,6 +7415,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/platformTypes.kt");
}
@Test
@TestMetadata("privateSuperType.kt")
public void testPrivateSuperType() throws Exception {
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/privateSuperType.kt");
}
@Test
@TestMetadata("propertyReference.kt")
public void testPropertyReference() throws Exception {
@@ -0,0 +1,19 @@
// MODULE: lib
// FILE: Z.kt
package z
private interface I {
fun k() = "K"
}
interface G<T>
class Z : I, G<I> {
val o = "O"
}
// MODULE: main(lib)
// FILE: box.kt
import z.Z
fun box() = Z().run { o + k() }
@@ -7301,6 +7301,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/platformTypes.kt");
}
@Test
@TestMetadata("privateSuperType.kt")
public void testPrivateSuperType() throws Exception {
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/privateSuperType.kt");
}
@Test
@TestMetadata("propertyReference.kt")
public void testPropertyReference() throws Exception {
@@ -7415,6 +7415,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/platformTypes.kt");
}
@Test
@TestMetadata("privateSuperType.kt")
public void testPrivateSuperType() throws Exception {
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/privateSuperType.kt");
}
@Test
@TestMetadata("propertyReference.kt")
public void testPropertyReference() throws Exception {
@@ -397,6 +397,12 @@ public class JvmIrAgainstOldBoxTestGenerated extends AbstractJvmIrAgainstOldBoxT
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/platformTypes.kt");
}
@Test
@TestMetadata("privateSuperType.kt")
public void testPrivateSuperType() throws Exception {
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/privateSuperType.kt");
}
@Test
@TestMetadata("propertyReference.kt")
public void testPropertyReference() throws Exception {
@@ -397,6 +397,12 @@ public class JvmOldAgainstIrBoxTestGenerated extends AbstractJvmOldAgainstIrBoxT
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/platformTypes.kt");
}
@Test
@TestMetadata("privateSuperType.kt")
public void testPrivateSuperType() throws Exception {
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/privateSuperType.kt");
}
@Test
@TestMetadata("propertyReference.kt")
public void testPropertyReference() throws Exception {
@@ -5605,6 +5605,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/nestedTypeAliasExpansion.kt");
}
@Test
@TestMetadata("privateSuperType.kt")
public void testPrivateSuperType() throws Exception {
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/privateSuperType.kt");
}
@Test
@TestMetadata("propertyReference.kt")
public void testPropertyReference() throws Exception {