Files
Nim/tests/range/timplicitrangeconsts.nim
ringabout 286b7eb6f6 fixes #25608; ImplicitRangeConversion now skips compile-time constants
The warning gate previously only exempted literal AST nodes
(nkCharLit..nkUInt64Lit, nkFloatLit..nkFloat128Lit). Enum constants,
named consts, and constant expressions passed through and triggered a
spurious ImplicitRangeConversion warning even though the compiler already
knows their value and can validate range membership exactly.

Replace the literal-kind check with a call to getConstExpr: if the
source node folds to a compile-time constant the warning is suppressed.
Non-constant values (variables, parameters, runtime expressions) are
unaffected and still warn as before.

Add tests/range/timplicitrangeconsts.nim to guard the fix.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-06-02 15:35:09 +08:00

31 lines
461 B
Nim

discard """
cmd: "nim check $options --hints:off --warning:ImplicitRangeConversion --warningaserror:ImplicitRangeConversion $file"
action: "compile"
"""
type
E = enum
ea, eb
R = range[eb..eb]
I = range[0..3]
proc accept(r: R) = discard
proc accept(i: I) = discard
var r: R
var i: I
const enumOk = eb
const enumAlias = enumOk
const intOk = 1 + 2
r = eb
r = enumOk
r = enumAlias
accept(eb)
accept(enumOk)
accept(enumAlias)
i = intOk
accept(intOk)