Fix capacity for const and shallow [backport] (#22705)

(cherry picked from commit b542be1e7d)
This commit is contained in:
Amjad Ben Hedhili
2023-09-18 21:57:30 +01:00
committed by narimiran
parent fff127bec7
commit ef8b8317ff
4 changed files with 8 additions and 8 deletions

View File

@@ -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".}

View File

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

View File

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

View File

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