File tree Expand file tree Collapse file tree 3 files changed +14
-4
lines changed
compiler/src/dotty/tools/dotc Expand file tree Collapse file tree 3 files changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -2030,10 +2030,11 @@ object messages {
20302030 }
20312031 }
20322032
2033- case class UnableToEmitSwitch ()(implicit ctx : Context )
2033+ case class UnableToEmitSwitch (tooFewCases : Boolean )(implicit ctx : Context )
20342034 extends Message (UnableToEmitSwitchID ) {
20352035 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"
20372038 val explanation = {
20382039 val codeExample =
20392040 """ val ConstantB = 'B'
Original file line number Diff line number Diff line change @@ -48,6 +48,9 @@ object PatternMatcher {
4848
4949 final val selfCheck = false // debug option, if on we check that no case gets generated twice
5050
51+ /** Minimal number of cases to emit a switch */
52+ final val MinSwitchCases = 4
53+
5154 /** Was symbol generated by pattern matcher? */
5255 def isPatmatGenerated (sym : Symbol )(implicit ctx : Context ): Boolean =
5356 sym.is(Synthetic ) &&
@@ -863,7 +866,7 @@ object PatternMatcher {
863866 plan match {
864867 case plan : TestPlan =>
865868 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
867870 Match (plan.scrutinee, emitSwitchCases(switchCases))
868871 else {
869872 /** Merge nested `if`s that have the same `else` branch into a single `if`.
@@ -982,7 +985,7 @@ object PatternMatcher {
982985 patmatch.println(i " original types: ${typesInCases(original.cases)}%, % " )
983986 patmatch.println(i " switch types : ${typesInCases(resultCases)}%, % " )
984987 patmatch.println(i " tree = $result" )
985- ctx.warning(UnableToEmitSwitch (), original.pos)
988+ ctx.warning(UnableToEmitSwitch (numTypes(original.cases) < MinSwitchCases ), original.pos)
986989 }
987990 case _ =>
988991 }
Original file line number Diff line number Diff line change @@ -13,4 +13,10 @@ class Test {
1313 case '5' | Constant => 3
1414 case '4' => 4
1515 }
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+ }
1622}
You can’t perform that action at this time.
0 commit comments