Libs proposal: Policy for constifying traits and trait implementations
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.2k
- PR merge metrics
- PR metrics pending
Description
Context
For the past ~year we have been getting a number PRs that constify traits or their implementations. It seems like a number of these aren't intended with any specific goal other than completeness. There are a few concerns about this that I and other reviewers have brought up:
- A complete unstably constified standard library isn't necessary for proving out
consttraits, yet many PRs seem to be aiming for this. - Syntax changes before the RFC is accepted would cause churn (not the most concerning).
- Semantic changes to the RFC are less likely but still possible. Hopefully we won't need to worry about this; however, it is entirely possible for a logic change to mean we need to re-review or possibly remove every impl accepted thus far.
- Similar to the above: any necessary change or removal while const traits are unstable would have a ripple effect, which compounds with more dependent impls.
- This constification process can introduce less maintainable code. We have had real bugs from this.
- Constifications always introduces restrictions in how library code can be changed. There are also non-obvious interactions, such as https://github.com/rust-lang/rust/pull/155640#issuecomment-4300330699.
- Our review bandwidth is limited right now. The cost per additional
constimpl is often small but does add up, and any future changes will have a cost as well.
In short, it feels like we are acquiring tech debt with not enough payoff at this time. For an idea of scale, a simple rg shows we currently have at least 76 const traits, 773 const impls, and 29 const_derives. That's already a lot of API!
To help mitigate this, it would be nice if we had a more clear policy about what we do and don't want to constify for now.
Proposed Guidelines
- The main driver of what to constify should be needs within rust-lang. That is:
- Constifications needed to test new aspects of const traits or other lang features are completely fine. Examples: const iterators, const closures, sized hierarchy work, patterns, const heap, lang item traits requiring const methods, etc.
- Small constifications that make it easier to write standard library code are fine (caveats below).
- Giving nightly users more const API is not sufficient reason to constify a trait or impl at this time, nor is checking off impls from a list. †
- Standard library code should still generally not be written or rewritten using const traits†. Examples to avoid:
for->while, moreascasts, potentially anythingFIXME(const-hack), anything that relies on a "deep" const trait chain (to avoid the ripple effect of changes). - Avoid adding const traits/impls in
allocorstd. The usefulness of these crates in const contexts is more fundamentally limited than by traits. - When in doubt, link users to this issue and decline the trait-related constifications.†
- These are guidelines; there will be exceptions. Rules for
const traitare more lax than forimpl const.
All aspects marked with † are things that we could consider relaxing once there is an accepted const trait RFC.
I would like to reiterate that the goal here is not to limit experimentation, but rather to give library maintainers a handle on how widely and deeply the standard library is tied to const traits while still under design. We can revisit these guidelines once there is an RFC, and remove them completely once const traits are stable.
Cc @rust-lang/wg-const-eval
Cc @rust-lang/project-const-traits
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 by reading the Context and Proposed Guidelines in this issue, then review the 21-comment discussion and the linked constification example. There are no files or tests named in the payload. Done would require an agreed policy for constifying traits and implementations, rather than a straightforward code change.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Documentation
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100