You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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; ...}
) becausesym.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 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. However, we already have logic in FirstTransform which reorders statements so that companions and classes are next to each other: https://github.com/lampepfl/dotty/blob/8f99dad4664ff28b4d77e0ab0b1e6fe7c590524a/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala#L70-L71
If we moved this logic before pickling, that would simplify our job in unpickling (though we'd still need more complex logic to find the companions in old tasty files).
This came up in #14105 which had to special-case local classes to avoid introducing a difference in behavior between typing and unpickling.
The text was updated successfully, but these errors were encountered: