Skip to content

codegen: always wrap register_function/add_member_function in sol::overload(...) - #315

Merged
joergbrech merged 2 commits into
mainfrom
fix-overload-issue
Nov 12, 2026
Merged

joergbrech merged 2 commits into
mainfrom
fix-overload-issue

Conversation

@joergbrech

Copy link
Copy Markdown
Collaborator

Summary

Fixes #314.

render_module() only wrapped a same-named group of callables in
sol::overload(...) when more than one real C++ overload was discovered
(len(overloads) > 1). For exactly one overload, it spliced a
CodeGenerator subclass's own emit_callable() return value bare into
register_function()/add_member_function() — both single-Func
template-parameter calls, not variadic.

emit_callable()'s own docstring already allows a subclass to return
several candidate lambdas for one Callable by embedding extra top-level
commas (the same thing emit_constructor() has always been allowed to do,
relied on by add_constructors()'s own unconditional flat/variadic
splicing). Once a subclass legitimately needs that for a single-overload
callable — e.g. widening one reference parameter into several
sol2-exact-match candidates across a class hierarchy, since sol2's
base-class-aware usertype conversion only reliably applies to by-value
parameters, not reference ones — the bare splice above produced several
literal extra positional arguments to a function taking only one. Hard
compile error.

Fix

Always wrap in sol::overload(...), regardless of len(overloads). A
sol::overload(...) containing exactly one function behaves identically to
the bare lambda it replaces (it's just sol::overload_set<F> with one F),
so this is strictly more general, not a behavior change for any existing
subclass.

Deliberately not done the other way (having emit_callable() wrap its own
multi-candidate output in sol::overload(...) and letting render_module()
nest it inside another sol::overload(...) for the len(overloads) > 1
case): confirmed directly against the vendored sol2 source
(sol/function_types_overloaded.hpp's overloaded_function::call) that each
candidate needs a statically-deducible arity/signature via function_traits
to participate in overload_match, and a nested overload_set has none.

Testing

Verified against grunk-geoml's real use case (geoml::repeat_shape widened
from 1 to 9 candidates): generated code now compiles, links, and the
previously-failing recipe runs correctly end-to-end.

No existing test exercises render_module()'s wrapping decision at the
Python level directly — covered instead by every downstream plugin's own
generated-code build, which already serves as the integration test for this
code path.


Implemented with Claude

@joergbrech
joergbrech merged commit 6bf6424 into main Nov 12, 2026
24 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant