From 58cf62451d144d9fd5438f1798e7dfb3423f373f Mon Sep 17 00:00:00 2001 From: metagn Date: Thu, 19 Sep 2024 00:50:58 +0300 Subject: 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. --- compiler/semtypes.nim | 2 ++ tests/casestmt/trangeexhaustiveness.nim | 7 +++++++ 2 files changed, 9 insertions(+) create mode 100644 tests/casestmt/trangeexhaustiveness.nim 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 -- cgit 1.4.1-2-gfad0