-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Description
Problem
The current impl_from_args
function in derive-impl/src/from_args.rs
computes arity bounds incorrectly. The generated fn arity()
returns 0..=#arity
(where #arity
is fields.len()
), which:
- Always allows zero arguments—even when some fields are required
- Counts keyword-only and defaulted fields toward both bounds
- Does not distinguish between positional and keyword-only parameters
Root Cause
The current structure makes it challenging to compute accurate arity bounds because generate_field
encapsulates both attribute parsing and code generation, and we lose access to the attr.kind
and attr.default
information after it returns the TokenStream
.
Solution
Refactor the code to separate attribute parsing from code generation:
- First pass: Parse attributes from all fields to extract
attr.kind
andattr.default
- Second pass: Generate field code using parsed attributes
- Compute bounds: Calculate accurate arity bounds as:
min_arity
= number of non-defaultPositionalOnly
+PositionalOrKeyword
fieldsmax_arity
= total number ofPositionalOnly
+PositionalOrKeyword
fields
References
- Original PR: Fix invalid args count msg #5960
- Discussion: Fix invalid args count msg #5960 (comment)
Implementation Notes
The refactoring should split generate_field
into separate functions for parsing and generation, allowing impl_from_args
to access the parsed attribute information needed for accurate arity calculation.
Metadata
Metadata
Assignees
Labels
No labels