Skip to content

Commit b542be1

Browse files
authored
Fix capacity for const and shallow [backport] (nim-lang#22705)
1 parent 2c5b94b commit b542be1

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

lib/system.nim

+4-4
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

@@ -1048,6 +1044,10 @@ const
10481044
hasThreadSupport = compileOption("threads") and not defined(nimscript)
10491045
hasSharedHeap = defined(boehmgc) or defined(gogc) # don't share heaps; every thread has its own
10501046

1047+
when notJSnotNims and not defined(nimSeqsV2):
1048+
template space(s: PGenericSeq): int =
1049+
s.reserved and not (seqShallowFlag or strlitFlag)
1050+
10511051
when hasThreadSupport and defined(tcc) and not compileOption("tlsEmulation"):
10521052
# tcc doesn't support TLS
10531053
{.error: "`--tlsEmulation:on` must be used when using threads with tcc backend".}

lib/system/seqs_v2.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func capacity*[T](self: seq[T]): int {.inline.} =
193193
assert lst.capacity == 42
194194

195195
let sek = cast[ptr NimSeqV2[T]](unsafeAddr self)
196-
result = if sek.p != nil: (sek.p.cap and not strlitFlag) else: 0
196+
result = if sek.p != nil: sek.p.cap and not strlitFlag else: 0
197197

198198

199199
{.pop.} # See https://github.com/nim-lang/Nim/issues/21401

lib/system/strs_v2.nim

+1-1
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ func capacity*(self: string): int {.inline.} =
343343
assert str.capacity == 42
344344

345345
let str = cast[NimString](self)
346-
result = if str != nil: str.reserved else: 0
346+
result = if str != nil: str.space else: 0
347347

348348
func capacity*[T](self: seq[T]): int {.inline.} =
349349
## Returns the current capacity of the seq.
@@ -354,4 +354,4 @@ func capacity*[T](self: seq[T]): int {.inline.} =
354354
assert lst.capacity == 42
355355

356356
let sek = cast[PGenericSeq](self)
357-
result = if sek != nil: (sek.reserved and not strlitFlag) else: 0
357+
result = if sek != nil: sek.space else: 0

0 commit comments

Comments
 (0)