scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.81k stars 1.05k forks source link

TASTy should list the types of template parents #8028

Open bishabosha opened 4 years ago

bishabosha commented 4 years ago

Currently in order to unpickle the parents of a template in TASTy, you must parse many arbitrary trees including selecting the correct overloaded constructor, and performing unification of type variables on the return type of that constructor.

In Scala 2 all that is required is the types of the parents; it is inefficient to traverse expressions to calculate and propagate a type.

there should be a simple list of types enumerating the parents, that can be referenced to from the actual trees that make up the extends clauses.

LPTK commented 4 years ago

As far as I know, one way of doing it in Scala 2 is through symbols. One can call baseClasses on the child ClassSymbol, and then call child.selfType.baseType(theParentSymbol) to get the type of the parent from the POV of the child.

This does not seem possible with the current API, but it would be very useful.

Also, is there a reason why we cannot list the members of a Type anymore?

bishabosha commented 4 years ago

I has been possible to optimise reading the parents by shortcutting traversals to only go as far as identifying the prefix of a parent constructor. Which may be cached in a simple tree if seen before within the same tasty file

sjrd commented 4 years ago

Even if you found a more efficient workaround, I still believe that this information not being available at our fingers is an issue, that needs fixing.

I'll reopen so that this doesn't get lost.

bishabosha commented 4 years ago

for the definition

class Lexeme private (private val str: String) extends AnyVal with Ordered[Lexeme] {
  def compare(that: Lexeme) = str.compareTo(that.str)
}

the tasty we care about looks like

     5:   TYPEDEF(183) 2 [Lexeme]
     9:     TEMPLATE(162)
...
    22:       APPLY(9)
    24:         SELECT 13 [<init>[Signed Signature(List(),java.lang.Object)]]
    26:           NEW
    27:             IDENTtpt 14 [AnyVal]
    29:               TYPEREF 14 [AnyVal]
    31:                 TERMREFpkg 6 [scala]
    33:       APPLIEDtpt(14)
    35:         IDENTtpt 15 [Ordered]
    37:           TYPEREF 15 [Ordered]
    39:             TERMREF 16 [package]
    41:               SHAREDtype 19
    43:         IDENTtpt 2 [Lexeme]
    45:           TYPEREFsymbol 5
    47:             TERMREFpkg 1 [tastytest]

and we can basically skip Address 22 as there is no need to resolve the constructor and complex trees can be avoided for IDENTtpt and APPLIEDtpt, just use the simple TypeTree wrapper