This message was deleted.
# compiler
s
This message was deleted.
🚫 1
y
I severely doubt that the compiler would let you reparent a class like that You can use a nested type alias though. Not sure if it'll be enough for your purposes.
What's the motivation behind placing
Foo
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.
Maybe generate
Namespace
inside
Foo
as a
private object
or something
Bar
might complain about the visibility though... Okay, how about this:
Copy code
@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)
  }
}
Oh crap! 😅 I think it's reasonable to add a checker for this. Should be easy enough! And because it's
sealed
, 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 it
There isn't an easy solution for this in the base language. You're looking for something between
private
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:
Copy code
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`:
Copy code
class Foo {
  class Bar @Sub private constructor() {
    companion object {
      @JvmSynthetic fun create() = Bar()
    }
  }

  companion object {
    private fun Bar(): Bar = Bar.create()
  }
}
Using
@JvmSynthetic
and
@Deprecated(..., level = DeprecationLevel.HIDDEN)
, you can do it all in FIR even:
Copy code
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 it
If you add things only in IR, other Kotlin modules won't see it (IIRC), and Java will, hence why you should use
@JvmSynthetic
For the longest time, FIR wasn't even a thing. The declaration generator extension was a huge step forward for compiler plugins because you could finally affect the declarations the compiler actually sees. So yes, if something is not declared in FIR, it won't be resolvable by code at all. IR is very unrestricted in what you can generate, and there's not really a reason to restrict IR from generating extra declarations (in fact, the compiler itself generates a lot of declarations in IR). I.e. it's not a hack, it's the status quo Took me some time, but I found "evidence" of this: https://kotlinlang.slack.com/archives/CJ699L62W/p1669725866579599?thread_ts=1669719907.683379&cid=CJ699L62W Some plugins already do this, FWIW, e.g. github.com/kyay10/metro/blob/…/GeneratedClassIr.kt
Can confirm:
Generating declarations in IR is supported, compiler itself does it e.g. adding more functions for
@JvmOverloads
or transforming lambdas into classes. One must be mindful about IC though.
Can also confirm, IC is not a concern if you're generating something and only using it in the same file.
An IR-generated declaration like that is only visible in metadata if you call
IrPluginContext.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 compiler
That 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.
I don't think that part is needed, but I'm not 1000% sure. My understanding is that
@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
@Deprecation
b
Also 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.
❤️ 1
thank you color 1