diff --git a/compiler/injectdestructors.nim b/compiler/injectdestructors.nim index 1d73e4ee11..4a17dd7015 100644 --- a/compiler/injectdestructors.nim +++ b/compiler/injectdestructors.nim @@ -787,6 +787,12 @@ template handleNestedTempl(n, processCall: untyped, willProduceStmt = false, result = nil assert(false) + if willProduceStmt: + # The value is now produced by statements in the branches. Keeping the + # control-flow node's expression type would make code generators allocate + # a second, unused destination for it. + result.typ = nil + proc pRaiseStmt(n: PNode, c: var Con; s: var Scope): PNode = if optOwnedRefs in c.graph.config.globalOptions and n[0].kind != nkEmpty: if n[0].kind in nkCallKinds: diff --git a/tests/ccgbugs/t26119.nim b/tests/ccgbugs/t26119.nim new file mode 100644 index 0000000000..0ead6520ef --- /dev/null +++ b/tests/ccgbugs/t26119.nim @@ -0,0 +1,20 @@ +discard """ + targets: "c cpp" + matrix: "--mm:refc; --mm:orc" + ccodeCheck: "\\i !@('W T' \\d+ '_;')" +""" + +type W {.exportc.} = object + data: array[1000, byte] + items: seq[int] + +# bug #26119: assigning a try expression directly to the result must not leave +# an unused temporary of the result type in the generated procedure. +proc y(): W {.exportc.} = + try: + default(W) + except CatchableError: + default(W) + +var v = y() +doAssert v.data[0] == 0 and v.items.len == 0