-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]())) { | ||
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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 */ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.