Skip to content

Commit abaf97e

Browse files
committed
Use EmptyTree for the cond of infinite WhileDo loops.
Such `WhileDo` node have type `Nothing` instead of `Unit`. This is used in the loops generated by `TailRec` in order not to need some spurious dead code.
1 parent 5ee5ee0 commit abaf97e

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,12 @@ class TailRec extends MiniPhase {
140140
}
141141

142142
Block(
143-
initialValDefs :::
144-
WhileDo(Literal(Constant(true)), {
143+
initialValDefs,
144+
WhileDo(EmptyTree, {
145145
Labeled(transformer.continueLabel.get.asTerm, {
146146
Return(rhsFullyTransformed, ref(origMeth))
147147
})
148-
}) ::
149-
Nil,
150-
Throw(Literal(Constant(null))) // unreachable code
148+
})
151149
)
152150
} else {
153151
if (mandatory) ctx.error(

compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ trait TypeAssigner {
486486
tree.withType(defn.NothingType)
487487

488488
def assignType(tree: untpd.WhileDo)(implicit ctx: Context): WhileDo =
489-
tree.withType(defn.UnitType)
489+
tree.withType(if (tree.cond eq EmptyTree) defn.NothingType else defn.UnitType)
490490

491491
def assignType(tree: untpd.Try, expr: Tree, cases: List[CaseDef])(implicit ctx: Context): Try =
492492
if (cases.isEmpty) tree.withType(expr.tpe)

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,9 @@ class Typer extends Namer
11261126
}
11271127

11281128
def typedWhileDo(tree: untpd.WhileDo)(implicit ctx: Context): Tree = track("typedWhileDo") {
1129-
val cond1 = typed(tree.cond, defn.BooleanType)
1129+
val cond1 =
1130+
if (tree.cond eq EmptyTree) EmptyTree
1131+
else typed(tree.cond, defn.BooleanType)
11301132
val body1 = typed(tree.body, defn.UnitType)
11311133
assignType(cpy.WhileDo(tree)(cond1, body1))
11321134
}

0 commit comments

Comments
 (0)