Skip to content

Commit

Permalink
adds =destroy T support for strings and seqs (nim-lang#22167)
Browse files Browse the repository at this point in the history
* adds =destroy T support for strings and seqs

* fixes system

* fixes tests
  • Loading branch information
ringabout authored and bung87 committed Jul 29, 2023
1 parent 5a16f1c commit af8fe5b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions compiler/liftdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ proc symPrototype(g: ModuleGraph; typ: PType; owner: PSym; kind: TTypeAttachedOp
let src = newSym(skParam, getIdent(g.cache, if kind == attachedTrace: "env" else: "src"),
idgen, result, info)

if kind == attachedDestructor and typ.kind == tyRef:
if kind == attachedDestructor and typ.kind in {tyRef, tyString, tySequence} and g.config.selectedGC in {gcArc, gcOrc, gcAtomicArc}:
dest.typ = typ
else:
dest.typ = makeVarType(typ.owner, typ, idgen)
Expand Down Expand Up @@ -1196,7 +1196,7 @@ proc patchBody(g: ModuleGraph; c: PContext; n: PNode; info: TLineInfo; idgen: Id
if op != nil:
if op.ast.isGenericRoutine:
internalError(g.config, info, "resolved destructor is generic")
if op.magic == mDestroy:
if op.magic == mDestroy and t.kind != tyString:
internalError(g.config, info, "patching mDestroy with mDestroy?")
n[0] = newSymNode(op)
for x in n: patchBody(g, c, x, info, idgen)
Expand Down
8 changes: 7 additions & 1 deletion lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,13 @@ proc arrGet[I: Ordinal;T](a: T; i: I): T {.
proc arrPut[I: Ordinal;T,S](a: T; i: I;
x: S) {.noSideEffect, magic: "ArrPut".}

when defined(nimAllowNonVarDestructor):
when defined(nimAllowNonVarDestructor) and arcLikeMem:
proc `=destroy`*(x: string) {.inline, magic: "Destroy".} =
discard

proc `=destroy`*[T](x: seq[T]) {.inline, magic: "Destroy".} =
discard

proc `=destroy`*[T](x: ref T) {.inline, magic: "Destroy".} =
discard

Expand Down
2 changes: 1 addition & 1 deletion tests/arc/topt_no_cursor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ try:
finally:
`=destroy`(splitted)
finally:
`=destroy_1`(lan_ip)
`=destroy`(lan_ip)
-- end of expandArc ------------------------
--expandArc: mergeShadowScope
Expand Down
12 changes: 6 additions & 6 deletions tests/destructor/tv2_cast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ data =
:tmpD_2))
:tmpD
`=destroy`(:tmpD_2)
`=destroy_1`(:tmpD_1)
`=destroy_1`(data)
`=destroy`(:tmpD_1)
`=destroy`(data)
-- end of expandArc ------------------------
--expandArc: main1
Expand All @@ -37,8 +37,8 @@ data =
:tmpD_1))
:tmpD
`=destroy`(:tmpD_1)
`=destroy_1`(data)
`=destroy_1`(s)
`=destroy`(data)
`=destroy`(s)
-- end of expandArc ------------------------
--expandArc: main2
Expand All @@ -54,7 +54,7 @@ data =
:tmpD_1))
:tmpD
`=destroy`(:tmpD_1)
`=destroy_1`(data)
`=destroy`(data)
`=destroy`(s)
-- end of expandArc ------------------------
--expandArc: main3
Expand All @@ -73,7 +73,7 @@ data =
:tmpD
`=destroy`(:tmpD_2)
`=destroy`(:tmpD_1)
`=destroy_1`(data)
`=destroy`(data)
-- end of expandArc ------------------------
'''
"""
Expand Down

0 comments on commit af8fe5b

Please sign in to comment.