Skip to content

Commit 2acfbf8

Browse files
committed
Remove DoWhile from tasty-reflect.
This avoids the awkward situation where `WhileDo` needs to be tested before `While` for it to meaningful, or suffering some more performance hit by rejecting `do-while`-like stuff in the `While` deconstructor. It will probably also simplify most usage patterns of the tasty-reflect API, since users will only have to worry about one kind of loop. The decompiler takes it upon itself to deconstruct `While` loops that look like the product of `do..while` to print a nice decompilation output.
1 parent 3bc7893 commit 2acfbf8

File tree

3 files changed

+10
-22
lines changed

3 files changed

+10
-22
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/TreeOpsImpl.scala

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,6 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with TastyCoreImpl with He
378378
case _ => None
379379
}
380380
}
381-
382-
object DoWhile extends DoWhileExtractor {
383-
def unapply(x: Term)(implicit ctx: Context): Option[(Term, Term)] = x match {
384-
case Trees.WhileDo(Trees.Block(Trees.Block(Nil, body) :: Nil, Trees.Block(Nil, cond)), Trees.Literal(Constants.Constant(()))) =>
385-
Some((body, cond))
386-
case _ => None
387-
}
388-
}
389381
}
390382

391383
def termAsParent(term: Term): Parent = term

library/src/scala/tasty/reflect/TreeOps.scala

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,6 @@ trait TreeOps extends TastyCore {
298298
/** Extractor for while loops. Matches `while (<cond>) <body>` and returns (<cond>, <body>) */
299299
def unapply(tree: Tree)(implicit ctx: Context): Option[(Term, Term)]
300300
}
301-
302-
val DoWhile: DoWhileExtractor
303-
abstract class DoWhileExtractor {
304-
/** Extractor for do while loops. Matches `do <body> while (<cond>)` and returns (<body>, <cond>) */
305-
def unapply(tree: Tree)(implicit ctx: Context): Option[(Term, Term)]
306-
}
307301
}
308302

309303
implicit def termAsParent(term: Term): Parent

library/src/scala/tasty/util/ShowSourceCode.scala

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,17 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
227227
this
228228
}
229229

230-
case Term.DoWhile(body, cond) => // DoWhile must come before While
231-
this += "do "
232-
printTree(body) += " while "
233-
inParens(printTree(cond))
234-
235230
case Term.While(cond, body) =>
236-
this += "while "
237-
inParens(printTree(cond)) += " "
238-
printTree(body)
231+
(cond, body) match {
232+
case (Term.Block(Term.Block(Nil, body1) :: Nil, Term.Block(Nil, cond1)), Term.Literal(Constant.Unit())) =>
233+
this += "do "
234+
printTree(body1) += " while "
235+
inParens(printTree(cond1))
236+
case _ =>
237+
this += "while "
238+
inParens(printTree(cond)) += " "
239+
printTree(body)
240+
}
239241

240242
case IsDefDef(ddef @ DefDef(name, targs, argss, tpt, rhs)) =>
241243
printDefAnnotations(ddef)

0 commit comments

Comments
 (0)