From b740e8cca8cee12fcd690a0bfea95ea6fe76075f Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Sat, 15 Feb 2025 03:52:43 +0800 Subject: [PATCH] fixes #24673; divmod errors for ranges (#24679) fixes #24673 The problem is that there is no way to distinguish `cint`, `cint`, etc ctypes with Nim types. So `when T is cint | clong | clonglong:` is true for types derived from `int`, `int32` and `int64`. In this PR, it fixes the branch to avoid erros for `Natural` (cherry picked from commit b211ada2734c78370e741095c733c054aa2b4d8a) --- lib/pure/math.nim | 2 +- tests/stdlib/tmath.nim | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 7a5603ece3..1139597753 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -105,7 +105,7 @@ when not defined(js) and not defined(nimscript): # C when compileOption("overflowChecks"): if y == 0: raise new(DivByZeroDefect) - elif (x == T.low and y == -1.T): + elif (x == T.low and int64(y) == -1): raise new(OverflowDefect) let res = divmod_c(x, y) result[0] = res.quot diff --git a/tests/stdlib/tmath.nim b/tests/stdlib/tmath.nim index 69534b16e2..5d3fd450ff 100644 --- a/tests/stdlib/tmath.nim +++ b/tests/stdlib/tmath.nim @@ -545,3 +545,8 @@ when not defined(js) and not defined(danger): doAssertRaises(OverflowDefect): discard sum(x) +block: # bug #24673 + let x: Natural = 5 + let y: Natural = 3 + + doAssert divmod(x, y) == (Natural 1, Natural 2)