summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2020-05-04 07:43:22 +0200
committerGitHub <noreply@github.com>2020-05-04 07:43:22 +0200
commitb6fb609e0d263a209c6db48dd367f13eb7f4ff87 (patch)
treef2f177bb2c63bd28f8927501f6be6f48cca27dd4
parent3e060cfb0ab6b1affe26a7b09e6b4192e0d68a7f (diff)
downloadNim-b6fb609e0d263a209c6db48dd367f13eb7f4ff87.tar.gz
destructors: don't produce stupid code for 'cast' (#14208) [backport:1.2]
* destructors: don't produce stupid code for 'cast'

* fixes #14207
-rw-r--r--compiler/dfa.nim4
-rw-r--r--compiler/injectdestructors.nim8
-rw-r--r--tests/arc/tarcmisc.nim14
3 files changed, 22 insertions, 4 deletions
diff --git a/compiler/dfa.nim b/compiler/dfa.nim
index 0cefec285..7db5f5f65 100644
--- a/compiler/dfa.nim
+++ b/compiler/dfa.nim
@@ -541,8 +541,8 @@ template genNoReturn(c: var Con; n: PNode) =
   c.code.add Instr(n: n, kind: goto, dest: high(int) - c.code.len)
 
 proc genRaise(c: var Con; n: PNode) =
-  genJoins(c, n)
   gen(c, n[0])
+  genJoins(c, n)
   if c.inTryStmt > 0:
     c.tryStmtFixups.add c.gotoI(n)
   else:
@@ -553,11 +553,11 @@ proc genImplicitReturn(c: var Con) =
     gen(c, c.owner.ast[resultPos])
 
 proc genReturn(c: var Con; n: PNode) =
-  genJoins(c, n)
   if n[0].kind != nkEmpty:
     gen(c, n[0])
   else:
     genImplicitReturn(c)
+  genJoins(c, n)
   genNoReturn(c, n)
 
 const
diff --git a/compiler/injectdestructors.nim b/compiler/injectdestructors.nim
index 680a38397..9e5207437 100644
--- a/compiler/injectdestructors.nim
+++ b/compiler/injectdestructors.nim
@@ -314,7 +314,7 @@ proc genDiscriminantAsgn(c: var Con; n: PNode): PNode =
   # discriminator is ordinal value that doesn't need sink destroy
   # but fields within active case branch might need destruction
 
-  # tmp to support self assignments 
+  # tmp to support self assignments
   let tmp = getTemp(c, n[1].typ, n.info)
   c.addTopVar(tmp)
 
@@ -329,7 +329,7 @@ proc genDiscriminantAsgn(c: var Con; n: PNode): PNode =
   if hasDestructor(objType):
     if objType.attachedOps[attachedDestructor] != nil and
         sfOverriden in objType.attachedOps[attachedDestructor].flags:
-      localError(c.graph.config, n.info, errGenerated, """Assignment to discriminant for object's with user defined destructor is not supported, object must have default destructor. 
+      localError(c.graph.config, n.info, errGenerated, """Assignment to discriminant for object's with user defined destructor is not supported, object must have default destructor.
 It is best to factor out piece of object that needs custom destructor into separate object or not use discriminator assignment""")
       result.add newTree(nkFastAsgn, le, tmp)
       return
@@ -957,6 +957,10 @@ proc p(n: PNode; c: var Con; mode: ProcessMode): PNode =
       for i in 0..<n.len:
         result[i] = p(n[i], c, mode)
       inc c.hasUnstructuredCf
+    of nkCast:
+      result = shallowCopy(n)
+      result[0] = n[0]
+      result[1] = p(n[1], c, mode)
     else:
       result = shallowCopy(n)
       for i in 0..<n.len:
diff --git a/tests/arc/tarcmisc.nim b/tests/arc/tarcmisc.nim
index ce8e902c6..c0583a640 100644
--- a/tests/arc/tarcmisc.nim
+++ b/tests/arc/tarcmisc.nim
@@ -1,5 +1,6 @@
 discard """
   output: '''
+123xyzabc
 destroyed: false
 destroyed: false
 closed
@@ -8,6 +9,19 @@ destroying variable
   cmd: "nim c --gc:arc $file"
 """
 
+proc takeSink(x: sink string): bool = true
+
+proc b(x: sink string): string =
+  if takeSink(x):
+    return x & "abc"
+
+proc bbb(inp: string) =
+  let y = inp & "xyz"
+  echo b(y)
+
+bbb("123")
+
+
 # bug #13691
 type Variable = ref object
   value: int