RFC: Field attributes
Nobody has claimed this yet.
- Dominant language
- Lean
- Stars
- 9.2k
- Forks
- 990
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 175
Description
Proposal
Structure fields in Lean generate projection functions, but it is currently impossible to attach attributes to those projections at the point of declaration. Any attribute on a field is rejected with "Invalid attribute: Attributes cannot be added to fields". This proposal lifts that restriction and makes it so that users can annotate fields directly instead of writing separate attribute commands after the structure:
-- Today you must write this separately:
structure Config where
oldField : Nat
normalize : String → String
attribute [deprecated "Use newField instead"] Config.oldField
attribute [simp] Config.normalize
-- With this proposal:
structure Config where
@[deprecated "Use newField instead"] oldField : Nat
@[simp] normalize : String → String
This also enables metadata attributes like a serde-style pattern where attributes annotate fields for deriving handlers rather than affecting the projection's elaboration behavior:
initialize jsonField : ParametricAttribute String
registerParametricAttribute `json_rename ...
structure User where
@[json_rename "user_name"] userName : String
@[json_rename "email_address"] email : String
I think this is great for projects using deriving for serialization (JSON, binary formats, ORMs), documentation generators that want structured per-field metadata and any user who wants @[simp], @[inline], or @[deprecated] on a specific projection without a post-hoc attribute command. The pattern is familiar to users coming from Rust (#[serde(...)]), Haskell (field annotations) and Java/Kotlin (annotation processors).
The implementation is minimal, the change removes two lines from checkValidFieldModifier and adds five lines to the finalize step in Structure.lean, following the identical pattern already used for applying docstrings to field projections. No new persistent storage or data types are needed since attributes are applied to the projection declaration via the existing Term.applyAttributes infrastructure, exactly as they would be if written as a standalone attribute command after the structure.
Community Feedback
Discussed on Lean Zulip: https://leanprover.zulipchat.com/#narrow/channel/113488-general/topic/Field.20attributes/with/582642404. No replies at time of submission and no prior issues found on this specific feature.
A prototype implementation is available at SrGaabriel/lean4 with tests passing on CI.
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
Start in Structure.lean by reviewing checkValidFieldModifier and the structure finalization step, following the existing pattern for applying docstrings to field projections. Use the prototype and its CI tests as a reference; done means declaration-site attributes are accepted and applied to generated projections, including metadata attributes.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 65/100