From 3bc78930d18d751a75ff6908f5dd166935dc3ffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Doeraene?= Date: Sat, 15 Sep 2018 21:52:27 +0200 Subject: [PATCH 1/2] Promote `WhileDo` loops to typed trees. They remain as such, without desugaring, until the back-end. We also use them to desugar `DoWhile` loops, instead of label-defs: do { body } while (cond) is now desugared as while { { body }; { cond } } () the inner blocks protecting the respective scopes of `body` and `cond` from each other. This is the second step towards getting rid of label-defs. --- .../backend/jvm/DottyBackendInterface.scala | 7 ++ .../src/dotty/tools/dotc/ast/Desugar.scala | 19 +---- compiler/src/dotty/tools/dotc/ast/Trees.scala | 15 ++++ compiler/src/dotty/tools/dotc/ast/tpd.scala | 6 ++ compiler/src/dotty/tools/dotc/ast/untpd.scala | 10 +-- .../tools/dotc/core/tasty/TastyFormat.scala | 83 ++++++++++--------- .../tools/dotc/core/tasty/TreePickler.scala | 6 ++ .../tools/dotc/core/tasty/TreeUnpickler.scala | 4 + .../tools/dotc/printing/RefinedPrinter.scala | 4 +- .../tools/dotc/tastyreflect/TreeOpsImpl.scala | 19 ++--- .../tools/dotc/transform/MegaPhase.scala | 31 ++++++- .../dotty/tools/dotc/typer/TypeAssigner.scala | 3 + .../src/dotty/tools/dotc/typer/Typer.scala | 7 ++ .../src/scala/tasty/util/ShowExtractors.scala | 2 + .../src/scala/tasty/util/ShowSourceCode.scala | 10 +-- scala-backend | 2 +- .../quote-inline-function.check | 10 ++- 17 files changed, 145 insertions(+), 93 deletions(-) diff --git a/compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala b/compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala index fb5fb35e49e9..8114a00f80a9 100644 --- a/compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala +++ b/compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala @@ -63,6 +63,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma type Throw = tpd.Apply type Labeled = tpd.Labeled type Return = tpd.Return + type WhileDo = tpd.WhileDo type Block = tpd.Block type Typed = tpd.Typed type Match = tpd.Match @@ -196,6 +197,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma implicit val ThrowTag: ClassTag[Throw] = ClassTag[Throw](classOf[Throw]) implicit val LabeledTag: ClassTag[Labeled] = ClassTag[Labeled](classOf[Labeled]) implicit val ReturnTag: ClassTag[Return] = ClassTag[Return](classOf[Return]) + implicit val WhileDoTag: ClassTag[WhileDo] = ClassTag[WhileDo](classOf[WhileDo]) implicit val LiteralTag: ClassTag[Literal] = ClassTag[Literal](classOf[Literal]) implicit val BlockTag: ClassTag[Block] = ClassTag[Block](classOf[Block]) implicit val TypedTag: ClassTag[Typed] = ClassTag[Typed](classOf[Typed]) @@ -1088,6 +1090,11 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma def _2: Symbol = if (field.from.symbol.isLabel) field.from.symbol else NoSymbol } + object WhileDo extends WhileDoDeconstructor { + def _1: Tree = field.cond + def _2: Tree = field.body + } + object Ident extends IdentDeconstructor { def get = field.name } diff --git a/compiler/src/dotty/tools/dotc/ast/Desugar.scala b/compiler/src/dotty/tools/dotc/ast/Desugar.scala index 1104ecb9230e..b22e31492156 100644 --- a/compiler/src/dotty/tools/dotc/ast/Desugar.scala +++ b/compiler/src/dotty/tools/dotc/ast/Desugar.scala @@ -927,13 +927,6 @@ object desugar { /** Main desugaring method */ def apply(tree: Tree)(implicit ctx: Context): Tree = { - /** { label def lname(): Unit = rhs; call } - */ - def labelDefAndCall(lname: TermName, rhs: Tree, call: Tree) = { - val ldef = DefDef(lname, Nil, ListOfNil, TypeTree(defn.UnitType), rhs).withFlags(Label | Synthetic) - Block(ldef, call) - } - /** Create tree for for-comprehension `` or * `` where mapName and flatMapName are chosen * corresponding to whether this is a for-do or a for-yield. @@ -1158,16 +1151,10 @@ object desugar { case PrefixOp(op, t) => val nspace = if (ctx.mode.is(Mode.Type)) tpnme else nme Select(t, nspace.UNARY_PREFIX ++ op.name) - case WhileDo(cond, body) => - // {