Skip to content

Refactor FromArgs derive to compute accurate arity bounds #5980

@coderabbitai

Description

@coderabbitai

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:

  1. First pass: Parse attributes from all fields to extract attr.kind and attr.default
  2. Second pass: Generate field code using parsed attributes
  3. Compute bounds: Calculate accurate arity bounds as:
    • min_arity = number of non-default PositionalOnly + PositionalOrKeyword fields
    • max_arity = total number of PositionalOnly + PositionalOrKeyword fields

References

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
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions