Skip to content

Add CustomKotlinLikeDumpStrategy.nameOf API#5741

Open
ZacSweers wants to merge 1 commit intoJetBrains:masterfrom
ZacSweers:z/betterKotlinDump
Open

Add CustomKotlinLikeDumpStrategy.nameOf API#5741
ZacSweers wants to merge 1 commit intoJetBrains:masterfrom
ZacSweers:z/betterKotlinDump

Conversation

@ZacSweers
Copy link
Contributor

@ZacSweers ZacSweers commented Mar 12, 2026

Currently, class names in dumpKotlinLike() always just print the simple class name. This works fine in a lot of scenarios, but is ambiguous in scenarios where the class name is either a companion object or a common/generated simple name. This is a proposal API to allow customization of class names with added container context.

If this is acceptable, I can also add a test to the sandbox.

Open questions (aside from the API itself)

  1. Should this expand to any named declaration?
  2. Should we tweak the existing behavior to, for companion objects, print the parent class + companion for clarity? I am not sure there are many scenarios where just Companion is desirable. Though it would be a large change to existing dump files.

Here is an example PR using a fork based on this API: ZacSweers/metro#1976

@kotlin-safe-merge
Copy link

kotlin-safe-merge bot commented Mar 12, 2026

Code Owners

Rule Owners Approval
/​compiler/​ir/​ir.​tree/​ kotlin-common-backend 🔴

@ddolovov
Copy link
Contributor

Hi. Could you please provide examples when printing companios object's simple name is not sufficient?

From my experience, it's usually the top-level class that is dumped. So, there would be a nested object that would be structurally visibly as located under the class. Something like this

class Foo
  companion object Companion {
    ...
  }

Isn't that clear enough?

@ZacSweers
Copy link
Contributor Author

There are a lot of examples in the linked PR. It's not about class dumps but rather how IrCall expressions are rendered, as they do not currently show any context indicating what class's companion object is being invoked.

@ddolovov
Copy link
Contributor

There are a lot of examples in the linked PR. It's not about class dumps but rather how IrCall expressions are rendered, as they do not currently show any context indicating what class's companion object is being invoked.

OK, I see. It's about replacing

Companion.newInstance(b = <this>.#b.invoke(), e = <this>.#e.invoke())

by something like

MetroFactory/* companion */.newInstance(b = <this>.#b.invoke(), e = <this>.#e.invoke())

I'd say it's better to render the exact name of the companion object instead of adding /* companion */ comment, because companions may have different names. Ex:

class Foo {
    companion object Bar
}

But it's up to you, because the actual nameOf() implementation is in your repository.

Copy link
Contributor

@ddolovov ddolovov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please squash commits into a single one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure container parameter is necessary. We can always deduce the container from the declaration using IrDeclaration.parent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the declaration being referenced here isn't necessarily in the container though. This is like a context scope, whereas the declaration is the resolved referenced class

Comment on lines 209 to 211
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be useful to have a more generic version of nameOf() suitable for any IrDeclarationWithName.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@ZacSweers ZacSweers force-pushed the z/betterKotlinDump branch from 25720c5 to 76936d8 Compare March 20, 2026 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants