[BUG] [mojo-lang] `Writable` trait not recognized for a type with a `VariadicPack` field
Open
Nobody has claimed this yet.
bug
mojo
- Dominant language
- Mojo
- Stars
- 29.8k
- Forks
- 3.2k
- PR merge metrics
- No merged PRs in 30d
Description
Bug description
comptime _write_fn_type = fn[W: Writer] (mut writer: W) unified
struct WriteFn[*write_fn_types: _write_fn_type](Writable):
comptime V = VariadicPack[
elt_is_mutable=True,
origin=MutExternalOrigin,
True,
_write_fn_type,
*Self.write_fn_types,
]
var write_fns: Self.V
@implicit
fn __init__(out self, var *funcs: * Self.write_fn_types):
self.write_fns = rebind_var[type_of(self.write_fns)](funcs^)
fn __init__(
deinit lhs: WriteFn,
deinit rhs: WriteFn,
out self: WriteFn[
*Variadic.concat_types[lhs.write_fn_types, rhs.write_fn_types]
],
):
fn _lhs_handler[idx: Int](var elt: lhs.V.element_types[idx]) capturing:
self.write_fns[idx] = rebind_var[type_of(self.write_fns[idx])](elt^)
fn _rhs_handler[idx: Int](var elt: rhs.V.element_types[idx]) capturing:
comptime i = lhs.V.__len__() + idx
self.write_fns[i] = rebind_var[type_of(self.write_fns[i])](elt^)
lhs.write_fns^.consume_elements[_lhs_handler]()
rhs.write_fns^.consume_elements[_rhs_handler]()
__mlir_op.`lit.ownership.mark_initialized`(__get_mvalue_as_litref(self))
# fn append(var self, var rhs: WriteFn) -> type_of(WriteFn(self, rhs)):
# return {self, rhs}
# fn prepend(var self, var lhs: WriteFn) -> type_of(WriteFn(lhs, self)):
# return {lhs, self}
fn write_to(self, mut writer: Some[Writer]):
@parameter
for i in range(Self.V.__len__()):
self.write_fns[i](writer)
fn main():
var elems = [0, 1, 2]
fn _write_1(mut writer: Some[Writer]) unified {read}:
writer.write("List[Int]: ")
fn _write_2(mut writer: Some[Writer]) unified {read}:
writer.write("[")
for i, elem in enumerate(elems):
if i > 0:
writer.write(", ")
writer.write(elem)
writer.write("]")
var fn_1 = WriteFn(_write_1)
var fn_2 = WriteFn(_write_2)
# print(fn_1.append(fn_2))
# print(fn_2.prepend(fn_1))
print(WriteFn(fn_1^, fn_2^))
output
oss/modular/mojo/stdlib/std/format/__init__.mojo:194:8: note: function instantiation failed
oss/modular/mojo/stdlib/std/format/__init__.mojo:222:46: note: call expansion failed with parameter value(s): (..., ..., ...)
oss/modular/mojo/stdlib/std/format/__init__.mojo:261:4: note: function instantiation failed
oss/modular/mojo/stdlib/std/format/__init__.mojo:281:10: note: call expansion failed
oss/modular/mojo/stdlib/std/builtin/constrained.mojo:152:5: note: constraint failed: Could not derive Writable for WriteFn[<unprintable>] - member field `write_fns: VariadicPack[True, {}, True, <unprintable>, <unprintable>]` does not implement Writable
It seems it is trying to run the default trait implementation instead of using the one I implemented
Steps to reproduce
...
System information
Mojo 0.26.2.0.dev2026012806
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the provided Mojo example and start with the diagnostics referencing oss/modular/mojo/stdlib/std/format/init.mojo and std/builtin/constrained.mojo. Trace why Writable resolution rejects WriteFn when its member is a VariadicPack, then verify that the example recognizes the implemented trait without the reported constraint failure.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100