fixes #21987; don't create type bound ops for anything in a function with a nodestroy pragma (#21992)

* fixes #21987; don't create type bound ops for anything in a function with a `nodestroy` pragma

* add a comment
This commit is contained in:
ringabout
2023-06-04 14:37:58 +08:00
committed by GitHub
parent 25fe4124e6
commit 929cb4d601
2 changed files with 52 additions and 1 deletions

View File

@@ -90,7 +90,10 @@ const
errLetNeedsInit = "'let' symbol requires an initialization"
proc createTypeBoundOps(tracked: PEffects, typ: PType; info: TLineInfo) =
if typ == nil: return
if typ == nil or sfGeneratedOp in tracked.owner.flags:
# don't create type bound ops for anything in a function with a `nodestroy` pragma
# bug #21987
return
when false:
let realType = typ.skipTypes(abstractInst)
if realType.kind == tyRef and

View File

@@ -89,3 +89,51 @@ block: # bug #21974
var a = newTest[X]()
a.push((1, "One"))
doAssert a.pop.value == "One"
# bug #21987
type
EmbeddedImage* = distinct Image
Image = object
len: int
proc imageCopy*(image: Image): Image {.nodestroy.}
proc `=destroy`*(x: var Image) =
discard
proc `=sink`*(dest: var Image; source: Image) =
`=destroy`(dest)
wasMoved(dest)
proc `=dup`*(source: Image): Image {.nodestroy.} =
result = imageCopy(source)
proc `=copy`*(dest: var Image; source: Image) =
dest = imageCopy(source) # calls =sink implicitly
proc `=destroy`*(x: var EmbeddedImage) = discard
proc `=dup`*(source: EmbeddedImage): EmbeddedImage {.nodestroy.} = source
proc `=copy`*(dest: var EmbeddedImage; source: EmbeddedImage) {.nodestroy.} =
dest = source
proc imageCopy*(image: Image): Image =
result = image
proc main2 =
block:
var a = Image(len: 2).EmbeddedImage
var b = Image(len: 1).EmbeddedImage
b = a
doAssert Image(a).len == 2
doAssert Image(b).len == 2
block:
var a = Image(len: 2)
var b = Image(len: 1)
b = a
doAssert a.len == 2
doAssert b.len == 0
main2()