Skip to content

Commit 7f6afc7

Browse files
committed
Fix #444: be more conservative on when Block and If types should be recomputed.
1 parent 2362081 commit 7f6afc7

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,15 +455,25 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
455455
override def Block(tree: Tree)(stats: List[Tree], expr: Tree)(implicit ctx: Context): Block = {
456456
val tree1 = untpd.cpy.Block(tree)(stats, expr)
457457
tree match {
458-
case tree: Block if (expr.tpe eq tree.expr.tpe) => tree1.withTypeUnchecked(tree.tpe)
458+
case tree: Block if ((expr.tpe eq tree.expr.tpe) && (expr.tpe eq tree.tpe)) =>
459+
// the second guard is needed in case avoid somehow widened the type.
460+
// if it did it could potentially need to rewiden it
461+
// eg {val s = ...; s}
462+
// changing type of s should change type of block, though type of expr is unchanged - TermRef(s)
463+
tree1.withTypeUnchecked(tree.tpe)
459464
case _ => ta.assignType(tree1, stats, expr)
460465
}
461466
}
462467

463468
override def If(tree: Tree)(cond: Tree, thenp: Tree, elsep: Tree)(implicit ctx: Context): If = {
464469
val tree1 = untpd.cpy.If(tree)(cond, thenp, elsep)
465470
tree match {
466-
case tree: If if (thenp.tpe eq tree.thenp.tpe) && (elsep.tpe eq tree.elsep.tpe) => tree1.withTypeUnchecked(tree.tpe)
471+
case tree: If if (thenp.tpe eq tree.thenp.tpe) && (elsep.tpe eq tree.elsep.tpe) &&
472+
((tree.tpe eq thenp.tpe) || (tree.tpe eq elsep.tpe)) =>
473+
// last guard is needed in case previous if had computed a widened ORType that needs to be recomputed
474+
// eg {val a = ...; val b = ...; if(...) a else b}
475+
// changing type of a or b should change type of if, though types of both trees remain unchanged
476+
tree1.withTypeUnchecked(tree.tpe)
467477
case _ => ta.assignType(tree1, thenp, elsep)
468478
}
469479
}

0 commit comments

Comments
 (0)