Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local Default Bounds to assist Forget and other ?Trait. #3783

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

Ddystopia
Copy link

@Ddystopia Ddystopia commented Mar 1, 2025

This RFC proposes a mechanism for crates to define default bounds on generics. By specifying these defaults at the crate level we can reduce the need for verbose and repetitive ?Trait annotations while maintaining backward compatibility and enabling future language evolution.

It is targeted at supporting migrations from default being Trait to ?Trait, where Trait represents some assumption that is present everywhere but is not really exercised a lot, such as Forget, size = stride etc. Features like DynSized, as well as extern types, are out of the scope of this RFC, because it does not fit into this category. DynSized is not retracting mostly unexercised assumptions in order to make it ?DynSized the default.

Primary use case is Forget marker trait.

Rendered

Pre-RFC thread

@Noratrieb Noratrieb added T-lang Relevant to the language team, which will review and decide on the RFC. T-types Relevant to the types team, which will review and decide on the RFC. labels Mar 1, 2025
@Aloso
Copy link

Aloso commented Mar 7, 2025

Another drawback is that (similar to #[no_std] support and the MSRV) it is yet another thing for library authors to consider: Do I want to support ?Forget types?

  • If not, I can just ignore it and won't have any problems. But some users might not be able to use my library.
  • If I relax the bound by default, I might get compiler errors which I have to fix, causing churn and slowing down development. And once ?Forget bounds are part of the public API, making them !Forget again is a breaking change.

One situation where it could cause friction is when dependencies do not support ?Forget:

#![default_trait_bounds(?Forget)]

fn frobnicate(x: impl ToString) {
    other_crate::frobnicate(x); // error: `Forget` bound not satisfied
}

That said, I like this proposal and think that the benefits outweigh the drawbacks. Thank you!

[`DynSized`]: https://github.com/rust-lang/rfcs/pull/2984
[`extern types`]: https://github.com/rust-lang/rfcs/pull/1861

The syntax is to be bikeshedded, initially, it might be with a crate-level attributes.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What’s the advantage of starting with this being a crate-level attribute rather than a module-level attribute?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC. T-types Relevant to the types team, which will review and decide on the RFC.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants