Skip to content

Commit cd2bd96

Browse files
authored
Merge pull request #5968 from dotty-staging/fix-if-block
Address comments in #5958
2 parents daf4ce9 + a6dd55d commit cd2bd96

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,12 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
573573
val tree1 = untpdCpy.Block(tree)(stats, expr)
574574
tree match {
575575
case tree: Block if (expr.tpe eq tree.expr.tpe) && (expr.tpe eq tree.tpe) =>
576-
// the second guard is needed in case avoid somehow widened the type.
577-
// if it did it could potentially need to rewiden it
578-
// eg {val s = ...; s}
579-
// changing type of s should change type of block, though type of expr is unchanged - TermRef(s)
576+
// The last guard is a conservative check: if `tree.tpe` is different from `expr.tpe`, then
577+
// it was computed from widening `expr.tpe`, and tree transforms might cause `expr.tpe.widen`
578+
// to change even if `expr.tpe` itself didn't change, e.g:
579+
// { val s = ...; s }
580+
// If the type of `s` changed, then the type of the block might have changed, even though `expr.tpe`
581+
// will still be `TermRef(NoPrefix, s)`
580582
tree1.withTypeUnchecked(tree.tpe)
581583
case _ => ta.assignType(tree1, stats, expr)
582584
}
@@ -587,9 +589,10 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
587589
tree match {
588590
case tree: If if (thenp.tpe eq tree.thenp.tpe) && (elsep.tpe eq tree.elsep.tpe) &&
589591
((tree.tpe eq thenp.tpe) || (tree.tpe eq elsep.tpe)) =>
590-
// last guard is needed in case previous if had computed a widened ORType that needs to be recomputed
591-
// eg {val a = ...; val b = ...; if(...) a else b}
592-
// changing type of a or b should change type of if, though types of both trees remain unchanged
592+
// The last guard is a conservative check similar to the one done in `Block` above,
593+
// if `tree.tpe` is not identical to the type of one of its branch, it might have been
594+
// computed from the widened type of the branches, so the same reasoning than
595+
// in `Block` applies.
593596
tree1.withTypeUnchecked(tree.tpe)
594597
case _ => ta.assignType(tree1, thenp, elsep)
595598
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,10 @@ class ElimErasedValueType extends MiniPhase with InfoTransformer {
127127
override def transformInlined(tree: Inlined)(implicit ctx: Context): Tree =
128128
transformTypeOfTree(tree)
129129

130-
// FIXME: transformIf and transformBlock won't be required anymore once #444 is fixed.
131130
override def transformIdent(tree: Ident)(implicit ctx: Context): Tree =
132131
transformTypeOfTree(tree)
133132
override def transformSelect(tree: Select)(implicit ctx: Context): Tree =
134133
transformTypeOfTree(tree)
135-
override def transformBlock(tree: Block)(implicit ctx: Context): Tree =
136-
transformTypeOfTree(tree)
137-
override def transformIf(tree: If)(implicit ctx: Context): Tree =
138-
transformTypeOfTree(tree)
139134
override def transformTypeTree(tree: TypeTree)(implicit ctx: Context): Tree =
140135
transformTypeOfTree(tree)
141136
}

tests/pos/vcblock.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Position(val foo: Int) extends AnyVal {
2+
def thing(): Position = {
3+
val y = new Position(1)
4+
y
5+
}
6+
}

tests/pos/vcif.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Position(val foo: Int) extends AnyVal {
2+
def orElse(that: Position) =
3+
if (foo != 0) this else that
4+
}

0 commit comments

Comments
 (0)