summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/destroyer.nim4
-rw-r--r--tests/destructor/tdiscard.nim18
2 files changed, 22 insertions, 0 deletions
diff --git a/compiler/destroyer.nim b/compiler/destroyer.nim
index 0570873ea..dd12a7966 100644
--- a/compiler/destroyer.nim
+++ b/compiler/destroyer.nim
@@ -601,6 +601,10 @@ proc p(n: PNode; c: var Con): PNode =
   of nkNone..nkNilLit, nkTypeSection, nkProcDef, nkConverterDef, nkMethodDef,
       nkIteratorDef, nkMacroDef, nkTemplateDef, nkLambda, nkDo, nkFuncDef:
     result = n
+  of nkDiscardStmt:
+    result = n
+    if n[0].typ != nil and hasDestructor(n[0].typ):
+      result = genDestroy(c, n[0].typ, n[0])
   of nkCast, nkHiddenStdConv, nkHiddenSubConv, nkConv:
     result = copyNode(n)
     # Destination type
diff --git a/tests/destructor/tdiscard.nim b/tests/destructor/tdiscard.nim
new file mode 100644
index 000000000..2e4a4b285
--- /dev/null
+++ b/tests/destructor/tdiscard.nim
@@ -0,0 +1,18 @@
+type
+  O = object
+
+var dCalls = 0
+
+proc `=destroy`(x: var O) = inc dCalls
+proc `=sink`(x: var O, y: O) = doAssert false
+
+proc newO(): O = discard
+
+proc main() =
+  doAssert dCalls == 0
+  discard newO()
+  doAssert dCalls == 1
+  discard newO()
+  doAssert dCalls == 2
+
+main()