File tree 3 files changed +14
-4
lines changed
compiler/src/dotty/tools/dotc 3 files changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -2030,10 +2030,11 @@ object messages {
2030
2030
}
2031
2031
}
2032
2032
2033
- case class UnableToEmitSwitch ()(implicit ctx : Context )
2033
+ case class UnableToEmitSwitch (tooFewCases : Boolean )(implicit ctx : Context )
2034
2034
extends Message (UnableToEmitSwitchID ) {
2035
2035
val kind = " Syntax"
2036
- val msg = hl " Could not emit switch for ${" @switch" } annotated match "
2036
+ val tooFewStr = if (tooFewCases) " since there are not enough cases" else " "
2037
+ val msg = hl " Could not emit switch for ${" @switch" } annotated match $tooFewStr"
2037
2038
val explanation = {
2038
2039
val codeExample =
2039
2040
""" val ConstantB = 'B'
Original file line number Diff line number Diff line change @@ -48,6 +48,9 @@ object PatternMatcher {
48
48
49
49
final val selfCheck = false // debug option, if on we check that no case gets generated twice
50
50
51
+ /** Minimal number of cases to emit a switch */
52
+ final val MinSwitchCases = 4
53
+
51
54
/** Was symbol generated by pattern matcher? */
52
55
def isPatmatGenerated (sym : Symbol )(implicit ctx : Context ): Boolean =
53
56
sym.is(Synthetic ) &&
@@ -863,7 +866,7 @@ object PatternMatcher {
863
866
plan match {
864
867
case plan : TestPlan =>
865
868
val switchCases = collectSwitchCases(plan)
866
- if (switchCases.lengthCompare(4 ) >= 0 ) // at least 3 cases + default
869
+ if (switchCases.lengthCompare(MinSwitchCases ) >= 0 ) // at least 3 cases + default
867
870
Match (plan.scrutinee, emitSwitchCases(switchCases))
868
871
else {
869
872
/** Merge nested `if`s that have the same `else` branch into a single `if`.
@@ -982,7 +985,7 @@ object PatternMatcher {
982
985
patmatch.println(i " original types: ${typesInCases(original.cases)}%, % " )
983
986
patmatch.println(i " switch types : ${typesInCases(resultCases)}%, % " )
984
987
patmatch.println(i " tree = $result" )
985
- ctx.warning(UnableToEmitSwitch (), original.pos)
988
+ ctx.warning(UnableToEmitSwitch (numTypes(original.cases) < MinSwitchCases ), original.pos)
986
989
}
987
990
case _ =>
988
991
}
Original file line number Diff line number Diff line change @@ -13,4 +13,10 @@ class Test {
13
13
case '5' | Constant => 3
14
14
case '4' => 4
15
15
}
16
+
17
+ def test3 (x : Any ) = (x : @ annotation.switch) match { // error: could not emit switch - too few cases
18
+ case 1 => 1
19
+ case 2 => 2
20
+ case x : String => 4
21
+ }
16
22
}
You can’t perform that action at this time.
0 commit comments