Skip to content

Commit 7ccdee0

Browse files
committed
Simplify readTopLevel
1 parent b95a918 commit 7ccdee0

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ class TreeUnpickler(reader: TastyReader,
8282
/** The unpickled tree */
8383
def unpickle()(implicit ctx: Context): Tree = {
8484
assert(roots != null, "unpickle without previous enterTopLevel")
85-
val rdr = new TreeReader(reader)
86-
if (rdr.isTopLevel) rdr.readTopLevel().head
87-
else rdr.readTerm()
85+
new TreeReader(reader).readTopLevel()
8886
}
8987

9088
class Completer(owner: Symbol, reader: TastyReader) extends LazyType {
@@ -851,19 +849,14 @@ class TreeUnpickler(reader: TastyReader,
851849
}
852850
}
853851

854-
def isTopLevel(implicit ctx: Context): Boolean =
855-
nextByte == IMPORT || nextByte == PACKAGE
852+
def isTopLevel(implicit ctx: Context): Boolean = nextByte == PACKAGE
856853

857-
def readTopLevel()(implicit ctx: Context): List[Tree] = {
858-
@tailrec def read(acc: ListBuffer[Tree]): List[Tree] = {
859-
if (isTopLevel) {
860-
acc += readIndexedStat(NoSymbol)
861-
if (!isAtEnd) read(acc) else acc.toList
862-
}
863-
else // top-level trees which are not imports or packages are not part of tree
864-
acc.toList
865-
}
866-
read(new ListBuffer[tpd.Tree])
854+
/** Read the top level of a tree.
855+
* It may be a package in case of class file tasty or a term if in a term.
856+
*/
857+
def readTopLevel()(implicit ctx: Context): Tree = {
858+
if (isTopLevel) readIndexedStat(NoSymbol)
859+
else readTerm()
867860
}
868861

869862
def readIndexedStat(exprOwner: Symbol)(implicit ctx: Context): Tree = nextByte match {

0 commit comments

Comments
 (0)