Repository navigation
Tracking issue for auto traits #13231
Description
This is a tracking issue for RFC 19 / RFC 127.
The feature gate for the issue is #![feature(auto_traits)].
Note
This feature used to be called opt-in built-in traits (gated behind optin_builtin_traits) and
used to include negative negative impls (now gated behind negative_impls; tracking issue: #68318).
About tracking issues
Tracking issues are used to record the overall progress of implementation.
They are also used as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.
Implementation history
Completed Checklist (Historical)
Checklist
Here is a check-list of code to write and tricky scenarios to be sure we handle:
-
forbid conditional negative impls as described here(negative impls are no longer part of this feature)- now tracked in forbid conditional, negative impls #79098 instead
- conditional negative impls not properly enforced Bounds seem to be ignored in negative impls #23072
-
impl !Pod for ..should not be legal Negative blanket OIBIT impl treated as positive #28475 - fix feature-gate for
impl Foo for ..impl Trait for .. {} does not require a feature gate #23225 - defaulted traits ought to be have more restrictive coherence rules defaulted traits ought to be have more restrictive coherence rules #22978
- defaulted traits should have no methods ICE: "resolved vtable bad vtable" with
impl Trait for .. {}#23080 / Check that traits with default impls have no methods #23117 - Add parser support for
impl Foo for .. - Port
Send/Syncto use new infrastructure internally - Add
unsafe impl Send for ../unsafe impl Sync for .. - Object types should never match against a defaulted trait (though the object type itself may add a candidate for traits that appear in the object type)
- When a defaulted trait matches, it should impose any supertrait bounds on the matched type as nested obligations
- Be wary of type inference -- if we haven't resolved a type yet, we have to be ambiguous
- Systematic testing for
constituent_types - Coherence interaction: a defaulted trait can only be implemented for structs/enums
- Coherence interaction: a trait can only be defaulted in the crate where it is defined
- Coherence interaction: a trait can only be defaulted once
- Coherence interaction: an auto-trait can be redundantly implemented for an object that has it - It is possible to redundantly implement an auto trait for a trait-object type #56934
- Defaulted impls cannot be generic
- Fix the interaction with PhantomData. OIBIT should treat PhantomData as if there were an instance of T reachable rather than breaking it down like it would a different struct. OIBIT: for
PhantomData<T>checkTrather than the struct itself #23091 - Allow negative implementations for traits that have a default implementation (besides Send/Sync).
- ICE "wrong number of generic parameters" with abused auto trait #104808 lifetimes on auto traits are buggy
- Coherence rules: is
impl AutoTrait for dyn Traitlegal? Tracking issue for auto traits #13231 (comment) - Interaction with dyn safety. Autotrait bounds on dyn-safe trait methods #107082
- Do
[u8]negative impls affectstr. Tracking issue for auto traits #13231 (comment)
- Add syntax for negative implementations of traits #20285
- Add support for default trait implementations #21689
- Add defaulted impls for Send and Sync #23211
- […] TODO: recover these parts of the impl history
- Rename
optin_builtin_traitstoauto_traits#79336 - […] TODO: recover these parts of the impl history
Unresolved Questions
- Since these are recursive, library authors must somehow coordinate. How do we ensure that library A properly / soundly opts out of library B's auto trait? See Tracking issue for auto traits #13231 (comment).
- Name and syntax?
- ...
Activity
[-]opt-in built-in trait bounds RFC tracker[/-][+]opt-in built-in bounds traits RFC tracker[/+]pnkfelix commentedon Apr 3, 2014
Marking 1.0, P-backcompat-lang.
nikomatsakis commentedon Apr 15, 2014
cc me
flaper87 commentedon Apr 22, 2014
I'm actively working on this. Unfortunately, I've moved quite slowly because of other bugs I had to fix. I hope to be able to submit a PR this week.
257 remaining items
GoldsteinE commentedon Sep 27, 2023
Oops, I mixed up “opting out” and “opting in” in my original message. Either way, the point stands: I don’t see a reason to opt out of arbitrary auto traits unless you’re doing something that can affect this particular system. Author of the auto trait guarantees that it’s safe to impl it if all the fields impl it.
steffahn commentedon Dec 11, 2023
For
Ungil, I’ve found the issuePython::allow_threadsis unsound in the presence ofscoped-tls. PyO3/pyo3#3640which is that an auto-trait is unable to restrict access to certain types even if those types always feature a non-
'staticlifetime as provided by to a higher-kindedfor<'a> FnOnce(SomeType<'a>)-style callback.I’m relatively certain that the
AutoreleaseSafeexample suffers from the same issue.Of course one approach would be to declare
scoped-tlsunsound on the (arguably quite thin) grounds that the standard library only sets precedent that'staticdata is allowed to be shared via thread-local storage. Assuming we don’t declarescoped-tlsunsound, an auto traits feature seems entirely unable to be a solution for sound API in either of the API cases forAutoreleaseSafenorUngil.GoldsteinE commentedon Dec 11, 2023
Oh, that’s really sad. If
scoped-tlsis valid (and I don’t see a reason for it not to be), thenauto trait Ungilcan only make pyo3 less-obviously-unsound instead of sound. IMHO that’s still a valid reason for it to be an auto trait, but it’s really unfortunate that it doesn’t just make it clean and sound.madsmtm commentedon Dec 11, 2023
Hmm, I don't think that's a problem for
objc2'sAutoreleaseSafe, since the only type it's used on isAutoreleasePool<'pool>, whose only purpose is to carry a lifetime (it's a ZST that doesn't actually carry any data), which it seems likescoped-tls-hktmanages to properly bind to the execution of the closure.So even though we can access the pool, we can't actually do anything bad with it, since it's only the lifetime that we care about, and that is still correctly bounded. See the following example for details.
If you manage to produce a similar example that does compile, then I'd love to know!
steffahn commentedon Dec 11, 2023
@madsmtm Here we go:
Auto merge of rust-lang#13231 - Jarcho:no_tree_walk_in_const, r=Alexe…
Zenithsiz commentedon May 6, 2025
Is it possible to make an auto trait not depend on the fields of a type?
I'd like to be able to have an auto trait
auto trait Everything {}, and a typestruct Wrapper<T>(T)such that an auto-trait is always implemented forWrapperregardless ofT: Everything(unless there's an explicitimpl !Everything for Wrapper<...> {}).Further motivation
I have a normal trait with a default implementation depending on an auto trait:
I then have 2 types,
Normalwhich just wants the default impl andSpecialwhich wants to do a special implementation:The issues happen downstream, where a consumer of
Normalcannot callseton a genericT, only onT: WantsAutoSet:(See the playground)
This can be fixed by just using
impl<T> WantsAutoSet for Normal<T> {}, (or by havingNormalnot have a type parameter), but I have a lot of theseNormal-like types and I don't want to need to make this impl for each one, since at that point I'd just make it a normal trait instead of an auto trait.Also these
Normal-like types do need ownership ofTfor theSetimpl, I can't just usePhantomDatato wave it away somehow.(Also note that this is somewhat like specialization, but I can't just use actual specialization because the
Specialimpl ofSethas different bounds that are incompatible (i.e. not a subset), and I'd also like to "disable" the default implementation because it'd inefficient forSpecial, which wouldn't work with specialization.)feat: Support for `auto trait` (tree-sitter#7)
feat: Support for `auto trait` (tree-sitter#7)
[-]Tracking Issue for auto traits (auto_traits) -- formerly called opt-in built-in traits (optin_builtin_traits)[/-][+]Tracking issue for auto traits[/+]lygstate commentedon May 8, 2026
When this can be stablized, #149335 also want this and blocks #154046
I really want io to be mved from std into core.
madsmtm commentedon May 28, 2026
See #13231 (comment), it likely won't be stabilized outside of the standard library.