diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 7474942776..b44931fb09 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -324,7 +324,7 @@ proc isComplexValueType(t: PType): bool {.inline.} = (t.kind == tyProc and t.callConv == ccClosure) proc resetLoc(p: BProc, loc: var TLoc) = - let containsGcRef = containsGarbageCollectedRef(loc.t) + let containsGcRef = p.config.selectedGc != gcDestructors and containsGarbageCollectedRef(loc.t) let typ = skipTypes(loc.t, abstractVarRange) if isImportedCppType(typ): return if p.config.selectedGc == gcDestructors and typ.kind in {tyString, tySequence}: diff --git a/compiler/liftdestructors.nim b/compiler/liftdestructors.nim index b85360eade..8a367f32b2 100644 --- a/compiler/liftdestructors.nim +++ b/compiler/liftdestructors.nim @@ -263,12 +263,19 @@ proc newSeqCall(g: ModuleGraph; x, y: PNode): PNode = lenCall.typ = getSysType(g, x.info, tyInt) result.add lenCall -proc setLenCall(g: ModuleGraph; x, y: PNode; m: TMagic): PNode = - let lenCall = genBuiltin(g, mLengthSeq, "len", y) +proc setLenStrCall(g: ModuleGraph; x, y: PNode): PNode = + let lenCall = genBuiltin(g, mLengthStr, "len", y) lenCall.typ = getSysType(g, x.info, tyInt) - result = genBuiltin(g, m, "setLen", genAddr(g, x)) + result = genBuiltin(g, mSetLengthStr, "setLen", genAddr(g, x)) result.add lenCall +proc setLenSeqCall(c: var TLiftCtx; t: PType; x, y: PNode): PNode = + let lenCall = genBuiltin(c.graph, mLengthSeq, "len", y) + lenCall.typ = getSysType(c.graph, x.info, tyInt) + var op = getSysMagic(c.graph, x.info, "setLen", mSetLengthSeq) + op = c.c.instTypeBoundOp(c.c, op, t, c.info, attachedAsgn, 1) + result = newTree(nkCall, newSymNode(op, x.info), genAddr(c.graph, x), lenCall) + proc forallElements(c: var TLiftCtx; t: PType; body, x, y: PNode) = let i = declareCounter(c, body, firstOrd(c.graph.config, t)) let whileLoop = genWhileLoop(c, i, x) @@ -286,7 +293,7 @@ proc seqOp(c: var TLiftCtx; t: PType; body, x, y: PNode) = # var i = 0 # while i < y.len: dest[i] = y[i]; inc(i) # This is usually more efficient than a destroy/create pair. - body.add setLenCall(c.graph, x, y, mSetLengthSeq) + body.add setLenSeqCall(c, t, x, y) forallElements(c, t, body, x, y) of attachedSink: let moveCall = genBuiltin(c.graph, mMove, "move", x) @@ -307,7 +314,7 @@ proc strOp(c: var TLiftCtx; t: PType; body, x, y: PNode) = # var i = 0 # while i < y.len: dest[i] = y[i]; inc(i) # This is usually more efficient than a destroy/create pair. - body.add setLenCall(c.graph, x, y, mSetLengthStr) + body.add setLenStrCall(c.graph, x, y) forallElements(c, t, body, x, y) of attachedSink: let moveCall = genBuiltin(c.graph, mMove, "move", x) diff --git a/lib/core/seqs.nim b/lib/core/seqs.nim index 4a21515b70..7eb2afe17a 100644 --- a/lib/core/seqs.nim +++ b/lib/core/seqs.nim @@ -145,8 +145,7 @@ proc setLen[T](s: var seq[T], newlen: Natural) = if newlen < s.len: shrink(s, newLen) else: - var v: T # get the default value of 'v' - grow(s, newLen, v) + grow(s, newLen, default(T)) when false: proc resize[T](s: var NimSeqV2[T]) = diff --git a/lib/system.nim b/lib/system.nim index 7bd3aa625d..c0efc40a37 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -1706,6 +1706,10 @@ proc `@`* [IDX, T](a: array[IDX, T]): seq[T] {. ## echo @a # => @[1, 3, 5] ## echo @b # => @['f', 'o', 'o'] +when defined(nimHasDefault): + proc default*(T: typedesc): T {.magic: "Default", noSideEffect.} + ## returns the default value of the type ``T``. + proc setLen*[T](s: var seq[T], newlen: Natural) {. magic: "SetLengthSeq", noSideEffect.} ## Sets the length of seq `s` to `newlen`. ``T`` may be any sequence type. @@ -4400,10 +4404,6 @@ when defined(genode): # and return to thread entrypoint. -when defined(nimHasDefault): - proc default*(T: typedesc): T {.magic: "Default", noSideEffect.} - ## returns the default value of the type ``T``. - import system/widestrs export widestrs