Skip to content

Commit 64f1d11

Browse files
committed
Fix missing CompilationUnit on class symbol loaded in TreeUnpickler
Also refactor accesses to the tasty attributes to get them directly from the compilation unit info instead of the unpickler.
1 parent 135e97f commit 64f1d11

File tree

7 files changed

+40
-40
lines changed

7 files changed

+40
-40
lines changed

compiler/src/dotty/tools/dotc/core/SymbolLoaders.scala

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -420,20 +420,9 @@ class TastyLoader(val tastyFile: AbstractFile) extends SymbolLoader {
420420
private val unpickler: tasty.DottyUnpickler =
421421
handleUnpicklingExceptions:
422422
val tastyBytes = tastyFile.toByteArray
423-
new tasty.DottyUnpickler(tastyBytes) // reads header and name table
424-
425-
val compilationUnitInfo: CompilationUnitInfo | Null =
426-
val tastyHeader = unpickler.unpickler.header
427-
val tastyVersion = TastyVersion(
428-
tastyHeader.majorVersion,
429-
tastyHeader.minorVersion,
430-
tastyHeader.experimentalVersion,
431-
)
432-
val attributes = unpickler.tastyAttributes
433-
new CompilationUnitInfo(
434-
tastyFile,
435-
tastyInfo = Some(TastyInfo(tastyVersion, attributes)),
436-
)
423+
new tasty.DottyUnpickler(tastyFile, tastyBytes) // reads header and name table
424+
425+
val compilationUnitInfo: CompilationUnitInfo | Null = unpickler.compilationUnitInfo
437426

438427
def description(using Context): String = "TASTy file " + tastyFile.toString
439428

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ import dotty.tools.tasty.TastyReader
1616
import dotty.tools.tasty.TastyFormat.{ASTsSection, PositionsSection, CommentsSection, AttributesSection}
1717
import dotty.tools.tasty.TastyVersion
1818

19+
import dotty.tools.io.AbstractFile
20+
1921
object DottyUnpickler {
2022

2123
/** Exception thrown if classfile is corrupted */
2224
class BadSignature(msg: String) extends RuntimeException(msg)
2325

24-
class TreeSectionUnpickler(posUnpickler: Option[PositionUnpickler], commentUnpickler: Option[CommentUnpickler], attributeUnpickler: Option[AttributeUnpickler])
26+
class TreeSectionUnpickler(compilationUnitInfo: CompilationUnitInfo, posUnpickler: Option[PositionUnpickler], commentUnpickler: Option[CommentUnpickler])
2527
extends SectionUnpickler[TreeUnpickler](ASTsSection) {
2628
def unpickle(reader: TastyReader, nameAtRef: NameTable): TreeUnpickler =
27-
new TreeUnpickler(reader, nameAtRef, posUnpickler, commentUnpickler, attributeUnpickler)
29+
new TreeUnpickler(reader, nameAtRef, compilationUnitInfo, posUnpickler, commentUnpickler)
2830
}
2931

3032
class PositionsSectionUnpickler extends SectionUnpickler[PositionUnpickler](PositionsSection) {
@@ -44,21 +46,28 @@ object DottyUnpickler {
4446
}
4547

4648
/** A class for unpickling Tasty trees and symbols.
49+
* @param tastyFile tasty file from which we unpickle (used for CompilationUnitInfo)
4750
* @param bytes the bytearray containing the Tasty file from which we unpickle
4851
* @param mode the tasty file contains package (TopLevel), an expression (Term) or a type (TypeTree)
4952
*/
50-
class DottyUnpickler(bytes: Array[Byte], mode: UnpickleMode = UnpickleMode.TopLevel) extends ClassfileParser.Embedded with tpd.TreeProvider {
53+
class DottyUnpickler(tastyFile: AbstractFile, bytes: Array[Byte], mode: UnpickleMode = UnpickleMode.TopLevel) extends ClassfileParser.Embedded with tpd.TreeProvider {
5154
import tpd.*
5255
import DottyUnpickler.*
5356

5457
val unpickler: TastyUnpickler = new TastyUnpickler(bytes)
58+
59+
val tastyAttributes: Attributes =
60+
unpickler.unpickle(new AttributesSectionUnpickler)
61+
.map(_.attributes).getOrElse(Attributes.empty)
62+
val compilationUnitInfo: CompilationUnitInfo =
63+
import unpickler.header.{majorVersion, minorVersion, experimentalVersion}
64+
val tastyVersion = TastyVersion(majorVersion, minorVersion, experimentalVersion)
65+
val tastyInfo = TastyInfo(tastyVersion, tastyAttributes)
66+
new CompilationUnitInfo(tastyFile, Some(tastyInfo))
67+
5568
private val posUnpicklerOpt = unpickler.unpickle(new PositionsSectionUnpickler)
5669
private val commentUnpicklerOpt = unpickler.unpickle(new CommentsSectionUnpickler)
57-
private val attributeUnpicklerOpt = unpickler.unpickle(new AttributesSectionUnpickler)
58-
private val treeUnpickler = unpickler.unpickle(treeSectionUnpickler(posUnpicklerOpt, commentUnpicklerOpt, attributeUnpicklerOpt)).get
59-
60-
def tastyAttributes: Attributes =
61-
attributeUnpicklerOpt.map(_.attributes).getOrElse(Attributes.empty)
70+
private val treeUnpickler = unpickler.unpickle(treeSectionUnpickler(posUnpicklerOpt, commentUnpicklerOpt)).get
6271

6372
/** Enter all toplevel classes and objects into their scopes
6473
* @param roots a set of SymDenotations that should be overwritten by unpickling
@@ -69,9 +78,8 @@ class DottyUnpickler(bytes: Array[Byte], mode: UnpickleMode = UnpickleMode.TopLe
6978
protected def treeSectionUnpickler(
7079
posUnpicklerOpt: Option[PositionUnpickler],
7180
commentUnpicklerOpt: Option[CommentUnpickler],
72-
attributeUnpicklerOpt: Option[AttributeUnpickler]
7381
): TreeSectionUnpickler =
74-
new TreeSectionUnpickler(posUnpicklerOpt, commentUnpicklerOpt, attributeUnpicklerOpt)
82+
new TreeSectionUnpickler(compilationUnitInfo, posUnpicklerOpt, commentUnpicklerOpt)
7583

7684
protected def computeRootTrees(using Context): List[Tree] = treeUnpickler.unpickle(mode)
7785

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,16 @@ import scala.compiletime.uninitialized
4949

5050
/** Unpickler for typed trees
5151
* @param reader the reader from which to unpickle
52+
* @param compilationUnitInfo the compilation unit info of the TASTy
5253
* @param posUnpicklerOpt the unpickler for positions, if it exists
5354
* @param commentUnpicklerOpt the unpickler for comments, if it exists
5455
* @param attributeUnpicklerOpt the unpickler for attributes, if it exists
5556
*/
5657
class TreeUnpickler(reader: TastyReader,
5758
nameAtRef: NameTable,
59+
compilationUnitInfo: CompilationUnitInfo,
5860
posUnpicklerOpt: Option[PositionUnpickler],
59-
commentUnpicklerOpt: Option[CommentUnpickler],
60-
attributeUnpicklerOpt: Option[AttributeUnpickler]) {
61+
commentUnpicklerOpt: Option[CommentUnpickler]) {
6162
import TreeUnpickler.*
6263
import tpd.*
6364

@@ -92,22 +93,21 @@ class TreeUnpickler(reader: TastyReader,
9293
/** The root owner tree. See `OwnerTree` class definition. Set by `enterTopLevel`. */
9394
private var ownerTree: OwnerTree = uninitialized
9495

96+
/** TASTy attributes */
97+
private val attributes: Attributes = compilationUnitInfo.tastyInfo.get.attributes
98+
9599
/** Was unpickled class compiled with capture checks? */
96-
private val withCaptureChecks: Boolean =
97-
attributeUnpicklerOpt.exists(_.attributes.captureChecked)
100+
private val withCaptureChecks: Boolean = attributes.captureChecked
98101

99-
private val unpicklingScala2Library =
100-
attributeUnpicklerOpt.exists(_.attributes.scala2StandardLibrary)
102+
private val unpicklingScala2Library = attributes.scala2StandardLibrary
101103

102104
/** This dependency was compiled with explicit nulls enabled */
103105
// TODO Use this to tag the symbols of this dependency as compiled with explicit nulls (see use of unpicklingScala2Library).
104-
private val explicitNulls =
105-
attributeUnpicklerOpt.exists(_.attributes.explicitNulls)
106+
private val explicitNulls = attributes.explicitNulls
106107

107-
private val unpicklingJava =
108-
attributeUnpicklerOpt.exists(_.attributes.isJava)
108+
private val unpicklingJava = attributes.isJava
109109

110-
private val isOutline = attributeUnpicklerOpt.exists(_.attributes.isOutline)
110+
private val isOutline = attributes.isOutline
111111

112112
private def registerSym(addr: Addr, sym: Symbol) =
113113
symAtAddr(addr) = sym
@@ -636,8 +636,8 @@ class TreeUnpickler(reader: TastyReader,
636636
rootd.symbol
637637
case _ =>
638638
val completer = adjustIfModule(new Completer(subReader(start, end)))
639-
if (isClass)
640-
newClassSymbol(ctx.owner, name.asTypeName, flags, completer, privateWithin, coord)
639+
if isClass then
640+
newClassSymbol(ctx.owner, name.asTypeName, flags, completer, privateWithin, coord, compilationUnitInfo)
641641
else
642642
newSymbol(ctx.owner, name, flags, completer, privateWithin, coord)
643643
}

compiler/src/dotty/tools/dotc/fromtasty/ReadTasty.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Denotations.staticRef
1111
import NameOps.*
1212
import ast.Trees.Tree
1313
import Phases.Phase
14+
import core.tasty.Attributes
1415

1516
/** Load trees from TASTY files */
1617
class ReadTasty extends Phase {

compiler/src/dotty/tools/dotc/quoted/PickledQuotes.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import scala.quoted.runtime.impl.*
2121
import scala.collection.mutable
2222

2323
import QuoteUtils.*
24+
import dotty.tools.io.NoAbstractFile
2425

2526
object PickledQuotes {
2627
import tpd.*
@@ -268,7 +269,7 @@ object PickledQuotes {
268269
quotePickling.println(s"**** unpickling quote from TASTY\n${TastyPrinter.showContents(bytes, ctx.settings.color.value == "never")}")
269270

270271
val mode = if (isType) UnpickleMode.TypeTree else UnpickleMode.Term
271-
val unpickler = new DottyUnpickler(bytes, mode)
272+
val unpickler = new DottyUnpickler(NoAbstractFile, bytes, mode)
272273
unpickler.enter(Set.empty)
273274

274275
val tree = unpickler.tree

compiler/src/dotty/tools/dotc/transform/Pickler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ class Pickler extends Phase {
235235
ctx.initialize()
236236
val unpicklers =
237237
for ((cls, (unit, bytes)) <- pickledBytes) yield {
238-
val unpickler = new DottyUnpickler(bytes)
238+
val unpickler = new DottyUnpickler(unit.source.file, bytes)
239239
unpickler.enter(roots = Set.empty)
240240
cls -> (unit, unpickler)
241241
}

compiler/test/dotty/tools/dotc/core/tasty/CommentPicklingTest.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import dotty.tools.vulpix.TestConfiguration
1818

1919
import org.junit.Test
2020
import org.junit.Assert.{assertEquals, assertFalse, fail}
21+
import dotty.tools.io.AbstractFile
2122

2223
class CommentPicklingTest {
2324

@@ -116,7 +117,7 @@ class CommentPicklingTest {
116117
implicit val ctx: Context = setup(args, initCtx).map(_._2).getOrElse(initCtx)
117118
ctx.initialize()
118119
val trees = files.flatMap { f =>
119-
val unpickler = new DottyUnpickler(f.toByteArray())
120+
val unpickler = new DottyUnpickler(AbstractFile.getFile(f.jpath), f.toByteArray())
120121
unpickler.enter(roots = Set.empty)
121122
unpickler.rootTrees(using ctx)
122123
}

0 commit comments

Comments
 (0)