Skip to content

Commit ab6d5fa

Browse files
committed
Quick-fix non-idempotent code generated by pattern match
1 parent f85ee4e commit ab6d5fa

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package transform
33

44
import core._
55
import MegaPhase._
6-
import collection.mutable
6+
import scala.collection.mutable
77
import SymDenotations._, Symbols._, Contexts._, Types._, Names._, StdNames._, NameOps._
88
import ast.Trees._
99
import util.Positions._
@@ -636,7 +636,7 @@ object PatternMatcher {
636636
}
637637
override def hashCode: Int = tree.hash
638638
}
639-
type SeenVars = Map[RHS, TermSymbol]
639+
type SeenVars = mutable.LinkedHashMap[RHS, TermSymbol]
640640

641641
/** The variables known at entry to label */
642642
val seenAtLabel = newMutableSymbolMap[SeenVars]
@@ -664,15 +664,18 @@ object PatternMatcher {
664664

665665
override def apply(plan: LetPlan): Plan = {
666666
initializer(plan.sym) = apply(initializer(plan.sym))
667-
val seenVars1 =
667+
val seenVars1: SeenVars =
668668
if (isPatmatGenerated(plan.sym)) {
669669
val thisRhs = new RHS(initializer(plan.sym))
670670
seenVars.get(thisRhs) match {
671671
case Some(seen) =>
672672
initializer(plan.sym) = ref(seen)
673673
seenVars
674674
case none =>
675-
seenVars.updated(thisRhs, plan.sym)
675+
val seenVars1: SeenVars = mutable.LinkedHashMap.empty
676+
seenVars1 ++= seenVars
677+
seenVars1(thisRhs) = plan.sym
678+
seenVars1
676679
}
677680
}
678681
else seenVars
@@ -685,7 +688,9 @@ object PatternMatcher {
685688
plan.body = apply(plan.body)
686689
val paramsMap = paramsOfLabel.getOrElse(plan.sym, Map())
687690
plan.params = paramsMap.values.toList.sortBy(_.name.toString)
688-
val seenVars1 = seenVars ++ paramsMap
691+
val seenVars1: SeenVars = mutable.LinkedHashMap.empty
692+
seenVars1 ++= seenVars
693+
seenVars1 ++= paramsMap
689694
labelled(plan.sym) = new Merge(seenVars1)(labelled(plan.sym))
690695
plan
691696
}
@@ -709,7 +714,7 @@ object PatternMatcher {
709714
else CallPlan(plan.label, newArgs)
710715
}
711716
}
712-
(new Merge(Map()))(plan)
717+
(new Merge(new mutable.LinkedHashMap()))(plan)
713718
}
714719

715720
/** Inline let-bound trees that are referenced only once.

0 commit comments

Comments
 (0)