Skip to content

Commit 8f408bb

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 437a828 commit 8f408bb

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import reporting.diagnostic.messages.TailrecNotApplicable
3232
* {{{
3333
* var localForParam1: T1 = param1
3434
* ...
35-
* while (true) {
35+
* while (<empty>) {
3636
* tailResult[ResultType]: {
3737
* return {
3838
* // original rhs with tail recursive calls transformed (see below)
@@ -60,7 +60,7 @@ import reporting.diagnostic.messages.TailrecNotApplicable
6060
* def fact(n: Int, acc: Int): Int = {
6161
* var acc$tailLocal1: Int = acc
6262
* var n$tailLocal1: Int = n
63-
* while (true) {
63+
* while (<empty>) {
6464
* tailLabel1[Unit]: {
6565
* return {
6666
* if (n$tailLocal1 == 0)
@@ -163,14 +163,12 @@ class TailRec extends MiniPhase {
163163
}
164164

165165
Block(
166-
initialVarDefs :::
167-
WhileDo(Literal(Constant(true)), {
166+
initialVarDefs,
167+
WhileDo(EmptyTree, {
168168
Labeled(transformer.continueLabel.asTerm, {
169169
Return(rhsFullyTransformed, origMeth)
170170
})
171-
}) ::
172-
Nil,
173-
Throw(Literal(Constant(null))) // unreachable code
171+
})
174172
)
175173
} else {
176174
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)