Skip to content

Commit 2e7c9eb

Browse files
Jasper JenkinsAraq
authored andcommitted
case coverage error message for char (#12948)
1 parent 28466ce commit 2e7c9eb

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

compiler/semstmts.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ proc semCase(c: PContext, n: PNode; flags: TExprFlags): PNode =
953953
if chckCovered:
954954
if covered == toCover(c, n[0].typ):
955955
hasElse = true
956-
elif n[0].typ.skipTypes(abstractRange).kind == tyEnum:
956+
elif n[0].typ.skipTypes(abstractRange).kind in {tyEnum, tyChar}:
957957
localError(c.config, n.info, "not all cases are covered; missing: $1" %
958958
formatMissingEnums(c, n))
959959
else:

compiler/semtypes.nim

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ iterator processBranchVals(b: PNode): int =
607607
assert b.kind in {nkOfBranch, nkElifBranch, nkElse}
608608
if b.kind == nkOfBranch:
609609
for i in 0..<b.len-1:
610-
if b[i].kind == nkIntLit:
610+
if b[i].kind in {nkIntLit, nkCharLit}:
611611
yield b[i].intVal.int
612612
elif b[i].kind == nkRange:
613613
for i in b[i][0].intVal..b[i][1].intVal:
@@ -621,9 +621,12 @@ proc renderAsType(vals: IntSet, t: PType): string =
621621
for val in vals:
622622
if result.len > 1:
623623
result &= ", "
624-
if t.kind in {tyEnum, tyBool}:
624+
case t.kind:
625+
of tyEnum, tyBool:
625626
while t.n[enumSymOffset].sym.position < val: inc(enumSymOffset)
626627
result &= t.n[enumSymOffset].sym.name.s
628+
of tyChar:
629+
result.addQuoted(char(val))
627630
else:
628631
if i == 64:
629632
result &= "omitted $1 values..." % $(vals.len - i)

tests/casestmt/tincompletecaseobject2.nim

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@ discard """
22
cmd: "nim check $file"
33
errormsg: "not all cases are covered; missing: {A, B}"
44
nimout: '''
5-
tincompletecaseobject2.nim(16, 1) Error: not all cases are covered; missing: {B, C, D}
6-
tincompletecaseobject2.nim(19, 1) Error: not all cases are covered; missing: {A, C}
7-
tincompletecaseobject2.nim(22, 1) Error: not all cases are covered; missing: {A, B}
5+
tincompletecaseobject2.nim(18, 1) Error: not all cases are covered; missing: {' ', '!', '\"', '#', '$', '%', '&', '\'', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~'}
6+
tincompletecaseobject2.nim(22, 1) Error: not all cases are covered; missing: {B, C, D}
7+
tincompletecaseobject2.nim(25, 1) Error: not all cases are covered; missing: {A, C}
8+
tincompletecaseobject2.nim(28, 1) Error: not all cases are covered; missing: {A, B}
89
'''
910
"""
1011
type
1112
ABCD = enum A, B, C, D
1213
AliasABCD = ABCD
1314
RangeABC = range[A .. C]
1415
AliasRangeABC = RangeABC
16+
PrintableChars = range[' ' .. '~']
17+
18+
case PrintableChars 'x':
19+
of '0'..'9', 'A'..'Z', 'a'..'z': discard
20+
of '(', ')': discard
1521

1622
case AliasABCD A:
1723
of A: discard

0 commit comments

Comments
 (0)