Skip to content

Commit a1790eb

Browse files
committed
Merge pull request #550 from dotty-staging/lazy-vals-fixes
Various fixes to LazyVals.
2 parents 36ebabd + de97de5 commit a1790eb

File tree

4 files changed

+37
-23
lines changed

4 files changed

+37
-23
lines changed

src/dotty/runtime/LazyVals.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ object LazyVals {
6060
}.toArray
6161

6262
@inline def getMonitor(obj: Object, fieldId: Int = 0) = {
63-
var id = (java.lang.System.identityHashCode(obj) + fieldId) % base
63+
var id = (
64+
/*java.lang.System.identityHashCode(obj) + */ // should be here, but #548
65+
fieldId) % base
66+
6467
if (id < 0) id += base
6568
monitors(id)
6669
}

src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ class PlainPrinter(_ctx: Context) extends Printer {
4040
def homogenize(tp: Type): Type =
4141
if (homogenizedView)
4242
tp match {
43-
case tp: ThisType if tp.cls.is(Package) && !tp.cls.isEffectiveRoot =>
43+
case tp: ThisType if tp.cls.is(Package) && !tp.cls.isEffectiveRoot =>
4444
ctx.requiredPackage(tp.cls.fullName).termRef
45-
case tp: TypeVar if tp.isInstantiated =>
45+
case tp: TypeVar if tp.isInstantiated =>
4646
homogenize(tp.instanceOpt)
47-
case AndType(tp1, tp2) =>
47+
case AndType(tp1, tp2) =>
4848
homogenize(tp1) & homogenize(tp2)
49-
case OrType(tp1, tp2) =>
49+
case OrType(tp1, tp2) =>
5050
homogenize(tp1) | homogenize(tp2)
5151
case _ =>
5252
val tp1 = tp.simplifyApply
@@ -390,7 +390,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
390390

391391
def toText(const: Constant): Text = const.tag match {
392392
case StringTag => "\"" + escapedString(const.value.toString) + "\""
393-
case ClazzTag => "classOf[" ~ toText(const.tpe.firstBaseArgInfo(defn.ClassClass)) ~ "]"
393+
case ClazzTag => "classOf[" ~ toText(const.typeValue.classSymbol) ~ "]"
394394
case CharTag => s"'${escapedChar(const.charValue)}'"
395395
case LongTag => const.longValue.toString + "L"
396396
case EnumTag => const.symbolValue.name.toString

src/dotty/tools/dotc/transform/Constructors.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ class Constructors extends MiniPhaseTransform with SymTransformer { thisTransfor
3232
override def phaseName: String = "constructors"
3333
override def runsAfter: Set[Class[_ <: Phase]] = Set(classOf[Erasure])
3434

35+
36+
/** All initializers should be moved into constructor
37+
*/
38+
override def checkPostCondition(tree: tpd.Tree)(implicit ctx: Context): Unit = {
39+
tree match {
40+
case t: ValDef if ((t.rhs ne EmptyTree) && !(t.symbol is Flags.Lazy) && t.symbol.owner.isClass) =>
41+
assert(false, i"$t initializers should be moved to constructors")
42+
case _ =>
43+
}
44+
}
45+
3546
/** Symbols that are owned by either <local dummy> or a class field move into the
3647
* primary constructor.
3748
*/

src/dotty/tools/dotc/transform/LazyVals.scala

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,20 @@ class LazyVals extends MiniPhaseTransform with IdentityDenotTransformer {
5959
}
6060
}
6161

62-
/** Append offset fields to companion objects
63-
*/
64-
override def transformTypeDef(tree: TypeDef)(implicit ctx: Context, info: TransformerInfo): Tree = {
65-
if (!tree.symbol.isClass) tree
66-
else {
67-
appendOffsetDefs.get(tree.symbol) match {
68-
case None => tree
69-
case Some(data) =>
70-
val template = tree.rhs.asInstanceOf[Template]
71-
val newTemplate = cpy.Template(template)(body = data.defs ::: template.body)
72-
cpy.TypeDef(tree)(rhs = newTemplate) //(ctx.withMode(Mode.FutureDefsOK))
73-
}
74-
}
62+
63+
/** Append offset fields to companion objects
64+
*/
65+
override def transformTemplate(template: tpd.Template)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = {
66+
val cls = ctx.owner.asClass
67+
68+
appendOffsetDefs.get(cls) match {
69+
case None => template
70+
case Some(data) =>
71+
cpy.Template(template)(body = data.defs ::: template.body)
7572
}
73+
74+
}
75+
7676
/** Replace a local lazy val inside a method,
7777
* with a LazyHolder from
7878
* dotty.runtime(eg dotty.runtime.LazyInt)
@@ -109,10 +109,10 @@ class LazyVals extends MiniPhaseTransform with IdentityDenotTransformer {
109109
tpe)
110110
val initTree = DefDef(initSymbol, initBody)
111111
val holderTree = ValDef(holderSymbol, New(holderImpl.typeRef, List()))
112-
val methodBody = {
113-
tpd.If(flag, EmptyTree, ref(initSymbol))
114-
result.ensureApplied.ensureConforms(tpe)
115-
}
112+
val methodBody = tpd.If(flag.ensureApplied,
113+
result.ensureApplied,
114+
ref(initSymbol).ensureApplied).ensureConforms(tpe)
115+
116116
val methodTree = DefDef(x.symbol.asTerm, methodBody)
117117
ctx.debuglog(s"found a lazy val ${x.show},\n rewrote with ${holderTree.show}")
118118
Thicket(holderTree, initTree, methodTree)

0 commit comments

Comments
 (0)