Skip to content

Commit 52d8c77

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 4adc949 commit 52d8c77

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
@@ -141,14 +141,12 @@ class TailRec extends MiniPhase {
141141
}
142142

143143
Block(
144-
initialValDefs :::
145-
WhileDo(Literal(Constant(true)), {
144+
initialValDefs,
145+
WhileDo(EmptyTree, {
146146
Labeled(transformer.continueLabel.get.asTerm, {
147147
Return(rhsFullyTransformed, ref(origMeth))
148148
})
149-
}) ::
150-
Nil,
151-
Throw(Literal(Constant(null))) // unreachable code
149+
})
152150
)
153151
} else {
154152
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
@@ -487,7 +487,7 @@ trait TypeAssigner {
487487
tree.withType(defn.NothingType)
488488

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

492492
def assignType(tree: untpd.Try, expr: Tree, cases: List[CaseDef])(implicit ctx: Context) =
493493
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
@@ -1138,7 +1138,9 @@ class Typer extends Namer
11381138
}
11391139

11401140
def typedWhileDo(tree: untpd.WhileDo)(implicit ctx: Context): Tree = track("typedWhileDo") {
1141-
val cond1 = typed(tree.cond, defn.BooleanType)
1141+
val cond1 =
1142+
if (tree.cond eq EmptyTree) EmptyTree
1143+
else typed(tree.cond, defn.BooleanType)
11421144
val body1 = typed(tree.body, defn.UnitType)
11431145
assignType(cpy.WhileDo(tree)(cond1, body1))
11441146
}

0 commit comments

Comments
 (0)