Skip to content

Fix pattern matching alternative #5653

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
47 changes: 38 additions & 9 deletions compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -769,16 +769,45 @@ object PatternMatcher {
}

/** Emit cases of a switch */
private def emitSwitchCases(cases: List[(List[Tree], Plan)]): List[CaseDef] = (cases: @unchecked) match {
case (alts, ons) :: cases1 =>
val pat = alts match {
case alt :: Nil => alt
case Nil => Underscore(defn.IntType) // default case
case _ => Alternative(alts)
private def emitSwitchCases(cases: List[(List[Tree], Plan)]): List[CaseDef] = cases.foldLeft((List[CaseDef](), List[Tree]())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Generally our code style is to prefer tail-recursive formulation over folds and to replace Option with sentinels. This generally increases clarity and avoids unnecessary allocations. Allocations are your enemy - they make the compiler slow, more than anything else. And since they are spread out, there are no hot spots to address, you really have to take care to avoid them everywhere as much as is reasonable.

So would prefer the old formulation of emitCases over the new one.

case ((prev, collected), (alts, ons)) =>
collectCases(collected, alts) match {
case Some((pat, col)) => (CaseDef(pat, EmptyTree, emit(ons)) :: prev, col ::: collected)
case None => (prev, collected)
}
CaseDef(pat, EmptyTree, emit(ons)) :: emitSwitchCases(cases1)
case nil =>
Nil
}._1

/** Flattens the tree of patterns into a tree and collect all the alternative patterns in a list
* returns None if the pattern is redundant
*/
private def collectCases(existingPatterns: List[Tree], alts: List[Tree]): Option[(Tree, List[Tree])] = {
alts match {
case Nil => Some((Underscore(defn.IntType), Nil))
case _ => mapCases(removeRedundantCases(existingPatterns, alts))
}
}

private def mapCases(alts: List[Tree]): Option[(Tree, List[Tree])] = alts match {
case alt :: Nil => Some((alt, alt :: Nil))
case Nil => None
case _ => Some((Alternative(alts), alts))
}

/** Remove cases that already appear in the same pattern or in previous patterns */
private def removeRedundantCases(previousCases: List[Tree], cases: List[Tree]): List[Tree] = cases.foldLeft(List[Tree]()) {
case (cases, alt) =>
if (cases.exists(_ === alt) || previousCases.exists(_ === alt)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is quadratic in the number of cases. Maybe create a set of case values instead?

cases
} else {
alt :: cases
}
}

/** Flatten a list of patterns into a single tree */
private def simplifyCases(alts: List[Tree]): Tree = alts match {
Copy link
Contributor

Choose a reason for hiding this comment

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

Unused?

case alt :: Nil => alt
case Nil => Underscore(defn.IntType) // default case
case _ => Alternative(alts)
}

/** If selfCheck is `true`, used to check whether a tree gets generated twice */
Expand Down
28 changes: 28 additions & 0 deletions tests/pos/patternMatchAlternative.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

object Main {
val a: Int = 4
a match {
case 1 => println("1")
case 1 | 2 => println("1 or 2")
}

a match {
case 1 => 1
case 0 | 0 => 0
case 2 | 2 | 2 | 3 | 2 | 3 => 0
case 4 | (_ @ 4) => 0
case _ => -1
}

a match {
case 1 => 1
case 0 | 0 => 0
case 2 | 2 | 2 | 3 | 2 | 3 => 0
case _ => -1
}

a match {
case 0 | 1 => 0
case 1 => 1
}
}