summary refs log tree commit diff stats
diff options
context:
space:
mode:
authormetagn <metagngn@gmail.com>2024-09-19 00:50:58 +0300
committerGitHub <noreply@github.com>2024-09-18 23:50:58 +0200
commit58cf62451d144d9fd5438f1798e7dfb3423f373f (patch)
tree78070673f6658ec51f9482105832e224caa177c3
parent00ac961ab18c6c8cba9fd2bdad8390816dec1ec8 (diff)
downloadNim-58cf62451d144d9fd5438f1798e7dfb3423f373f.tar.gz
fix typed case range not counting for exhaustiveness (#24136)
fixes #22661

Range expressions in `of` branches in `case` statements start off as
calls to `..` then become `nkRange` when getting typed. For this reason
the compiler leaves `nkRange` alone when type checking the case
statements again, but it still does the exhaustiveness checking for the
entire case statement, and leaving the range alone means it doesn't
count the values of the range for exhaustiveness. So the counting is now
also done on `nkRange` nodes in the same way as when typechecking it the
first time.
-rw-r--r--compiler/semtypes.nim2
-rw-r--r--tests/casestmt/trangeexhaustiveness.nim7
2 files changed, 9 insertions, 0 deletions
diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim
index 8cba88747..6302a4590 100644
--- a/compiler/semtypes.nim
+++ b/compiler/semtypes.nim
@@ -619,6 +619,8 @@ proc semCaseBranch(c: PContext, n, branch: PNode, branchIndex: int,
     var b = branch[i]
     if b.kind == nkRange:
       branch[i] = b
+      # same check as in semBranchRange for exhaustiveness
+      covered = covered + getOrdValue(b[1]) + 1 - getOrdValue(b[0])
     elif isRange(b):
       branch[i] = semCaseBranchRange(c, n, b, covered)
     else:
diff --git a/tests/casestmt/trangeexhaustiveness.nim b/tests/casestmt/trangeexhaustiveness.nim
new file mode 100644
index 000000000..2b7f3558e
--- /dev/null
+++ b/tests/casestmt/trangeexhaustiveness.nim
@@ -0,0 +1,7 @@
+block: # issue #22661
+  template foo(a: typed) =
+    a
+    
+  foo:
+    case false
+    of false..true: discard