@@ -728,17 +728,11 @@ class Typer extends Namer
728
728
def typedIf (tree : untpd.If , pt : Type )(implicit ctx : Context ): Tree = track(" typedIf" ) {
729
729
if (tree.isInline) checkInInlineContext(" inline if" , tree.posd)
730
730
val cond1 = typed(tree.cond, defn.BooleanType )
731
-
732
- if (tree.elsep.isEmpty) {
733
- val thenp1 = typed(tree.thenp, defn.UnitType )
734
- val elsep1 = tpd.unitLiteral.withSpan(tree.span.endPos)
735
- cpy.If (tree)(cond1, thenp1, elsep1).withType(defn.UnitType )
736
- }
737
- else {
738
- val thenp1 :: elsep1 :: Nil = harmonic(harmonize, pt)(
739
- (tree.thenp :: tree.elsep :: Nil ).map(typed(_, pt.notApplied)))
740
- assignType(cpy.If (tree)(cond1, thenp1, elsep1), thenp1, elsep1)
731
+ val thenp1 :: elsep1 :: Nil = harmonic(harmonize, pt) {
732
+ val elsep = tree.elsep.orElse(untpd.unitLiteral.withSpan(tree.span.endPos))
733
+ (tree.thenp :: elsep :: Nil ).map(typed(_, pt.notApplied))
741
734
}
735
+ assignType(cpy.If (tree)(cond1, thenp1, elsep1), thenp1, elsep1)
742
736
}
743
737
744
738
/** Decompose function prototype into a list of parameter prototypes and a result prototype
@@ -2089,7 +2083,15 @@ class Typer extends Namer
2089
2083
case Thicket (stats) :: rest =>
2090
2084
traverse(stats ++ rest)
2091
2085
case stat :: rest =>
2092
- val stat1 = typed(stat)(ctx.exprContext(stat, exprOwner))
2086
+ val pt = stat match {
2087
+ case _ : untpd.If | _ : untpd.Match =>
2088
+ // Typing `if` and `match` statement with `Unit` as expected
2089
+ // type produces more efficient code (see #5750).
2090
+ defn.UnitType
2091
+ case _ =>
2092
+ WildcardType
2093
+ }
2094
+ val stat1 = typed(stat, pt)(ctx.exprContext(stat, exprOwner))
2093
2095
checkStatementPurity(stat1)(stat, exprOwner)
2094
2096
buf += stat1
2095
2097
traverse(rest)
0 commit comments