Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions python/grunk/codegen/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1656,13 +1656,27 @@ def render_module(result: ModuleResult, hooks: CodeGenerator,
if not overloads[0][0].is_static:
continue
overloads = ambiguity_safe_overload_order(overloads, hooks)
if len(overloads) == 1:
c, kinds = overloads[0]
fun_expr = hooks.emit_callable(c, kinds, ctor_mode)
else:
fun_expr = "sol::overload(\n " + ",\n ".join(
hooks.emit_callable(c, kinds, ctor_mode) for c, kinds in overloads
) + "\n )"
# Always wrapped in sol::overload(...), even for a single discovered
# C++ overload (len(overloads) == 1 used to skip this wrapping as a
# premature optimization) - a CodeGenerator subclass's own
# emit_callable() is free to return more than one candidate lambda
# for a SINGLE Callable by embedding extra top-level commas in its
# returned text (the same thing emit_constructor() has always been
# allowed to do, relied on by add_constructors()'s own unconditional
# flat/variadic splicing below) - e.g. to widen one concrete
# parameter type into several sol2-exact-match candidates covering
# a whole class hierarchy. Without this wrapping, such a widened
# single-overload result was spliced as literal extra positional
# arguments directly into a plugin_namespace::register_function()/
# usertype_proxy::add_member_function() call, both of which take
# exactly one Func template parameter - a hard compile error, not a
# silent bug (found via a real downstream plugin needing to widen a
# single-overload free function exactly this way). sol::overload(...)
# containing exactly one function works correctly too, so this is
# strictly more general, not merely a special-case fix.
fun_expr = "sol::overload(\n " + ",\n ".join(
hooks.emit_callable(c, kinds, ctor_mode) for c, kinds in overloads
) + "\n )"

# A *genuine* C++ namespace's own free functions (result.entity_kind
# == "namespace") get registered under their own bare name where
Expand Down Expand Up @@ -1756,13 +1770,11 @@ def render_module(result: ModuleResult, hooks: CodeGenerator,
lines.append(" .add_constructors(\n " + ",\n ".join(ctor_exprs) + "\n )")

for method_name, overloads in instance_methods.items():
if len(overloads) == 1:
c, kinds = overloads[0]
fun_expr = hooks.emit_callable(c, kinds, ctor_mode)
else:
fun_expr = "sol::overload(\n " + ",\n ".join(
hooks.emit_callable(c, kinds, ctor_mode) for c, kinds in overloads
) + "\n )"
# Always wrapped, same reasoning as the free-function/static-method
# loop above - see its own comment for the full "why".
fun_expr = "sol::overload(\n " + ",\n ".join(
hooks.emit_callable(c, kinds, ctor_mode) for c, kinds in overloads
) + "\n )"
if method_name.startswith("__meta_"):
# An operator group (see process_module's own "__meta_"
# keying) - a real sol2/Lua metamethod slot, not a named
Expand Down
80 changes: 80 additions & 0 deletions tests/python/test_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,3 +941,83 @@ def test_run_end_to_end_loads_code_generator_from_config(tmp_path):
cpp_source = (output_dir / "widget.cpp").read_text()
assert "sol::object factor" in cpp_source
assert "toy_ns::to_widget(factor)" in cpp_source


MULTI_CANDIDATE_WIDGET_HEADER = """\
class Widget {
public:
Widget();
double value() const;
static double unit();
};
"""


def test_run_end_to_end_wraps_single_overload_widened_into_several_candidates(tmp_path):
"""Regression test for the bug this fix addresses: render_module() used to
only wrap a same-named group of callables in sol::overload(...) when more
than one real C++ overload was discovered for that name. For exactly one
overload, it spliced a CodeGenerator subclass's own emit_callable() return
value bare, as the sole argument to
grunk::plugin_namespace::register_function()/
grunk::usertype_proxy::add_member_function() - both take exactly one Func
template parameter (see usertype_proxy.hpp's own add_member_function, not
its plural add_member_functions sibling, which already wraps in
sol::overload(...) itself for exactly this reason).

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) - e.g. to widen one parameter's concrete type into several
sol2-exact-match candidates covering a whole class hierarchy, since sol2's
own base-class-aware usertype conversion only reliably applies to a
by-value parameter, not a reference one (see grunk-geoml's own
codegen/hooks.py ALL_TOPODS_SHAPE_WIDEN_TYPES for the real-world
motivating case: geoml::repeat_shape has exactly one real C++ overload,
yet needs widening into 9 candidates). Before this fix, such a widened
single-overload result produced several literal extra positional
arguments to a function taking only one - a hard compile error.

Hooks below returns the SAME fake candidate lambda text twice, comma-
joined, for every non-constructor/non-operator Callable - a minimal stand-
in for a real multi-candidate widening, exercising both the free-function/
static-method loop ("unit") and the instance-method loop ("value") in one
pass."""
include_dir = tmp_path / "include"
include_dir.mkdir()
(include_dir / "widget.hpp").write_text(MULTI_CANDIDATE_WIDGET_HEADER)

(tmp_path / "hooks.py").write_text(
"from grunk.codegen import generate\n"
"class Hooks(generate.CodeGenerator):\n"
" def emit_callable(self, c, kinds, ctor_mode):\n"
" base = super().emit_callable(c, kinds, ctor_mode)\n"
" if c.kind in ('constructor', 'operator'):\n"
" return base\n"
" return base + ',\\n ' + base\n"
)

config_path = tmp_path / "config.yml"
config_path.write_text(yaml.safe_dump({
"code_generator": "hooks.py",
"modules": [{"name": "widget", "headers": ["widget.hpp"]}],
}))

output_dir = tmp_path / "generated"
args = argparse.Namespace(config=config_path, include_dir=include_dir, output_dir=output_dir)
assert generate.run(args) == 0

cpp_source = (output_dir / "widget.cpp").read_text()

# The static method ("unit", registered as a free function under its
# composed name) - exactly one real C++ overload, widened into two fake
# candidates - must be wrapped in a single sol::overload(...), not spliced
# bare into register_function()'s own single Func parameter.
assert 'ns.register_function("Widget_unit", sol::overload(' in cpp_source
assert cpp_source.count("return Widget::unit();") == 2

# The instance method ("value") - same shape, add_member_function() this
# time.
assert '.add_member_function("value", sol::overload(' in cpp_source
assert cpp_source.count("return self.value();") == 2
Loading