From 7c3c474981164db0f4d63df6aa0b78205764d39a Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 20 Apr 2026 07:43:21 +0200 Subject: [PATCH] much better fix --- compiler/ccgexprs.nim | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index ea2a468219..b4edfcf6dd 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -1889,11 +1889,17 @@ proc genObjConstr(p: BProc, e: PNode, d: var TLoc) = var t = e.typ.skipTypes(abstractInstOwned) let isRef = t.kind == tyRef - # check if we need to construct the object in a temporary + # check if we need to construct the object in a temporary. + # A temp is needed when: + # - the constructor produces a ref (isRef) + # - the destination is not a writable location (d.k == locNone) + # - the constructed type differs from the destination type (subtype + # assignments need the genAssignment path for ObjectAssignmentDefect) + # - the constructor's field values may alias the destination (isPartOf) var useTemp = isRef or - (d.k notin {locTemp,locLocalVar,locGlobalVar,locParam,locField} and - d.storage != OnHeap) or + d.k == locNone or + (d.t != nil and not sameBackendType(t, d.t.skipTypes(abstractInstOwned))) or (isPartOf(d.lode, e) != arNo) var tmp: TLoc = default(TLoc)