Browse files

SI-7290 Minor cleanups driven by review comments.

  - make a def a val, we only need to compute it once
  - add a clarifying comment
  - only report the first duplicate
  • Loading branch information...
1 parent 2e0be83 commit c3ad5af27df1b365873870827a6093f630881343 @retronym retronym committed Mar 27, 2013
View
8 src/compiler/scala/tools/nsc/transform/patmat/MatchOptimization.scala
@@ -16,9 +16,9 @@ import scala.reflect.internal.util.NoPosition
/** Optimize and analyze matches based on their TreeMaker-representation.
*
* The patmat translation doesn't rely on this, so it could be disabled in principle.
- *
- * TODO: split out match analysis
+ * - well, not quite: the backend crashes if we emit duplicates in switches (e.g. SI-7290)
*/
+// TODO: split out match analysis
trait MatchOptimization extends MatchTreeMaking with MatchAnalysis {
import PatternMatchingStats._
import global.{Tree, Type, Symbol, NoSymbol, CaseDef, atPos,
@@ -457,9 +457,9 @@ trait MatchOptimization extends MatchTreeMaking with MatchAnalysis {
case _ => t
}
// SI-7290 Discard duplicate alternatives that would crash the backend
- def distinctAlts = distinctBy(switchableAlts)(extractConst)
+ val distinctAlts = distinctBy(switchableAlts)(extractConst)
if (distinctAlts.size < switchableAlts.size) {
- val duplicated = switchableAlts.groupBy(extractConst).flatMap(_._2.drop(1))
+ val duplicated = switchableAlts.groupBy(extractConst).flatMap(_._2.drop(1).take(1)) // report the first duplicated
global.currentUnit.warning(pos, s"Pattern contains duplicate alternatives: ${duplicated.mkString(", ")}")
}
CaseDef(Alternative(distinctAlts), guard, body)
View
2 test/files/neg/t7290.check
@@ -1,7 +1,7 @@
t7290.scala:4: error: Pattern contains duplicate alternatives: 0
case 0 | 0 => 0
^
-t7290.scala:5: error: Pattern contains duplicate alternatives: 2, 2, 2, 3
+t7290.scala:5: error: Pattern contains duplicate alternatives: 2, 3
case 2 | 2 | 2 | 3 | 2 | 3 => 0
^
t7290.scala:6: error: Pattern contains duplicate alternatives: 4

0 comments on commit c3ad5af

Please sign in to comment.