diff options
-rw-r--r-- | changelog.md | 2 | ||||
-rw-r--r-- | doc/destructors.rst | 7 |
2 files changed, 6 insertions, 3 deletions
diff --git a/changelog.md b/changelog.md index 1eda82297..36582eee2 100644 --- a/changelog.md +++ b/changelog.md @@ -143,6 +143,8 @@ ## Language changes +- The `=destroy` hook no longer has to reset its target, as the compiler now automatically inserts + wasMoved calls where needed. - In the newruntime it is now allowed to assign to the discriminator field without restrictions as long as case object doesn't have custom destructor. The discriminator value doesn't have to be a constant either. If you have a diff --git a/doc/destructors.rst b/doc/destructors.rst index 4285bad8b..fefc646f5 100644 --- a/doc/destructors.rst +++ b/doc/destructors.rst @@ -39,12 +39,12 @@ written as: if x.data != nil: for i in 0..<x.len: `=destroy`(x[i]) dealloc(x.data) - x.data = nil proc `=`*[T](a: var myseq[T]; b: myseq[T]) = # do nothing for self-assignments: if a.data == b.data: return `=destroy`(a) + wasMoved(a) a.len = b.len a.cap = b.cap if b.data != nil: @@ -56,6 +56,7 @@ written as: # move assignment, optional. # Compiler is using `=destroy` and `copyMem` when not provided `=destroy`(a) + wasMoved(a) a.len = b.len a.cap = b.cap a.data = b.data @@ -120,7 +121,6 @@ The general pattern in ``=destroy`` looks like: # first check if 'x' was moved to somewhere else: if x.field != nil: freeResource(x.field) - x.field = nil @@ -149,6 +149,7 @@ The general pattern in ``=sink`` looks like: proc `=sink`(dest: var T; source: T) = `=destroy`(dest) + wasMoved(dest) dest.field = source.field @@ -178,6 +179,7 @@ The general pattern in ``=`` looks like: # protect against self-assignments: if dest.field != source.field: `=destroy`(dest) + wasMoved(dest) dest.field = duplicateResource(source.field) @@ -522,7 +524,6 @@ Let ``W`` be an ``owned ref`` type. Conceptually its hooks look like: if x != nil: assert x.refcount == 0, "dangling unowned pointers exist!" `=destroy`(x[]) - x = nil proc `=`(x: var W; y: W) {.error: "owned refs can only be moved".} |