Skip to content

Commit ef8b831

Browse files
AmjadHDnarimiran
authored andcommitted
Fix capacity for const and shallow [backport] (#22705)
(cherry picked from commit b542be1)
1 parent fff127b commit ef8b831

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

lib/system.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,6 @@ when not defined(js) and not defined(nimSeqsV2):
457457
data: UncheckedArray[char]
458458
NimString = ptr NimStringDesc
459459
460-
when notJSnotNims and not defined(nimSeqsV2):
461-
template space(s: PGenericSeq): int {.dirty.} =
462-
s.reserved and not (seqShallowFlag or strlitFlag)
463-
464460
when notJSnotNims:
465461
include "system/hti"
466462
@@ -1068,6 +1064,10 @@ const
10681064
hasThreadSupport = compileOption("threads") and not defined(nimscript)
10691065
hasSharedHeap = defined(boehmgc) or defined(gogc) # don't share heaps; every thread has its own
10701066

1067+
when notJSnotNims and not defined(nimSeqsV2):
1068+
template space(s: PGenericSeq): int =
1069+
s.reserved and not (seqShallowFlag or strlitFlag)
1070+
10711071
when hasThreadSupport and defined(tcc) and not compileOption("tlsEmulation"):
10721072
# tcc doesn't support TLS
10731073
{.error: "`--tlsEmulation:on` must be used when using threads with tcc backend".}

lib/system/seqs_v2.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func capacity*[T](self: seq[T]): int {.inline.} =
150150
assert lst.capacity == 42
151151
152152
let sek = cast[ptr NimSeqV2[T]](unsafeAddr self)
153-
result = if sek.p != nil: (sek.p.cap and not strlitFlag) else: 0
153+
result = if sek.p != nil: sek.p.cap and not strlitFlag else: 0
154154
155155
156156
{.pop.} # See https://github.com/nim-lang/Nim/issues/21401

lib/system/strs_v2.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,4 @@ func capacity*(self: string): int {.inline.} =
211211
assert str.capacity == 42
212212

213213
let str = cast[ptr NimStringV2](unsafeAddr self)
214-
result = if str.p != nil: str.p.cap else: 0
214+
result = if str.p != nil: str.p.cap and not strlitFlag else: 0

lib/system/sysstr.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ func capacity*(self: string): int {.inline.} =
350350
assert str.capacity == 42
351351
352352
let str = cast[NimString](self)
353-
result = if str != nil: str.reserved else: 0
353+
result = if str != nil: str.space else: 0
354354
355355
func capacity*[T](self: seq[T]): int {.inline.} =
356356
## Returns the current capacity of the seq.
@@ -361,4 +361,4 @@ func capacity*[T](self: seq[T]): int {.inline.} =
361361
assert lst.capacity == 42
362362
363363
let sek = cast[PGenericSeq](self)
364-
result = if sek != nil: (sek.reserved and not strlitFlag) else: 0
364+
result = if sek != nil: sek.space else: 0

0 commit comments

Comments
 (0)