Open
Description
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:
- simply never register local companions when typing (does the spec say anything about whether local classes can have companions?)
- or call registerCompanion when indexing a list of statements: https://github.com/lampepfl/dotty/blob/8f99dad4664ff28b4d77e0ab0b1e6fe7c590524a/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala#L712-L713
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.