fixes #22286; enforce Non-var T destructors by nimPreviewNonVarDestructor (#22975)

fixes #22286
ref https://forum.nim-lang.org/t/10642

For backwards compatibilities, we might need to keep the changes under a
preview compiler flag. Let's see how many packags it break.

**TODO** in the following PRs

- [ ] Turn the `var T` destructors warning into an error with
`nimPreviewNonVarDestructor`

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
(cherry picked from commit 379299a5ac)
This commit is contained in:
ringabout
2023-11-26 01:27:27 +08:00
committed by narimiran
parent cbcf6c5be6
commit be99f2fed8
13 changed files with 86 additions and 42 deletions

View File

@@ -368,19 +368,24 @@ proc arrPut[I: Ordinal;T,S](a: T; i: I;
const arcLikeMem = defined(gcArc) or defined(gcAtomicArc) or defined(gcOrc)
when defined(nimAllowNonVarDestructor) and arcLikeMem:
proc `=destroy`*(x: string) {.inline, magic: "Destroy".} =
when defined(nimAllowNonVarDestructor) and arcLikeMem and defined(nimPreviewNonVarDestructor):
proc `=destroy`*[T](x: T) {.inline, magic: "Destroy".} =
## Generic `destructor`:idx: implementation that can be overridden.
discard
else:
proc `=destroy`*[T](x: var T) {.inline, magic: "Destroy".} =
## Generic `destructor`:idx: implementation that can be overridden.
discard
proc `=destroy`*[T](x: seq[T]) {.inline, magic: "Destroy".} =
discard
when defined(nimAllowNonVarDestructor) and arcLikeMem:
proc `=destroy`*(x: string) {.inline, magic: "Destroy".} =
discard
proc `=destroy`*[T](x: ref T) {.inline, magic: "Destroy".} =
discard
proc `=destroy`*[T](x: seq[T]) {.inline, magic: "Destroy".} =
discard
proc `=destroy`*[T](x: var T) {.inline, magic: "Destroy".} =
## Generic `destructor`:idx: implementation that can be overridden.
discard
proc `=destroy`*[T](x: ref T) {.inline, magic: "Destroy".} =
discard
when defined(nimHasDup):
proc `=dup`*[T](x: T): T {.inline, magic: "Dup".} =