Skip to content

Commit 7a3146a

Browse files
committed
Make withDefinedSyms iterative
1 parent 02f2688 commit 7a3146a

File tree

2 files changed

+30
-62
lines changed

2 files changed

+30
-62
lines changed

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,6 @@ object Decorators {
112112
else x1 :: xs1
113113
}
114114

115-
def foldRightBN[U](z: => U)(op: (T, => U) => U): U = {
116-
@tailrec def foldLeftBN(xs: List[T], acc: => U): U = xs match {
117-
case x :: xs1 => foldLeftBN(xs1, op(x, acc))
118-
case Nil => acc
119-
}
120-
foldLeftBN(xs.reverse, z)
121-
}
122-
123115
final def hasSameLengthAs[U](ys: List[U]): Boolean = {
124116
@tailrec def loop(xs: List[T], ys: List[U]): Boolean =
125117
if (xs.isEmpty) ys.isEmpty

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

Lines changed: 30 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -147,52 +147,41 @@ class TreeChecker extends Phase with SymTransformer {
147147
// don't check value classes after typer, as the constraint about constructors doesn't hold after transform
148148
override def checkDerivedValueClass(clazz: Symbol, stats: List[Tree])(implicit ctx: Context) = ()
149149

150-
def withDefinedSym[T](tree: untpd.Tree)(op: => T)(implicit ctx: Context): T = tree match {
151-
case tree: untpd.DefTree =>
152-
preWithDefinedSym(tree)
153-
postWithDefinedSym(tree)(op)
154-
case _ => op
155-
}
156-
157-
private def preWithDefinedSym[T](tree: untpd.DefTree)(implicit ctx: Context): Unit = {
158-
val sym = tree.symbol
159-
assert(isValidJVMName(sym.name.encode), s"${sym.name.debugString} name is invalid on jvm")
160-
everDefinedSyms.get(sym) match {
161-
case Some(t) =>
162-
if (t ne tree)
163-
ctx.warning(i"symbol ${sym.fullName} is defined at least twice in different parts of AST")
164-
// should become an error
165-
case None =>
166-
everDefinedSyms(sym) = tree
167-
}
168-
assert(!nowDefinedSyms.contains(sym), i"doubly defined symbol: ${sym.fullName} in $tree")
169-
170-
if (ctx.settings.YcheckMods.value) {
150+
def withDefinedSyms[T](trees: List[untpd.Tree])(op: => T)(implicit ctx: Context) = {
151+
var locally = List.empty[Symbol]
152+
for (tree <- trees) {
153+
val sym = tree.symbol
171154
tree match {
172-
case t: untpd.MemberDef =>
173-
if (t.name ne sym.name) ctx.warning(s"symbol ${sym.fullName} name doesn't correspond to AST: ${t}")
174-
// todo: compare trees inside annotations
155+
case tree: untpd.DefTree =>
156+
assert(isValidJVMName(sym.name.encode), s"${sym.name.debugString} name is invalid on jvm")
157+
everDefinedSyms.get(sym) match {
158+
case Some(t) =>
159+
if (t ne tree)
160+
ctx.warning(i"symbol ${sym.fullName} is defined at least twice in different parts of AST")
161+
// should become an error
162+
case None =>
163+
everDefinedSyms(sym) = tree
164+
}
165+
assert(!nowDefinedSyms.contains(sym), i"doubly defined symbol: ${sym.fullName} in $tree")
166+
167+
if (ctx.settings.YcheckMods.value) {
168+
tree match {
169+
case t: untpd.MemberDef =>
170+
if (t.name ne sym.name) ctx.warning(s"symbol ${sym.fullName} name doesn't correspond to AST: ${t}")
171+
// todo: compare trees inside annotations
172+
case _ =>
173+
}
174+
}
175+
locally = sym :: locally
176+
nowDefinedSyms += sym
175177
case _ =>
176178
}
177179
}
178-
}
179-
180-
private def postWithDefinedSym[T](tree: untpd.DefTree)(op: => T)(implicit ctx: Context): T = {
181-
val sym = tree.symbol
182-
nowDefinedSyms += sym
183-
//ctx.echo(i"defined: ${tree.symbol}")
184180
val res = op
185-
nowDefinedSyms -= sym
186-
//ctx.echo(i"undefined: ${tree.symbol}")
181+
nowDefinedSyms --= locally
187182
res
188183
}
189184

190-
def withDefinedSyms[T](trees: List[untpd.Tree])(op: => T)(implicit ctx: Context) =
191-
trees.foldRightBN(op)(withDefinedSym(_)(_))
192-
193-
def withDefinedSymss[T](vparamss: List[List[untpd.ValDef]])(op: => T)(implicit ctx: Context): T =
194-
vparamss.foldRightBN(op)(withDefinedSyms(_)(_))
195-
196185
def assertDefined(tree: untpd.Tree)(implicit ctx: Context) =
197186
if (
198187
tree.symbol.maybeOwner.isTerm &&
@@ -396,7 +385,7 @@ class TreeChecker extends Phase with SymTransformer {
396385

397386
override def typedDefDef(ddef: untpd.DefDef, sym: Symbol)(implicit ctx: Context) =
398387
withDefinedSyms(ddef.tparams) {
399-
withDefinedSymss(ddef.vparamss) {
388+
withDefinedSyms(ddef.vparamss.flatten) {
400389
if (!sym.isClassConstructor && !(sym.name eq nme.STATIC_CONSTRUCTOR))
401390
assert(isValidJVMMethodName(sym.name.encode), s"${sym.name.debugString} name is invalid on jvm")
402391

@@ -419,21 +408,8 @@ class TreeChecker extends Phase with SymTransformer {
419408
}
420409
}
421410

422-
override def typedBlock(tree: untpd.Block, pt: Type)(implicit ctx: Context) = {
423-
var locally = List.empty[Symbol]
424-
for (stat <- tree.stats) {
425-
stat match {
426-
case stat: untpd.DefTree =>
427-
preWithDefinedSym(stat)
428-
locally = stat.symbol :: locally
429-
nowDefinedSyms += stat.symbol
430-
case _ =>
431-
}
432-
}
433-
val res = super.typedBlock(tree, pt)
434-
nowDefinedSyms --= locally
435-
res
436-
}
411+
override def typedBlock(tree: untpd.Block, pt: Type)(implicit ctx: Context) =
412+
withDefinedSyms(tree.stats) { super.typedBlock(tree, pt) }
437413

438414
override def typedInlined(tree: untpd.Inlined, pt: Type)(implicit ctx: Context) =
439415
withDefinedSyms(tree.bindings) { super.typedInlined(tree, pt) }

0 commit comments

Comments
 (0)