summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/destroyer.nim6
-rw-r--r--tests/destructor/tcast.nim18
2 files changed, 24 insertions, 0 deletions
diff --git a/compiler/destroyer.nim b/compiler/destroyer.nim
index 22ace3634..6c47c8884 100644
--- a/compiler/destroyer.nim
+++ b/compiler/destroyer.nim
@@ -601,6 +601,12 @@ proc p(n: PNode; c: var Con): PNode =
   of nkNone..nkNilLit, nkTypeSection, nkProcDef, nkConverterDef, nkMethodDef,
       nkIteratorDef, nkMacroDef, nkTemplateDef, nkLambda, nkDo, nkFuncDef:
     result = n
+  of nkCast, nkHiddenStdConv, nkHiddenSubConv, nkConv:
+    result = copyNode(n)
+    # Destination type
+    result.add n[0]
+    # Analyse the inner expression
+    result.add p(n[1], c)
   else:
     result = copyNode(n)
     recurse(n, result)
diff --git a/tests/destructor/tcast.nim b/tests/destructor/tcast.nim
new file mode 100644
index 000000000..7d5a1954c
--- /dev/null
+++ b/tests/destructor/tcast.nim
@@ -0,0 +1,18 @@
+discard """
+  cmd: '''nim c --newruntime $file'''
+"""
+
+# Make sure we don't walk cast[T] type section while injecting sinks/destructors
+block:
+  type
+    XY[T] = object
+      discard
+
+  proc `=`[T](x: var XY[T]; v: XY[T]) {.error.}
+  proc `=sink`[T](x: var XY[T]; v: XY[T]) {.error.}
+
+  proc main[T]() =
+    var m = cast[ptr XY[T]](alloc0(sizeof(XY[T])))
+    doAssert(m != nil)
+
+  main[int]()