@@ -455,15 +455,25 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
455
455
override def Block (tree : Tree )(stats : List [Tree ], expr : Tree )(implicit ctx : Context ): Block = {
456
456
val tree1 = untpd.cpy.Block (tree)(stats, expr)
457
457
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)
459
464
case _ => ta.assignType(tree1, stats, expr)
460
465
}
461
466
}
462
467
463
468
override def If (tree : Tree )(cond : Tree , thenp : Tree , elsep : Tree )(implicit ctx : Context ): If = {
464
469
val tree1 = untpd.cpy.If (tree)(cond, thenp, elsep)
465
470
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)
467
477
case _ => ta.assignType(tree1, thenp, elsep)
468
478
}
469
479
}
0 commit comments