Repository navigation
Description
DONE. Eleven sites in the declarations named a conversion by
hand that a nanobind type_caster now does once. Merged from the
spike workspace; check and 205 tests pass in the main tree.
Where it comes from
#600 deleted cpp/derived_path.hpp by teaching the union's
alias to say what C++ holds it. The emitter writes as_arms,
from_arms and held from that fact now, and no hand-written line
survives.
The conversion still has a NAME, though, and eleven bodies say it:
decl/derived_path.py SingleDerivedPathBuilt.__init__ huggorm::held
decl/derived_path.py SingleDerivedPathBuilt.drv_path huggorm::as_arms
decl/derived_path.py DerivedPathBuilt.__init__ huggorm::held
decl/derived_path.py DerivedPathBuilt.drv_path huggorm::as_arms
decl/store.py query_missing huggorm::from_arms
decl/store.py print_derived_path huggorm::from_arms
decl/store.py parse_derived_path huggorm::as_arms
decl/store.py query_path_info *self.query...()
Plus *self.drvPath twice, inside the two as_arms calls above.
That is goal 3: a rule applied identically in eleven places belongs in
the emitter. It is not a violation today - a Cxx body is the
sanctioned hatch and the function it calls is generated - but the
declarations still carry a line that says nothing about Nix.
What a caster would change
_bare puts std::variant<nix::StorePath, nix::DerivedPathBuilt> in
every signature, and the comment above it says why:
nanobind's caster is specialised on std::variant exactly, not on
something deriving from one. So the binding takes the arms the
PYTHON side has and a body converts, which is a decision and belongs
in a body.
A generated type_caster<nix::DerivedPath> removes that. It composes
with the caster nanobind already ships - make_caster<Arms> - and
does the visit in from_cpp and the build in from_python, which is
as_arms and from_arms under different names. Then the signature is
nix::DerivedPath and every body loses its call:
parse_derived_path return nix::DerivedPath::parse(self.config, target);
print_derived_path return target.to_string(self.config);
query_missing return self.queryMissing(targets);
A second caster, for nix::ref<const T>, takes the rest.
SingleDerivedPathBuilt::drvPath is one, so drv_path becomes
@reads("drvPath") and the two __init__ bodies stop calling held.
Store::queryPathInfo returns one, so query_path_info stops
dereferencing.
What it would cost, and why it is not done yet
It writes C++ shapes this repo has not written. A caster is not a
function the generated code calls; it is a specialisation the compiler
finds. NB_TYPE_CASTER, cast_t, movable_cast_t and the
from_python / from_cpp pair are nanobind's protocol, and getting
one wrong fails at RUNTIME with a bad_cast out of module init rather
than at compile time.
Neither union is default-constructible. DerivedPathOpaque holds
a nix::StorePath, which has no default constructor, so the caster
needs the std::optional-storage dance nanobind's own variant caster
does for exactly this reason. That is real code, not a template.
CLAUDE.md wants Carl's word first. Every line of C++ the codegen
did not write needs approval in advance, and a feasibility spike is C++
written by hand even when the end state is generated.
What the declaration would have to say
Probably nothing new for the union: Variant(cxx=, raw=, header=, wraps=) already carries every fact the caster needs, which is the
reason this is worth doing rather than a second design.
The ref caster needs one fact this repo has not stated: that
nix::ref<T> is a non-nullable handle, and how to make one
(nix::make_ref<T>). Decl.holder says something close for
shared_ptr and may be the right place.
Spiked in a jj workspace, and it works
Built in .claude/worktrees/caster-spike, so the main tree never
carried it. Two commits there: the caster, and deleting held once
nothing called it.
Result: 205 tests pass, check passes, and no declaration body
names a conversion any more. The seven that did now say plain
libstore:
query_missing return self.queryMissing(targets);
print_derived_path return target.to_string(self.config);
parse_derived_path return nix::DerivedPath::parse(self.config, target);
...Built.drv_path return *self.drvPath; (x2)
...Built.__init__ new (self) nix::SingleDerivedPathBuilt{
nix::make_ref<nix::SingleDerivedPath>(drv_path),
output}; (x2 in shape)
The signature says nix::DerivedPath now, which is what libstore
says. _bare returns the union's own cxx when its alias declares a
Variant, and _arms_type is the std::variant the caster casts
THROUGH - the conversions and the caster are the only two things that
still name it.
The PYTHON surface does not move. The stubs still say
def parse_derived_path(self, target: str) -> DerivedPath, because
the caster changes what C++ passes, not what Python sees.
The caster, and why it is not the macro
NB_TYPE_CASTER declares Value value;. Neither union is
default-constructible - the opaque arm holds a nix::StorePath,
which has no default constructor - so the storage is a
std::optional and the three cast operators reach through it. That
is the same reason nanobind's own variant caster does not use the
macro.
from_python runs the arms' caster and calls from_arms; from_cpp
calls as_arms and hands the result to the arms' caster. Both
conversions were already generated, so the caster is where they are
CALLED rather than new logic.
Proved by breaking, and the first attempt was wrong
Removing the caster and compiling succeeded. That is the failure
mode includes() already warns about: a missing caster does not fail
at compile time, because nanobind falls back to type_caster_base.
Removing it and running the SUITE fails, and an existing gate catches
it before any test does - the signature gate in huggorm-generated
diffs nanobind's introspected signature against the declared one:
Store.print_derived_path
nanobind: ("nix::StorePath | DerivedPathBuilt") -> str
declared: (StorePath | DerivedPathBuilt) -> str
The quotes are nanobind naming a type it has no caster for.
What is NOT worth doing: the ref caster
The plan named a second caster for nix::ref<T>, to turn drv_path
into @reads("drvPath") and query_path_info into a pointer bind.
It does not reach:
@reads emits return self.drvPath;, and the lambda's return type
comes from the DECLARED type - nix::SingleDerivedPath. The member
is a ref<const SingleDerivedPath>, which does not convert to one.
For @reads to work the emitter would have to know the member is a
ref, which is a fact no declaration states and which only this member
and queryPathInfo would use.
So Cxx("return *self.drvPath;") stays. It is one line, it says a
C++ fact about the member rather than about the binding, and the
alternative is a marker with two users that has to teach the emitter
a handle type.
Merged
The two spike commits landed with jj duplicate, and the gate is
green in the main tree. .claude/worktrees/caster-spike is forgotten.
The argument that decided it: the caster needs no new declared fact -
Variant(...) already carries the union's cxx, its raw(), its
header and its wrapped arm - so it is the same fact reaching one more
output. It costs 40 generated lines per union, and generated lines
are not a budget (CLAUDE.md); what it buys is that no declaration
body names a conversion, and that a signature says what libstore
says.
The one argument against was that a wrong caster fails late rather
than at compile time. That is true and it is COVERED: the signature
gate in huggorm-generated catches a missing one, which is exactly
how the failed perturbation was found.
Migrated from tasks/067-a-caster-instead-of-a-named-conversion.md.done.
Assisted-By: Opus 5
Activity