Skip to content

Commit d9892f9

Browse files
committed
Address review: simplify logic
1 parent ec1de2f commit d9892f9

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

compiler/src/dotty/tools/dotc/transform/init/Semantic.scala

+12-12
Original file line numberDiff line numberDiff line change
@@ -977,10 +977,10 @@ class Semantic {
977977
printer.println(acc.show + " initialized with " + value)
978978
}
979979

980-
// Handler is used to schedule super constructor calls.
980+
// Tasks is used to schedule super constructor calls.
981981
// Super constructor calls are delayed until all outers are set.
982-
type Handler = (() => Unit) => Unit
983-
def superCall(tref: TypeRef, ctor: Symbol, args: List[ArgInfo], source: Tree, handler: Handler)(using Env): Unit =
982+
type Tasks = mutable.ArrayBuffer[() => Unit]
983+
def superCall(tref: TypeRef, ctor: Symbol, args: List[ArgInfo], source: Tree, tasks: Tasks)(using Env): Unit =
984984
val cls = tref.classSymbol.asClass
985985
// update outer for super class
986986
val res = outerValue(tref, thisV, klass, source)
@@ -989,28 +989,29 @@ class Semantic {
989989

990990
// follow constructor
991991
if cls.hasSource then
992-
handler { () =>
992+
tasks.append { () =>
993993
printer.println("init super class " + cls.show)
994994
val res2 = thisV.call(ctor, args, superType = NoType, source)
995995
errorBuffer ++= res2.errors
996+
()
996997
}
997998

998999
// parents
999-
def initParent(parent: Tree, handler: Handler)(using Env) = parent match {
1000+
def initParent(parent: Tree, tasks: Tasks)(using Env) = parent match {
10001001
case tree @ Block(stats, NewExpr(tref, New(tpt), ctor, argss)) => // can happen
10011002
eval(stats, thisV, klass).foreach { res => errorBuffer ++= res.errors }
10021003
val (errors, args) = evalArgs(argss.flatten, thisV, klass)
10031004
errorBuffer ++= errors
1004-
superCall(tref, ctor, args, tree, handler)
1005+
superCall(tref, ctor, args, tree, tasks)
10051006

10061007
case tree @ NewExpr(tref, New(tpt), ctor, argss) => // extends A(args)
10071008
val (errors, args) = evalArgs(argss.flatten, thisV, klass)
10081009
errorBuffer ++= errors
1009-
superCall(tref, ctor, args, tree, handler)
1010+
superCall(tref, ctor, args, tree, tasks)
10101011

10111012
case _ => // extends A or extends A[T]
10121013
val tref = typeRefOf(parent.tpe)
1013-
superCall(tref, tref.classSymbol.primaryConstructor, Nil, parent, handler)
1014+
superCall(tref, tref.classSymbol.primaryConstructor, Nil, parent, tasks)
10141015
}
10151016

10161017
// see spec 5.1 about "Template Evaluation".
@@ -1020,13 +1021,12 @@ class Semantic {
10201021

10211022
// outers are set first
10221023
val tasks = new mutable.ArrayBuffer[() => Unit]
1023-
val handler: Handler = task => tasks.append(task)
10241024

10251025
// 1. first init parent class recursively
10261026
// 2. initialize traits according to linearization order
10271027
val superParent = tpl.parents.head
10281028
val superCls = superParent.tpe.classSymbol.asClass
1029-
initParent(superParent, handler)
1029+
initParent(superParent, tasks)
10301030

10311031
val parents = tpl.parents.tail
10321032
val mixins = klass.baseClasses.tail.takeWhile(_ != superCls)
@@ -1038,7 +1038,7 @@ class Semantic {
10381038
// calls and user code in the class body.
10391039
mixins.reverse.foreach { mixin =>
10401040
parents.find(_.tpe.classSymbol == mixin) match
1041-
case Some(parent) => initParent(parent, handler)
1041+
case Some(parent) => initParent(parent, tasks)
10421042
case None =>
10431043
// According to the language spec, if the mixin trait requires
10441044
// arguments, then the class must provide arguments to it explicitly
@@ -1049,7 +1049,7 @@ class Semantic {
10491049
// term arguments to B. That can only be done in a concrete class.
10501050
val tref = typeRefOf(klass.typeRef.baseType(mixin).typeConstructor)
10511051
val ctor = tref.classSymbol.primaryConstructor
1052-
if ctor.exists then superCall(tref, ctor, Nil, superParent, handler)
1052+
if ctor.exists then superCall(tref, ctor, Nil, superParent, tasks)
10531053
}
10541054

10551055
// initialize super classes after outers are set

0 commit comments

Comments
 (0)