This commit is contained in:
Andreas Rumpf 2019-06-26 19:34:05 +02:00
commit e083a1533b
3 changed files with 30 additions and 10 deletions

View file

@ -1306,6 +1306,12 @@ proc genNewSeqOfCap(p: BProc; e: PNode; d: var TLoc) =
let seqtype = skipTypes(e.typ, abstractVarRange)
var a: TLoc
initLocExpr(p, e.sons[1], a)
if p.config.selectedGC == gcDestructors:
if d.k == locNone: getTemp(p, e.typ, d, needsInit=false)
linefmt(p, cpsStmts, "$1.len = 0; $1.p = ($4*) #newSeqPayload($2, sizeof($3));$n",
[d.rdLoc, a.rdLoc, getTypeDesc(p.module, seqtype.lastSon),
getSeqPayloadType(p.module, seqtype)])
else:
putIntoDest(p, d, e, ropecg(p.module,
"($1)#nimNewSeqOfCap($2, $3)", [
getTypeDesc(p.module, seqtype),

View file

@ -997,8 +997,7 @@ proc newSeqOfCap*[T](cap: Natural): seq[T] {.
## assert len(x) == 1
discard
when not defined(JS) and not defined(gcDestructors):
# XXX enable this for --gc:destructors
when not defined(JS):
proc newSeqUninitialized*[T: SomeNumber](len: Natural): seq[T] =
## Creates a new sequence of type ``seq[T]`` with length ``len``.
##
@ -1011,6 +1010,9 @@ when not defined(JS) and not defined(gcDestructors):
## assert len(x) == 3
## x[0] = 10
result = newSeqOfCap[T](len)
when defined(gcDestructors):
cast[ptr int](addr result)[] = len
else:
var s = cast[PGenericSeq](result)
s.len = len

View file

@ -7,7 +7,9 @@ ha
@[1, 2, 3]
@["red", "yellow", "orange", "rtrt1", "pink"]
a: @[4, 2, 3]
35 35'''
0
30
41 41'''
"""
import allocators
@ -178,6 +180,16 @@ proc mutConstSeq() =
mutConstSeq()
proc mainSeqOfCap =
# bug #11098
var s = newSeqOfCap[int](10)
echo s.len
var s2 = newSeqUninitialized[int](30)
echo s2.len
mainSeqOfCap()
#echo s
let (a, d) = allocCounters()
discard cprintf("%ld %ld\n", a, d)