fix #25870 #25846: var return type in C++ backend - #26008
Open
nimamasl114514 wants to merge 5 commits into
Open
Conversation
nimamasl114514
force-pushed
the
fix-25870-final
branch
3 times, most recently
from
July 16, 2026 04:40
fbf941f to
68b3a92
Compare
genProcParams strips tfVarIsPtr from the proc signature so C++ procs with var T return produce T& instead of T*. However the call sites that assign results to T* temporaries (the else branches of fixupCall, genClosureCall, and genNamedParamCall) were not updated to match - they stored the T& result directly into a T* variable, causing a dangling pointer (SIGSEGV) or a clang compilation error on macOS. This broke std/oserrors.nim in C++ mode when msg[^1] appeared inside an and expression, triggering the splitDecls > 0 code path. Fix: wrap the call result with cAddr() when tfVarIsPtr is set on the return type, matching what getTempCpp already does.
nimamasl114514
force-pushed
the
fix-25870-final
branch
from
July 16, 2026 04:49
68b3a92 to
e0fb79d
Compare
genNamedParamCall only runs for sfNamedParamCall procs, which come from importObjC and compile to ObjC (.nim.m). ObjC has no C++ references, so var T is a pointer there and the cAddr() I added would produce T**. The compileToCpp branch above it is also dead since importObjC modules don't go through the C++ backend. Pointed out by demotomohiro.
…ath forces T* signature)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix #25870 #25846: C++ codegen for proc returning var T
The C++ codegen was generating conflicting types for procs with �ar T return:
the signature said T& but the internal result variable was T*, and callers
expected T& back. This caused link errors on some platforms (undefined symbols)
and clang errors on macOS.
The problem was that fVarIsPtr was being set too early (in semexprs) and leaked
into the proc type, so every place that looked at the return type saw T* instead
of T&. The fix moves the flag application into cgen where it belongs: we make a
shallow copy of the return type, set fVarIsPtr on the copy, and keep the
original clean for the signature.
There were also a few call sites that weren't updated when the signature changed
from T* to T&. Stuff like s[^1] in an �nd expression would hit the
splitDecls path and try to store a T& into a T* variable, which clang rightfully
refuses to compile. These now get the missing & operator.
Tested with
im c and
im cpp on the new test case, plus --threads:on.