Skip to content

Fix #10108: Distinguish between soft and hard union types #10112

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

Merged
merged 9 commits into from
Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -822,14 +822,16 @@ import transform.SymUtils._
|"""
}

class PatternMatchExhaustivity(uncoveredFn: => String)(using Context)
class PatternMatchExhaustivity(uncoveredFn: => String, hasMore: Boolean)(using Context)
extends Message(PatternMatchExhaustivityID) {
def kind = "Pattern Match Exhaustivity"
lazy val uncovered = uncoveredFn
def msg =
val addendum = if hasMore then "(More unmatched cases are elided)" else ""
em"""|${hl("match")} may not be exhaustive.
|
|It would fail on pattern case: $uncovered"""
|It would fail on pattern case: $uncovered
|$addendum"""


def explain =
Expand Down
29 changes: 17 additions & 12 deletions compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,22 @@ trait SpaceLogic {
})

/** Flatten space to get rid of `Or` for pretty print */
def flatten(space: Space)(using Context): List[Space] = space match {
def flatten(space: Space)(using Context): Seq[Space] = space match {
case Prod(tp, fun, spaces, full) =>
spaces.map(flatten) match {
case Nil => Prod(tp, fun, Nil, full) :: Nil
case ss =>
ss.foldLeft(List[Prod]()) { (acc, flat) =>
if (acc.isEmpty) flat.map(s => Prod(tp, fun, s :: Nil, full))
else for (Prod(tp, fun, ss, full) <- acc; s <- flat) yield Prod(tp, fun, ss :+ s, full)
}
val ss = LazyList(spaces: _*).map(flatten)

ss.foldLeft(LazyList(Nil : List[Space])) { (acc, flat) =>
for { sps <- acc; s <- flat }
yield sps :+ s
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

}.map { sps =>
Prod(tp, fun, sps, full)
}

case Or(spaces) =>
spaces.flatMap(flatten _)
case _ => List(space)
LazyList(spaces: _*).flatMap(flatten)

case _ =>
List(space)
}

/** Is `a` a subspace of `b`? Equivalent to `a - b == Empty`, but faster */
Expand Down Expand Up @@ -838,8 +841,10 @@ class SpaceEngine(using Context) extends SpaceLogic {
s != Empty && (!checkGADTSAT || satisfiable(s))
}

if (uncovered.nonEmpty)
report.warning(PatternMatchExhaustivity(show(uncovered)), sel.srcPos)

if uncovered.nonEmpty then
val hasMore = uncovered.lengthCompare(6) > 0
report.warning(PatternMatchExhaustivity(show(uncovered.take(6)), hasMore), sel.srcPos)
}

private def redundancyCheckable(sel: Tree): Boolean =
Expand Down
2 changes: 1 addition & 1 deletion tests/patmat/i8922c.check
Original file line number Diff line number Diff line change
@@ -1 +1 @@
26: Pattern Match Exhaustivity: (_, _, _)
26: Pattern Match Exhaustivity: (true, _: String, _), (true, _: Double, _), (true, true, _), (true, false, _), (true, (), _), (false, _: String, _)