mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-13 06:43:52 +00:00
fixes #11098
This commit is contained in:
@@ -1306,11 +1306,17 @@ proc genNewSeqOfCap(p: BProc; e: PNode; d: var TLoc) =
|
||||
let seqtype = skipTypes(e.typ, abstractVarRange)
|
||||
var a: TLoc
|
||||
initLocExpr(p, e.sons[1], a)
|
||||
putIntoDest(p, d, e, ropecg(p.module,
|
||||
"($1)#nimNewSeqOfCap($2, $3)", [
|
||||
getTypeDesc(p.module, seqtype),
|
||||
genTypeInfo(p.module, seqtype, e.info), a.rdLoc]))
|
||||
gcUsage(p.config, e)
|
||||
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),
|
||||
genTypeInfo(p.module, seqtype, e.info), a.rdLoc]))
|
||||
gcUsage(p.config, e)
|
||||
|
||||
proc genConstExpr(p: BProc, n: PNode): Rope
|
||||
proc handleConstExpr(p: BProc, n: PNode, d: var TLoc): bool =
|
||||
|
||||
@@ -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,8 +1010,11 @@ when not defined(JS) and not defined(gcDestructors):
|
||||
## assert len(x) == 3
|
||||
## x[0] = 10
|
||||
result = newSeqOfCap[T](len)
|
||||
var s = cast[PGenericSeq](result)
|
||||
s.len = len
|
||||
when defined(gcDestructors):
|
||||
cast[ptr int](addr result)[] = len
|
||||
else:
|
||||
var s = cast[PGenericSeq](result)
|
||||
s.len = len
|
||||
|
||||
proc len*[TOpenArray: openArray|varargs](x: TOpenArray): int {.
|
||||
magic: "LengthOpenArray", noSideEffect.}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user