fixes #23223; prevents insert self-assignment (#23225)

fixes #23223

(cherry picked from commit 3379d26629)
This commit is contained in:
ringabout
2024-01-18 21:20:54 +08:00
committed by narimiran
parent a715aeb716
commit 2c964268f8
2 changed files with 9 additions and 0 deletions

View File

@@ -2426,6 +2426,8 @@ when hasAlloc or defined(nimscript):
## var a = "abc"
## a.insert("zz", 0) # a <- "zzabc"
## ```
if item.len == 0: # prevents self-assignment
return
var xl = x.len
setLen(x, xl+item.len)
var j = xl-1

View File

@@ -212,3 +212,10 @@ block:
doAssert not compiles(echo p.rawProc.repr)
doAssert not compiles(echo p.rawEnv.repr)
doAssert not compiles(echo p.finished)
proc bug23223 = # bug #23223
var stuff = "hello"
stuff.insert ""
doAssert stuff == "hello"
bug23223()