Skip to content

Commit 0ec4d98

Browse files
committed
Implement pickling and unpickling of computed types
1 parent de2b06a commit 0ec4d98

File tree

5 files changed

+46
-20
lines changed

5 files changed

+46
-20
lines changed

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ trait UntypedTreeInfo extends TreeInfo[Untyped] { self: Trees.Instance[Untyped]
291291
case _ => tree
292292
}
293293

294+
def isBounds(tree: Tree)(implicit ctx: Context) = tree match {
295+
case tree: TypeBoundsTree => true
296+
case TypedSplice(tree1) => tree1.tpe.isInstanceOf[TypeBounds]
297+
case _ => false
298+
}
299+
294300
/** True iff definition is a val or def with no right-hand-side, or it
295301
* is an abstract typoe declaration
296302
*/

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ Standard-Section: "ASTs" TopLevelStat*
5858
VALDEF Length NameRef type_Term rhs_Term? Modifier*
5959
DEFDEF Length NameRef TypeParam* Params* returnType_Term rhs_Term?
6060
Modifier*
61-
TYPEDEF Length NameRef (type_Term | Template) Modifier*
61+
TYPEDEF Length NameRef
62+
( type_Term
63+
| Template
64+
| TypeParam* Params* returnType_Term rhs_Term
65+
) Modifier*
6266
OBJECTDEF Length NameRef Template Modifier*
6367
IMPORT Length qual_Term Selector*
6468
Selector = IMPORTED name_NameRef
@@ -510,7 +514,8 @@ object TastyFormat {
510514
| ANDtpt
511515
| ORtpt
512516
| BYNAMEtpt
513-
| BIND => true
517+
| BIND
518+
| APPLY => true
514519
case _ => false
515520
}
516521

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,8 @@ class TreePickler(pickler: TastyPickler) {
469469
withLength { pickleParams(vparams) }
470470
}
471471
}
472-
pickleDef(DEFDEF, tree.symbol, tree.tpt, tree.rhs, pickleAllParams)
472+
val tag = if (tree.symbol.isTerm) DEFDEF else TYPEDEF
473+
pickleDef(tag, tree.symbol, tree.tpt, tree.rhs, pickleAllParams)
473474
case tree: TypeDef =>
474475
pickleDef(TYPEDEF, tree.symbol, tree.rhs)
475476
case tree: Template =>

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

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ class TreeUnpickler(reader: TastyReader,
438438
var flags = givenFlags
439439
if (lacksDefinition && tag != PARAM) flags |= Deferred
440440
if (tag == DEFDEF) flags |= Method
441+
if (tag == TYPEDEF && givenFlags.is(Transparent)) flags |= TypeMethod
441442
if (givenFlags is Module)
442443
flags = flags | (if (tag == VALDEF) ModuleValCreationFlags else ModuleClassCreationFlags)
443444
if (ctx.owner.isClass) {
@@ -754,28 +755,33 @@ class TreeUnpickler(reader: TastyReader,
754755
ta.assignType(untpd.ValDef(sym.name.asTermName, tpt, readRhs(localCtx)), sym)
755756

756757
def DefDef(tparams: List[TypeDef], vparamss: List[List[ValDef]], tpt: Tree) =
757-
ta.assignType(
758-
untpd.DefDef(sym.name.asTermName, tparams, vparamss, tpt, readRhs(localCtx)),
759-
sym)
758+
ta.assignType(
759+
untpd.DefDef(sym.name, tparams, vparamss, tpt, readRhs(localCtx)),
760+
sym)
760761

761762
def TypeDef(rhs: Tree) =
762763
ta.assignType(untpd.TypeDef(sym.name.asTypeName, rhs), sym)
763764

764765
def ta = ctx.typeAssigner
765766

766767
val name = readName()
768+
769+
def readDefDef() = {
770+
val tparams = readParams[TypeDef](TYPEPARAM)(localCtx)
771+
val vparamss = readParamss(localCtx)
772+
val tpt = readTpt()(localCtx)
773+
val typeParams = tparams.map(_.symbol.asType)
774+
val valueParamss = ctx.normalizeIfConstructor(
775+
vparamss.nestedMap(_.symbol.asTerm), name == nme.CONSTRUCTOR)
776+
val resType = ctx.effectiveResultType(sym, typeParams, tpt.tpe)
777+
sym.info = ctx.lambdaType(typeParams, valueParamss, resType, sym)
778+
DefDef(tparams, vparamss, tpt)
779+
}
780+
767781
pickling.println(s"reading def of $name at $start")
768782
val tree: MemberDef = tag match {
769783
case DEFDEF =>
770-
val tparams = readParams[TypeDef](TYPEPARAM)(localCtx)
771-
val vparamss = readParamss(localCtx)
772-
val tpt = readTpt()(localCtx)
773-
val typeParams = tparams.map(_.symbol.asType)
774-
val valueParamss = ctx.normalizeIfConstructor(
775-
vparamss.nestedMap(_.symbol.asTerm), name == nme.CONSTRUCTOR)
776-
val resType = ctx.effectiveResultType(sym, typeParams, tpt.tpe)
777-
sym.info = ctx.termLambda(typeParams, valueParamss, resType, sym)
778-
DefDef(tparams, vparamss, tpt)
784+
readDefDef()
779785
case VALDEF =>
780786
val tpt = readTpt()(localCtx)
781787
sym.info = tpt.tpe
@@ -795,7 +801,10 @@ class TreeUnpickler(reader: TastyReader,
795801
else sym.registerCompanionMethod(nme.COMPANION_MODULE_METHOD, companion)
796802
}
797803
TypeDef(readTemplate(localCtx))
798-
} else {
804+
}
805+
else if (sym.is(Transparent))
806+
readDefDef()
807+
else {
799808
sym.info = TypeBounds.empty // needed to avoid cyclic references when unpicklin rhs, see i3816.scala
800809
sym.setFlag(Provisional)
801810
val rhs = readTpt()(localCtx)
@@ -1337,8 +1346,13 @@ class TreeUnpickler(reader: TastyReader,
13371346
case ALTERNATIVE =>
13381347
untpd.Alternative(until(end)(readUntyped()))
13391348
case DEFDEF =>
1340-
untpd.DefDef(readName(), readParams[TypeDef](TYPEPARAM), readParamss(), readUntyped(), readRhs())
1341-
.withMods(readMods())
1349+
var name = readName()
1350+
val tparams = readParams[TypeDef](TYPEPARAM)
1351+
val vparamss = readParamss()
1352+
val tpt = readUntyped()
1353+
untpd.DefDef(
1354+
if (untpd.isBounds(tpt)) name.toTypeName else name,
1355+
tparams, vparamss, tpt, readRhs()).withMods(readMods())
13421356
case VALDEF | PARAM =>
13431357
untpd.ValDef(readName(), readUntyped(), readRhs())
13441358
.withMods(readMods())

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ trait NamerContextOps { this: Context =>
131131
termParamss
132132

133133
/** The method type corresponding to given parameters and result type */
134-
def termLambda(typeParams: List[TypeSymbol], valueParamss: List[List[TermSymbol]], resultType: Type, sym: Symbol)(implicit ctx: Context): Type = {
134+
def lambdaType(typeParams: List[TypeSymbol], valueParamss: List[List[TermSymbol]], resultType: Type, sym: Symbol)(implicit ctx: Context): Type = {
135135
val monotpe =
136136
(valueParamss :\ resultType) { (params, resultType) =>
137137
if (sym.isTerm) {
@@ -1247,7 +1247,7 @@ class Namer { typer: Typer =>
12471247
def wrapMethType(restpe: Type): Type = {
12481248
if (sym.isType) assert(restpe.isInstanceOf[TypeBounds], restpe)
12491249
instantiateDependent(restpe, typeParams, termParamss)
1250-
ctx.termLambda(typeParams, termParamss, restpe, sym)
1250+
ctx.lambdaType(typeParams, termParamss, restpe, sym)
12511251
}
12521252
if (isConstructor) {
12531253
// set result type tree to unit, but take the current class as result type of the symbol

0 commit comments

Comments
 (0)