Slackbot
10/13/2026, 9:18 AMYoussef Shoaib [MOD]
10/13/2026, 9:28 AMYoussef Shoaib [MOD]
10/13/2026, 9:35 AMFoo inside such a namespace?
If it's about constraining the usage of some declaration or something, note that you can use @JvmSynthetic and @Deprecated(..., level = DeprecationLevel.HIDDEN) to hide declaration, but still be able to use them in IR. Or, you can always generate declarations in IR, and they won't be visible to Kotlin code
I like this "key" idea here of having private companion object: Namespace. It doesn't necessitate this nesting though. You can expose the object using a function that's hidden away as described above.Youssef Shoaib [MOD]
10/13/2026, 9:47 AMNamespace inside Foo as a private object or something
Bar might complain about the visibility though...
Okay, how about this:
@Sub("Namespace") class Foo {
// sealed class Namespace
// private object NamespaceImpl: Namespace()
class Bar @Sub private constructor() {
// `invoke` with Namespace like you have above
}
fun f(): Foo.Bar {
return Foo.Bar(NamespaceImpl)
}
}Youssef Shoaib [MOD]
10/13/2026, 10:31 AMsealed, no one outside the module can subclass it, i.e. any potential erroneous subclassing will only happen with the compiler plugin loaded, so the checker will catch itYoussef Shoaib [MOD]
10/13/2026, 12:10 PMprivate and "file-private", which sadly doesn't exist.
Remember that you control the IR, so you can e.g. add extra constructors . The bodies of your FIR. generated functions don't need to be filled in until IR.
You can simplify your design down to:
class Foo {
class Bar @Sub private constructor()
// companion object {
// private fun Bar(): Bar // no body
// }
fun f(): Foo.Bar {
return Foo.Bar()
}
}
Then in IR, add your factory method to `Bar`:
class Foo {
class Bar @Sub private constructor() {
companion object {
@JvmSynthetic fun create() = Bar()
}
}
companion object {
private fun Bar(): Bar = Bar.create()
}
}Youssef Shoaib [MOD]
10/13/2026, 12:14 PM@JvmSynthetic and @Deprecated(..., level = DeprecationLevel.HIDDEN) , you can do it all in FIR even:
class Foo {
class Bar @Sub private constructor() {
// companion object {
// @JvmSynthetic @Deprecated("", level = DeprecationLevel.HIDDEN) fun create() = Bar()
// }
}
// companion object {
// private fun Bar(): Bar = Bar.create() // either somehow filled in FIR, or just fill it in IR normally
// }
fun f(): Foo.Bar {
return Foo.Bar()
}
}
Thus only Foo and its inner scope sees Bar(), and create is hidden from everyone, but your plugin can still use itYoussef Shoaib [MOD]
10/13/2026, 3:53 PM@JvmSyntheticYoussef Shoaib [MOD]
10/13/2026, 4:29 PMYoussef Shoaib [MOD]
10/13/2026, 4:48 PMGenerating declarations in IR is supported, compiler itself does it e.g. adding more functions foror transforming lambdas into classes. One must be mindful about IC though.@JvmOverloads
Youssef Shoaib [MOD]
10/13/2026, 5:29 PMYoussef Shoaib [MOD]
10/14/2026, 8:25 AMIrPluginContext.metadataDeclarationRegistrar.register*AsMetadataVisible. Its docs say:
Use this service to:
• add annotations to declarations if those annotations should be saved into metadata
• register that declaration generated at IR stage will appear in compiled metadata
This service properly works only in K2 compilerThat request has been implemented btw, so there's now
registerClassAsMetadataVisible.
As long as you don't call those methods on the relevant function, it won't be visible.
If, for some reason, this ever breaks, remember that you can use @Deprecated("", DeprecationLevel.HIDDEN) to achieve a similar result.Youssef Shoaib [MOD]
10/14/2026, 8:54 AM@JvmSynthetic is processed later on in the backend, so it doesn't care about metadata or anything like that. It probably wouldn't hurt to call it though.
I believe yes "visible in metadata" == "visible in source code", barring any magic with hidden @Deprecationbnorm
10/14/2026, 11:39 AMAlso does being visible in the metadata make the function reachable in source code?A little context and clarification here: metadata is how the Kotlin compiler knows what is present in a dependency. For Kotlin dependencies, we don't read JVM byte code for example, we only read the metadata. So adding something to metadata makes it findable to downstream consumers of the produced artifact; findable by both source code and compiler plugins. For something to be findable within a project, it must be generated in FIR. Which then in turn is written to metadata automatically. So to know if you should be generating FIR, IR+metadata, or just IR, you'll need to know how the code you are generating will be used: users in the current project, users in downstream projects, or just compiler plugins. And I'm using findable here explicitly, because we can still find
private functions in metadata via a compiler plugin, you just can't call them because they are private/not visible. But it can still be useful to find them in cases when your compiler plugin has cross project behavior like a DI framework. So there's a slight, but important, distinction when talking about "visibility" in regards to metadata.