Skip to content

Type If and Match in statement position as Unit #5768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -728,17 +728,11 @@ class Typer extends Namer
def typedIf(tree: untpd.If, pt: Type)(implicit ctx: Context): Tree = track("typedIf") {
if (tree.isInline) checkInInlineContext("inline if", tree.posd)
val cond1 = typed(tree.cond, defn.BooleanType)

if (tree.elsep.isEmpty) {
val thenp1 = typed(tree.thenp, defn.UnitType)
val elsep1 = tpd.unitLiteral.withSpan(tree.span.endPos)
cpy.If(tree)(cond1, thenp1, elsep1).withType(defn.UnitType)
}
else {
val thenp1 :: elsep1 :: Nil = harmonic(harmonize, pt)(
(tree.thenp :: tree.elsep :: Nil).map(typed(_, pt.notApplied)))
assignType(cpy.If(tree)(cond1, thenp1, elsep1), thenp1, elsep1)
val thenp1 :: elsep1 :: Nil = harmonic(harmonize, pt) {
val elsep = tree.elsep.orElse(untpd.unitLiteral.withSpan(tree.span.endPos))
(tree.thenp :: elsep :: Nil).map(typed(_, pt.notApplied))
}
assignType(cpy.If(tree)(cond1, thenp1, elsep1), thenp1, elsep1)
}

/** Decompose function prototype into a list of parameter prototypes and a result prototype
Expand Down Expand Up @@ -2089,7 +2083,15 @@ class Typer extends Namer
case Thicket(stats) :: rest =>
traverse(stats ++ rest)
case stat :: rest =>
val stat1 = typed(stat)(ctx.exprContext(stat, exprOwner))
val pt = stat match {
case _: untpd.If | _: untpd.Match =>
// Typing `if` and `match` statement with `Unit` as expected
// type produces more efficient code (see #5750).
defn.UnitType
case _ =>
WildcardType
}
val stat1 = typed(stat, pt)(ctx.exprContext(stat, exprOwner))
checkStatementPurity(stat1)(stat, exprOwner)
buf += stat1
traverse(rest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,9 +627,12 @@ class TestBCode extends DottyBytecodeTest {
@Test def i5750 = {
val source =
"""class Test {
| def foo: String = ""
| def foo(): String = ""
| def test(cond: Boolean): Int = {
| if (cond) foo
| if (cond) foo()
| if (cond) () else foo()
| if (cond) foo() else ()
| cond match { case true => foo(); case _ => () }
| 1
| }
|}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,7 @@ class ErrorMessagesTests extends ErrorMessagesTest {
| val z = Boo(1, "foo")
|
| z match {
| case Boo(a, b, c) => a
| case Boo(a, b, c) => ()
| }
|}
""".stripMargin
Expand Down
14 changes: 7 additions & 7 deletions tests/patmat/enum/patmat-enum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ object Test1 {
val day: Day = ???

day match {
case Day.MONDAY => true
case Day.TUESDAY => true
case Day.WEDNESDAY => true
case Day.MONDAY => ()
case Day.TUESDAY => ()
case Day.WEDNESDAY => ()
}
}

Expand All @@ -13,9 +13,9 @@ object Test2 {
val day: Day = ???

day match {
case MONDAY => true
case TUESDAY => true
case WEDNESDAY => true
case SUNDAY => true
case MONDAY => ()
case TUESDAY => ()
case WEDNESDAY => ()
case SUNDAY => ()
}
}
8 changes: 4 additions & 4 deletions tests/patmat/t3111.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ object Test {
val bool: Boolean = false

bool match {
case true => "true!"
case true => ()
}

bool match {
case true => "true!"
case false => "false!"
case _ => "cats and dogs living together... mass hysteria!"
case true => ()
case false => ()
case _ => ()
}
}
2 changes: 1 addition & 1 deletion tests/patmat/tuple.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
object Test {
(4, (4, 6)) match {
case (x, (y, z)) => true
case (x, (y, z)) => ()
}
}
4 changes: 2 additions & 2 deletions tests/pos-special/isInstanceOf/3324d.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Test {
val x: Any = ???

x match {
case _: List[Int @unchecked] => 5
case _: Option[Int] @unchecked => 5
case _: List[Int @unchecked] => ()
case _: Option[Int] @unchecked => ()
}
}
2 changes: 1 addition & 1 deletion tests/run/virtpatmat_alts.decompiled
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
object Test extends dotty.runtime.LegacyApp() {
scala.Tuple2.apply[scala.Boolean, scala.Boolean](true, true) match {
case (scala.Tuple2(true, true) | scala.Tuple2(false, false)) =>
1
()
}
scala.List.apply[scala.Int](5) match {
case (scala.::(1, scala.Nil) | scala.::(2, scala.Nil)) =>
Expand Down
2 changes: 1 addition & 1 deletion tests/run/virtpatmat_alts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
object Test extends dotty.runtime.LegacyApp {
(true, true) match {
case (true, true) | (false, false) => 1
case (true, true) | (false, false) => ()
}

List(5) match {
Expand Down