From 286b7eb6f69117fcae8679702f1f41ab0e1904a7 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 26 May 2026 22:43:30 +0800 Subject: [PATCH] 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> --- compiler/sempass2.nim | 5 +++-- tests/range/timplicitrangeconsts.nim | 30 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tests/range/timplicitrangeconsts.nim diff --git a/compiler/sempass2.nim b/compiler/sempass2.nim index 7b2be510f9..10685263b3 100644 --- a/compiler/sempass2.nim +++ b/compiler/sempass2.nim @@ -1559,9 +1559,10 @@ proc track(tracked: PEffects, n: PNode) = message(tracked.config, n.info, warnPtrToCstringConv, $n[1].typ) - # Check for implicit range conversions + # Check for implicit range conversions. Compile-time constants are already + # fully known here, so only non-constant values need the downsizing warning. if n.kind == nkHiddenStdConv and (not tracked.isArrayIndexing) and - n[1].kind notin {nkCharLit..nkUInt64Lit, nkFloatLit..nkFloat128Lit} and + getConstExpr(tracked.ownerModule, n[1], tracked.c.idgen, tracked.graph) == nil and shouldWarnRangeConversion(tracked.config, n.info, n.typ, n[1].typ): message(tracked.config, n.info, warnImplicitRangeConversion, typeToString(n[1].typ) & " -> " & typeToString(n.typ)) diff --git a/tests/range/timplicitrangeconsts.nim b/tests/range/timplicitrangeconsts.nim new file mode 100644 index 0000000000..c786230d8c --- /dev/null +++ b/tests/range/timplicitrangeconsts.nim @@ -0,0 +1,30 @@ +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)