Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kotlin] J2K: Correctly reference inner class types in outer class header #2807

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ermattt
Copy link

@ermattt ermattt commented Jul 5, 2024

J2K is being a little ovezealous when adding imports for nested, locally defined types. In K1, postprocessing handles most of the clean up, though it still misses type references in outer class headers. In addition to fixing the references-in-outer-class-headers bug (which produces unbuildable kotlin code), we can improve output for K1 & K2 modes by applying a few rules during type symbol printing:

  1. If a type's definition and reference live in the same file, no import is needed as long as we follow the next 3 rules:
  2. If a type's definition is a parent node of the reference, we can use the type's short name
  3. If a type's definition and reference share the same direct parent node, we can use the type's short name
  4. If a type's definition and reference share a non-direct parent node, we can use the type's semi-qualified name

I wish I could move this logic out of the printing phase and somewhere earlier, but I'm not sure how to do that without a significant refactor of how type symbols are handled.

@abelkov @darthorimar @jocelynluizzi13

@abelkov abelkov self-assigned this Jul 8, 2024
@abelkov abelkov added the kotlin Pull requests for Kotlin IntelliJ plugin label Jul 8, 2024
@abelkov
Copy link
Contributor

abelkov commented Jul 19, 2024

which produces unbuildable kotlin code

Can you please add a test for this?

Note: the problem with redundant K2 imports may be later fixed in the K2 import optimizer that we rely on. The fix on the J2K side may not be necessary.

Changing the usage of a nested type to a qualified reference with no import looks like a purely style issue, yes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kotlin Pull requests for Kotlin IntelliJ plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants