From 73112d64a3824963f6e74a06b60d6b9b2d189050 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 26 Mar 2025 23:49:00 +0800 Subject: [PATCH] fixes #24793; Revert "remove special treatments of sinking const sequences (#24812) fixes #24793 There doesn't seem to have a better solution --- compiler/injectdestructors.nim | 38 +++++++++++++++++++++++-- tests/objects/tobject_default_value.nim | 8 ++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/compiler/injectdestructors.nim b/compiler/injectdestructors.nim index ce9164058a..90c83124b1 100644 --- a/compiler/injectdestructors.nim +++ b/compiler/injectdestructors.nim @@ -493,6 +493,25 @@ proc passCopyToSink(n: PNode; c: var Con; s: var Scope): PNode = # no need to destroy it. result.add tmp +proc isDangerousSeq(t: PType): bool {.inline.} = + let t = t.skipTypes(abstractInst) + result = t.kind == tySequence and tfHasOwned notin t.elementType.flags + +proc containsConstSeq(n: PNode): bool = + if n.kind == nkBracket and n.len > 0 and n.typ != nil and isDangerousSeq(n.typ): + return true + result = false + case n.kind + of nkExprEqExpr, nkExprColonExpr, nkHiddenStdConv, nkHiddenSubConv, nkCast: + result = containsConstSeq(n[1]) + of nkObjConstr, nkClosure: + for i in 1.. 0 and isDangerousSeq(ri.typ): + inc c.inEnsureMove, isEnsureMove + result = c.genCopy(dest, ri, flags) + dec c.inEnsureMove, isEnsureMove + result.add p(ri, c, s, consumed) + c.finishCopy(result, dest, flags, isFromSink = false) + else: + result = c.genSink(s, dest, p(ri, c, s, consumed), flags) + of nkObjConstr, nkTupleConstr, nkClosure, nkCharLit..nkNilLit: result = c.genSink(s, dest, p(ri, c, s, consumed), flags) of nkSym: if isSinkParam(ri.sym) and isLastRead(ri, c, s): diff --git a/tests/objects/tobject_default_value.nim b/tests/objects/tobject_default_value.nim index ffa08d4315..8b6ea812b7 100644 --- a/tests/objects/tobject_default_value.nim +++ b/tests/objects/tobject_default_value.nim @@ -811,3 +811,11 @@ template main {.dirty.} = static: main() main() + +block: + type + MyTyp = ref object + thing = initTable[string,string]() + + var t = MyTyp() + t.thing[""] = ""