fixes #25488; Strings can be compared against nil (#25489)

fixes #25488
ref https://github.com/nim-lang/Nim/pull/20222

(cherry picked from commit 513c9aa69a)
This commit is contained in:
ringabout
2026-02-08 03:46:50 +08:00
committed by narimiran
parent 2ddeabceaf
commit becb06dd70

View File

@@ -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