summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/ccgexprs.nim9
-rw-r--r--tests/arc/tconst_to_sink.nim25
2 files changed, 33 insertions, 1 deletions
diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim
index f133b97a2..1bed4bc6c 100644
--- a/compiler/ccgexprs.nim
+++ b/compiler/ccgexprs.nim
@@ -1340,7 +1340,14 @@ proc genObjConstr(p: BProc, e: PNode, d: var TLoc) =
   #echo rendertree e, " ", e.isDeepConstExpr
   # inheritance in C++ does not allow struct initialization so
   # we skip this step here:
-  if not p.module.compileToCpp:
+  if not p.module.compileToCpp and optSeqDestructors notin p.config.globalOptions:
+    # disabled optimization: it is wrong for C++ and now also
+    # causes trouble for --gc:arc, see bug #13240
+    #[
+      var box: seq[Thing]
+      for i in 0..3:
+        box.add Thing(s1: "121") # pass by sink can mutate Thing.
+    ]#
     if handleConstExpr(p, e, d): return
   var t = e.typ.skipTypes(abstractInstOwned)
   let isRef = t.kind == tyRef
diff --git a/tests/arc/tconst_to_sink.nim b/tests/arc/tconst_to_sink.nim
new file mode 100644
index 000000000..ddcc46e67
--- /dev/null
+++ b/tests/arc/tconst_to_sink.nim
@@ -0,0 +1,25 @@
+discard """
+  output: '''@[(s1: "333", s2: ""), (s1: "abc", s2: "def"), (s1: "3x", s2: ""), (s1: "3x", s2: ""), (s1: "3x", s2: ""), (s1: "3x", s2: ""), (s1: "lastone", s2: "")]'''
+  cmd: "nim c --gc:arc $file"
+"""
+
+# bug #13240
+
+type
+  Thing = object
+    s1: string
+    s2: string
+
+var box: seq[Thing]
+
+const c = [Thing(s1: "333"), Thing(s1: "abc", s2: "def")]
+
+for i in 0..high(c):
+  box.add c[i]
+
+for i in 0..3:
+  box.add Thing(s1: "3x")
+
+box.add Thing(s1: "lastone")
+
+echo box