Skip to content

Commit 4c5b6ef

Browse files
committed
TASTY 3.0: Distinguish SHARED terms from types
In general, this makes things easier to debug at virtually no cost. It also makes it much easier to properly set positions on unpickled TypeTrees: in TreeUnpickler#readTpt we can know make sure that the position of the TypeTree is unpickled from the address found after following all shared terms but before following any shared type. Note that the major version bump could have been avoided by adding some compatibility code, but that doesn't seem worthwhile until Tasty is used in the wild.
1 parent a6394e9 commit 4c5b6ef

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

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

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Standard-Section: "ASTs" TopLevelStat*
108108
ORtpt Length left_Term right_Term
109109
BYNAMEtpt underlying_Term
110110
EMPTYTREE
111-
SHARED term_ASTRef
111+
SHAREDterm term_ASTRef
112112
HOLE Length idx_Nat arg_Tree*
113113
Application = APPLY Length fn_Term arg_Term*
114114
@@ -125,7 +125,7 @@ Standard-Section: "ASTs" TopLevelStat*
125125
TERMREF possiblySigned_NameRef qual_Type
126126
THIS clsRef_Type
127127
RECthis recType_ASTRef
128-
SHARED path_ASTRef
128+
SHAREDtype path_ASTRef
129129
130130
Constant = UNITconst
131131
FALSEconst
@@ -165,7 +165,7 @@ Standard-Section: "ASTs" TopLevelStat*
165165
POLYtype Length result_Type NamesTypes
166166
METHODtype Length result_Type NamesTypes // needed for refinements
167167
TYPELAMBDAtype Length result_Type NamesTypes // variance encoded in front of name: +/-/(nothing)
168-
SHARED type_ASTRef
168+
SHAREDtype type_ASTRef
169169
NamesTypes = NameType*
170170
NameType = paramName_NameRef typeOrBounds_ASTRef
171171
@@ -226,8 +226,8 @@ Standard Section: "Positions" Assoc*
226226
object TastyFormat {
227227

228228
final val header = Array(0x5C, 0xA1, 0xAB, 0x1F)
229-
val MajorVersion = 2
230-
val MinorVersion = 1
229+
val MajorVersion = 3
230+
val MinorVersion = 0
231231

232232
/** Tags used to serialize names */
233233
class NameTags {
@@ -302,23 +302,24 @@ object TastyFormat {
302302

303303
// Cat. 2: tag Nat
304304

305-
final val SHARED = 50
306-
final val TERMREFdirect = 51
307-
final val TYPEREFdirect = 52
308-
final val TERMREFpkg = 53
309-
final val TYPEREFpkg = 54
310-
final val RECthis = 55
311-
final val BYTEconst = 56
312-
final val SHORTconst = 57
313-
final val CHARconst = 58
314-
final val INTconst = 59
315-
final val LONGconst = 60
316-
final val FLOATconst = 61
317-
final val DOUBLEconst = 62
318-
final val STRINGconst = 63
319-
final val IMPORTED = 64
320-
final val RENAMED = 65
321-
final val SYMBOLconst = 66
305+
final val SHAREDterm = 50
306+
final val SHAREDtype = 51
307+
final val TERMREFdirect = 52
308+
final val TYPEREFdirect = 53
309+
final val TERMREFpkg = 54
310+
final val TYPEREFpkg = 55
311+
final val RECthis = 56
312+
final val BYTEconst = 57
313+
final val SHORTconst = 58
314+
final val CHARconst = 59
315+
final val INTconst = 60
316+
final val LONGconst = 61
317+
final val FLOATconst = 62
318+
final val DOUBLEconst = 63
319+
final val STRINGconst = 64
320+
final val IMPORTED = 65
321+
final val RENAMED = 66
322+
final val SYMBOLconst = 67
322323

323324
// Cat. 3: tag AST
324325

@@ -402,7 +403,7 @@ object TastyFormat {
402403
final val HOLE = 255
403404

404405
final val firstSimpleTreeTag = UNITconst
405-
final val firstNatTreeTag = SHARED
406+
final val firstNatTreeTag = SHAREDterm
406407
final val firstASTTreeTag = THIS
407408
final val firstNatASTTreeTag = IDENT
408409
final val firstLengthTreeTag = PACKAGE
@@ -492,7 +493,8 @@ object TastyFormat {
492493
case DEFAULTparameterized => "DEFAULTparameterized"
493494
case STABLE => "STABLE"
494495

495-
case SHARED => "SHARED"
496+
case SHAREDterm => "SHAREDterm"
497+
case SHAREDtype => "SHAREDtype"
496498
case TERMREFdirect => "TERMREFdirect"
497499
case TYPEREFdirect => "TYPEREFdirect"
498500
case TERMREFpkg => "TERMREFpkg"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class TreePickler(pickler: TastyPickler) {
143143
pickleNewType(tpe, richTypes)
144144
}
145145
else {
146-
writeByte(SHARED)
146+
writeByte(SHAREDtype)
147147
writeRef(prev.asInstanceOf[Addr])
148148
}
149149
} catch {
@@ -330,7 +330,7 @@ class TreePickler(pickler: TastyPickler) {
330330
def pickleTree(tree: Tree)(implicit ctx: Context): Unit = {
331331
val addr = registerTreeAddr(tree)
332332
if (addr != currentAddr) {
333-
writeByte(SHARED)
333+
writeByte(SHAREDterm)
334334
writeRef(addr)
335335
}
336336
else

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class TreeUnpickler(reader: TastyReader,
152152
/** The next tag, following through SHARED tags */
153153
def nextUnsharedTag: Int = {
154154
val tag = nextByte
155-
if (tag == SHARED) {
155+
if (tag == SHAREDtype || tag == SHAREDterm) {
156156
val lookAhead = fork
157157
lookAhead.reader.readByte()
158158
forkAt(lookAhead.reader.readAddr()).nextUnsharedTag
@@ -321,7 +321,7 @@ class TreeUnpickler(reader: TastyReader,
321321
readTypeRef().asInstanceOf[RecType].recThis
322322
case TYPEALIAS =>
323323
TypeAlias(readType())
324-
case SHARED =>
324+
case SHAREDtype =>
325325
val ref = readAddr()
326326
typeAtAddr.getOrElseUpdate(ref, forkAt(ref).readType())
327327
case UNITconst =>
@@ -899,7 +899,7 @@ class TreeUnpickler(reader: TastyReader,
899899
}
900900

901901
def readSimpleTerm(): Tree = tag match {
902-
case SHARED =>
902+
case SHAREDterm =>
903903
forkAt(readAddr()).readTerm()
904904
case IDENT =>
905905
untpd.Ident(readName()).withType(readType())
@@ -1051,16 +1051,16 @@ class TreeUnpickler(reader: TastyReader,
10511051
}
10521052

10531053
def readTpt()(implicit ctx: Context) =
1054-
if (isTypeTreeTag(nextUnsharedTag)) readTerm()
1054+
if (nextByte == SHAREDterm || isTypeTreeTag(nextUnsharedTag)) readTerm()
10551055
else {
10561056
val start = currentAddr
10571057
val tp = readType()
10581058
if (tp.exists) setPos(start, TypeTree(tp)) else EmptyTree
10591059
}
10601060

10611061
def readCases(end: Addr)(implicit ctx: Context): List[CaseDef] =
1062-
collectWhile((nextByte == CASEDEF || nextByte == SHARED) && currentAddr != end) {
1063-
if (nextByte == SHARED) {
1062+
collectWhile((nextByte == CASEDEF || nextByte == SHAREDterm) && currentAddr != end) {
1063+
if (nextByte == SHAREDterm) {
10641064
readByte()
10651065
forkAt(readAddr()).readCase()(ctx.fresh.setNewScope)
10661066
}

0 commit comments

Comments
 (0)