Skip to content

Commit aa5a5b1

Browse files
committed
Refine condition when emitted code is a switch
We now demand that the number of different constants in the emitted match is the same as the number of different constants in the original match.
1 parent 69f4aa2 commit aa5a5b1

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ object PatternMatcher {
9898
sym.is(Synthetic) &&
9999
(sym.is(Label) || sym.name.is(PatMatStdBinderName))
100100

101+
/** Test whether a type refers to a pattern-generated variable */
101102
private val refersToInternal = new TypeAccumulator[Boolean] {
102103
def apply(x: Boolean, tp: Type) =
103104
x || {
@@ -108,20 +109,9 @@ object PatternMatcher {
108109
} || foldOver(x, tp)
109110
}
110111

111-
/** A type map that eliminates all patternmatcher-generated termrefs that
112-
* can be replaced by a source-level alias.
112+
/** Widen type as far as necessary so that it does not refer to a pattern-
113+
* generated variable.
113114
*/
114-
private val sanitize2 = new TypeMap {
115-
def apply(t: Type): Type = t.widenExpr match {
116-
case t: TermRef if isPatmatGenerated(t.symbol) =>
117-
t.info.widenExpr match {
118-
case t1: TermRef => apply(t1)
119-
case _ => t
120-
}
121-
case t => mapOver(t)
122-
}
123-
}
124-
125115
private def sanitize(tp: Type): Type = tp.widenExpr match {
126116
case tp: TermRef if refersToInternal(false, tp) => sanitize(tp.underlying)
127117
case tp => tp
@@ -908,8 +898,12 @@ object PatternMatcher {
908898
case Block(_, Match(_, cases)) => cases
909899
case _ => Nil
910900
}
911-
if (resultCases.length < original.cases.length)
912-
ctx.warning(s"could not emit switch for @switch annotated match", original.pos)
901+
def numConsts(cdefs: List[CaseDef]) = {
902+
val tpes = cdefs.map(_.pat.tpe)
903+
tpes.toSet.size
904+
}
905+
if (numConsts(resultCases) < numConsts(original.cases))
906+
ctx.warning(i"could not emit switch for @switch annotated match", original.pos)
913907
case _ =>
914908
}
915909

0 commit comments

Comments
 (0)