From ef8b8317ffc053fe028f478b329c857d460b6932 Mon Sep 17 00:00:00 2001 From: Amjad Ben Hedhili Date: Mon, 18 Sep 2023 21:57:30 +0100 Subject: [PATCH] Fix `capacity` for const and shallow [backport] (#22705) (cherry picked from commit b542be1e7de067573370a3159b0812396309ef8b) --- lib/system.nim | 8 ++++---- lib/system/seqs_v2.nim | 2 +- lib/system/strs_v2.nim | 2 +- lib/system/sysstr.nim | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/system.nim b/lib/system.nim index 25133fca50..b524d43dfa 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -457,10 +457,6 @@ when not defined(js) and not defined(nimSeqsV2): data: UncheckedArray[char] NimString = ptr NimStringDesc -when notJSnotNims and not defined(nimSeqsV2): - template space(s: PGenericSeq): int {.dirty.} = - s.reserved and not (seqShallowFlag or strlitFlag) - when notJSnotNims: include "system/hti" @@ -1068,6 +1064,10 @@ const hasThreadSupport = compileOption("threads") and not defined(nimscript) hasSharedHeap = defined(boehmgc) or defined(gogc) # don't share heaps; every thread has its own +when notJSnotNims and not defined(nimSeqsV2): + template space(s: PGenericSeq): int = + s.reserved and not (seqShallowFlag or strlitFlag) + when hasThreadSupport and defined(tcc) and not compileOption("tlsEmulation"): # tcc doesn't support TLS {.error: "`--tlsEmulation:on` must be used when using threads with tcc backend".} diff --git a/lib/system/seqs_v2.nim b/lib/system/seqs_v2.nim index a8bf2e3e97..87577ed1d6 100644 --- a/lib/system/seqs_v2.nim +++ b/lib/system/seqs_v2.nim @@ -150,7 +150,7 @@ func capacity*[T](self: seq[T]): int {.inline.} = assert lst.capacity == 42 let sek = cast[ptr NimSeqV2[T]](unsafeAddr self) - result = if sek.p != nil: (sek.p.cap and not strlitFlag) else: 0 + result = if sek.p != nil: sek.p.cap and not strlitFlag else: 0 {.pop.} # See https://github.com/nim-lang/Nim/issues/21401 diff --git a/lib/system/strs_v2.nim b/lib/system/strs_v2.nim index a9a104d3d1..abdbcd7c3b 100644 --- a/lib/system/strs_v2.nim +++ b/lib/system/strs_v2.nim @@ -211,4 +211,4 @@ func capacity*(self: string): int {.inline.} = assert str.capacity == 42 let str = cast[ptr NimStringV2](unsafeAddr self) - result = if str.p != nil: str.p.cap else: 0 + result = if str.p != nil: str.p.cap and not strlitFlag else: 0 diff --git a/lib/system/sysstr.nim b/lib/system/sysstr.nim index 4acf1780b9..98711bbc1e 100644 --- a/lib/system/sysstr.nim +++ b/lib/system/sysstr.nim @@ -350,7 +350,7 @@ func capacity*(self: string): int {.inline.} = assert str.capacity == 42 let str = cast[NimString](self) - result = if str != nil: str.reserved else: 0 + result = if str != nil: str.space else: 0 func capacity*[T](self: seq[T]): int {.inline.} = ## Returns the current capacity of the seq. @@ -361,4 +361,4 @@ func capacity*[T](self: seq[T]): int {.inline.} = assert lst.capacity == 42 let sek = cast[PGenericSeq](self) - result = if sek != nil: (sek.reserved and not strlitFlag) else: 0 + result = if sek != nil: sek.space else: 0