summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorflywind <xzsflywind@gmail.com>2021-03-02 19:41:55 +0800
committerGitHub <noreply@github.com>2021-03-02 19:41:55 +0800
commit5628cd5de1fd0e7c8e12a3e3b7f4741ee4199a9e (patch)
tree0a8e3841003762722e32f888918c87ce54553a3b
parentab780f66ef67ea0333278d75e4af064bcae71c57 (diff)
downloadNim-5628cd5de1fd0e7c8e12a3e3b7f4741ee4199a9e.tar.gz
attempt to fix #16374 (#17232)
* remove unnecessary when statement

* remove outdated codes

* attempt to fix #16374

* fix
-rw-r--r--compiler/cgen.nim7
-rw-r--r--tests/ccgbugs/t16374.nim38
2 files changed, 44 insertions, 1 deletions
diff --git a/compiler/cgen.nim b/compiler/cgen.nim
index 165e4c115..f87082866 100644
--- a/compiler/cgen.nim
+++ b/compiler/cgen.nim
@@ -406,7 +406,12 @@ proc resetLoc(p: BProc, loc: var TLoc) =
   if isImportedCppType(typ): return
   if optSeqDestructors in p.config.globalOptions and typ.kind in {tyString, tySequence}:
     assert rdLoc(loc) != nil
-    linefmt(p, cpsStmts, "$1.len = 0; $1.p = NIM_NIL;$n", [rdLoc(loc)])
+
+    let atyp = skipTypes(loc.t, abstractInst)
+    if atyp.kind in {tyVar, tyLent}:
+      linefmt(p, cpsStmts, "$1->len = 0; $1->p = NIM_NIL;$n", [rdLoc(loc)])
+    else:
+      linefmt(p, cpsStmts, "$1.len = 0; $1.p = NIM_NIL;$n", [rdLoc(loc)])
   elif not isComplexValueType(typ):
     if containsGcRef:
       var nilLoc: TLoc
diff --git a/tests/ccgbugs/t16374.nim b/tests/ccgbugs/t16374.nim
new file mode 100644
index 000000000..8ccfa4815
--- /dev/null
+++ b/tests/ccgbugs/t16374.nim
@@ -0,0 +1,38 @@
+discard """
+  matrix: "--gc:refc; --gc:orc"
+"""
+
+block:
+  iterator mvalues(t: var seq[seq[int]]): var seq[int] =
+    yield t[0]
+
+  var t: seq[seq[int]]
+
+  while false:
+    for v in t.mvalues:
+      discard
+
+  proc ok =
+    while false:
+      for v in t.mvalues:
+        discard
+
+  ok()
+
+block:
+  iterator mvalues(t: var seq[seq[int]]): lent seq[int] =
+    yield t[0]
+
+  var t: seq[seq[int]]
+
+  while false:
+    for v in t.mvalues:
+      discard
+
+  proc ok =
+    while false:
+      for v in t.mvalues:
+        discard
+
+  ok()
+