From becb06dd70a6a9b2789071235decbb7cd29d5ebb Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Sun, 8 Feb 2026 03:46:50 +0800 Subject: [PATCH] fixes #25488; Strings can be compared against nil (#25489) fixes #25488 ref https://github.com/nim-lang/Nim/pull/20222 (cherry picked from commit 513c9aa69a59d4dd414363d518d317b6e614f2ee) --- lib/system/comparisons.nim | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/system/comparisons.nim b/lib/system/comparisons.nim index a8d78bb93a..0a6ac150bb 100644 --- a/lib/system/comparisons.nim +++ b/lib/system/comparisons.nim @@ -38,6 +38,21 @@ proc `==`*[T](x, y: ptr T): bool {.magic: "EqRef", noSideEffect.} proc `==`*[T: proc | iterator](x, y: T): bool {.magic: "EqProc", noSideEffect.} ## Checks that two `proc` variables refer to the same procedure. +when true: + # guard against string converted to cstring implicitly; see also #bug #25488 + proc isNil*(x: string): bool {.noSideEffect, error: "'isNil' is invalid for 'string'".} + + + # bug #9149; ensure that 'typeof(nil)' does not match *too* well by using 'typeof(nil) | typeof(nil)', + # especially for converters, see tests/overload/tconverter_to_string.nim + # Eventually we will be able to remove this hack completely. + + proc `==`*(x: string; y: typeof(nil) | typeof(nil)): bool {.error: "'nil' is invalid for 'string'".} = + discard + + proc `==`*(x: typeof(nil) | typeof(nil); y: string): bool {.error: "'nil' is invalid for 'string'".} = + discard + proc `<=`*[Enum: enum](x, y: Enum): bool {.magic: "LeEnum", noSideEffect.} proc `<=`*(x, y: string): bool {.magic: "LeStr", noSideEffect.} = ## Compares two strings and returns true if `x` is lexicographically