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

Do local classes have companions? Typer and TreeUnpickler disagree. #14508

Open
smarter opened this issue Feb 17, 2022 · 1 comment
Open

Do local classes have companions? Typer and TreeUnpickler disagree. #14508

smarter opened this issue Feb 17, 2022 · 1 comment

Comments

@smarter
Copy link
Member

smarter commented Feb 17, 2022

The logic responsible for making .companionClass/.companionModule work after unpickling is:
https://github.com/lampepfl/dotty/blob/8f99dad4664ff28b4d77e0ab0b1e6fe7c590524a/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala#L847-L856
But this does not work for local classes e.g.(val x = { class C; object C; ...}) because sym.scalacLinkedClass will look for the companion in the current scope, but when unpickling we don't enter local symbols in scopes (since the pickle refers to them by their address). To fix this we could either:

This came up in #14105 which had to special-case local classes to avoid introducing a difference in behavior between typing and unpickling.

@odersky
Copy link
Contributor

odersky commented Feb 21, 2022

This is tricky because a class and its companion can appear in any order in a list of statements, so we would need to remember which classes we've seen as we traverse the list.

I think it should still be doable without too much effort. We can ignore anonymous classes. Other local classes should be rare, so as long as we wait creating data structures until we hit one, we should be OK.

However, we already have logic in FirstTransform which reorders statements so that companions and classes are next to each other:

The problem with that is that it destroys the natural tree that IDEs would like to see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants