Skip to content

Commit a7954d0

Browse files
committed
Fix #9776: Don't issue a @switch warning when fewer than 4 cases
1 parent f9e2428 commit a7954d0

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,10 +1969,9 @@ import ast.tpd
19691969
|whose behavior may have changed since version change."""
19701970
}
19711971

1972-
class UnableToEmitSwitch(tooFewCases: Boolean)(using Context)
1972+
class UnableToEmitSwitch()(using Context)
19731973
extends SyntaxMsg(UnableToEmitSwitchID) {
1974-
def tooFewStr: String = if (tooFewCases) " since there are not enough cases" else ""
1975-
def msg = em"Could not emit switch for ${hl("@switch")} annotated match$tooFewStr"
1974+
def msg = em"Could not emit switch for ${hl("@switch")} annotated match"
19761975
def explain = {
19771976
val codeExample =
19781977
"""val ConstantB = 'B'

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -983,12 +983,13 @@ object PatternMatcher {
983983
cdefs.flatMap(cdef => typesInPattern(cdef.pat))
984984
def numTypes(cdefs: List[CaseDef]): Int =
985985
typesInCases(cdefs).toSet.size: Int // without the type ascription, testPickling fails because of #2840.
986-
if (numTypes(resultCases) < numTypes(original.cases)) {
986+
if (numTypes(resultCases) < numTypes(original.cases)
987+
&& numTypes(original.cases) >= MinSwitchCases) {
987988
patmatch.println(i"switch warning for ${ctx.compilationUnit}")
988989
patmatch.println(i"original types: ${typesInCases(original.cases)}%, %")
989990
patmatch.println(i"switch types : ${typesInCases(resultCases)}%, %")
990991
patmatch.println(i"tree = $result")
991-
report.warning(UnableToEmitSwitch(numTypes(original.cases) < MinSwitchCases), original.srcPos)
992+
report.warning(UnableToEmitSwitch(), original.srcPos)
992993
}
993994
case _ =>
994995
}

tests/neg-custom-args/fatal-warnings/i3561.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Test {
1414
case '4' => 4
1515
}
1616

17-
def test3(x: Any) = (x: @annotation.switch) match { // error: could not emit switch - too few cases
17+
def test3(x: Any) = (x: @annotation.switch) match {
1818
case 1 => 1
1919
case 2 => 2
2020
case x: String => 4

tests/neg-custom-args/fatal-warnings/switches.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ object Main {
3636
// multiple annotations are processed correctly
3737

3838
// thinks a val in an object is constant... so naive
39-
def fail1(c: Char) = (c: @switch @unchecked) match { // error: Could not emit switch for @switch annotated match
39+
def fail1(c: Char) = (c: @switch @unchecked) match {
4040
case 'A' => true
4141
case Other.C1 => true
4242
case _ => false
4343
}
4444

4545
// more naivete
46-
def fail2(c: Char) = (c: @unchecked @switch) match { // error: Could not emit switch for @switch annotated match
46+
def fail2(c: Char) = (c: @unchecked @switch) match {
4747
case 'A' => true
4848
case Other.C3 => true
4949
case _ => false

0 commit comments

Comments
 (0)