From cd22560af5f13a7c024b97e2f3de2b1fe2439eb3 Mon Sep 17 00:00:00 2001 From: metagn Date: Sun, 8 Sep 2024 21:17:26 +0300 Subject: [PATCH] fix string literal assignment with different lengths on ARC (#24083) fixes #24080 --- lib/system/strs_v2.nim | 2 +- tests/arc/tstringliteral.nim | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 tests/arc/tstringliteral.nim diff --git a/lib/system/strs_v2.nim b/lib/system/strs_v2.nim index dbee107774c34..404b4f78dc675 100644 --- a/lib/system/strs_v2.nim +++ b/lib/system/strs_v2.nim @@ -166,7 +166,7 @@ proc setLengthStrV2(s: var NimStringV2, newLen: int) {.compilerRtl.} = s.len = newLen proc nimAsgnStrV2(a: var NimStringV2, b: NimStringV2) {.compilerRtl.} = - if a.p == b.p: return + if a.p == b.p and a.len == b.len: return if isLiteral(b): # we can shallow copy literals: frees(a) diff --git a/tests/arc/tstringliteral.nim b/tests/arc/tstringliteral.nim new file mode 100644 index 0000000000000..c5fac22d82c59 --- /dev/null +++ b/tests/arc/tstringliteral.nim @@ -0,0 +1,17 @@ +discard """ + matrix: "--mm:arc; --mm:orc" +""" + +block: # issue #24080 + var a = (s: "a") + var b = "a" + a.s.setLen 0 + b = a.s + doAssert b == "" + +block: # issue #24080, longer string + var a = (s: "abc") + var b = "abc" + a.s.setLen 2 + b = a.s + doAssert b == "ab"