From 5628cd5de1fd0e7c8e12a3e3b7f4741ee4199a9e Mon Sep 17 00:00:00 2001 From: flywind Date: Tue, 2 Mar 2021 19:41:55 +0800 Subject: [PATCH] attempt to fix #16374 (#17232) * remove unnecessary when statement * remove outdated codes * attempt to fix #16374 * fix --- compiler/cgen.nim | 7 ++++++- tests/ccgbugs/t16374.nim | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/ccgbugs/t16374.nim diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 165e4c1155..f870828662 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 0000000000..8ccfa4815c --- /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() +