From 641243b70e6492d4c0e567733dff78aa8653ec2b Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 4 Sep 2026 14:18:07 +0800 Subject: [PATCH] fixes #26119; stack usage increase on try/except expression (#26166) fixes #26119 --- compiler/injectdestructors.nim | 6 ++++++ tests/ccgbugs/t26119.nim | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 tests/ccgbugs/t26119.nim 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