Repository navigation
Open
Labels
A-specializationArea: Trait impl specializationArea: Trait impl specializationA-trait-systemArea: Trait systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.Call for participation: Medium difficulty. Experience needed to fix: Intermediate.F-specialization`#![feature(specialization)]``#![feature(specialization)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Update 2026-08-06
Some tests were added in #160520, but this issue needs review for completeness.
rust-lang/rfcs#1210 included support for default impl, which declared a set of defaults that other impls can inherit (if they correspond to a subset of the default impl's types). This was intended as a replacement/improvement of the existing default methods that appear in trait bodies (it was also originally called partial impl). Unfortunately, this feature is not yet implemented!
Some things to be sure of:
- a
default implneed not include all items from the trait - a
default implalone does not mean that a type implements the trait - all items in a
default implare (implicitly)defaultand hence specializable
cc #31844
Reacted by Restioson, Denis Kotlyarov, KC, Christian Vallentin, liumuqing, MOZGIII, Christos Papadopoulos, sighoya, Paolo Barbolini, Felipe S. S. Schneider and 10 more
Reacted by Zverev Konstantin, Michael Crapse, sighoya, Felipe S. S. Schneider, SeniorMars and musjj
Reacted by sighoya, Felipe S. S. Schneider, SeniorMars, musjj and jasper310899
Metadata
Metadata
Assignees
Labels
A-specializationArea: Trait impl specializationArea: Trait impl specializationA-trait-systemArea: Trait systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.Call for participation: Medium difficulty. Experience needed to fix: Intermediate.F-specialization`#![feature(specialization)]``#![feature(specialization)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
nikomatsakis commentedon Nov 8, 2016
cc @giannicic @aturon
Auto merge of #37860 - giannicic:defaultimpl, r=nagisa
giannicic commentedon Apr 27, 2017
@nikomatsakis
the PR addressing the 3rd task list item has been merged.
Now i'm working on the first two points.
nikomatsakis commentedon Apr 28, 2017
@giannicic awesome!
Phlosioneer commentedon Aug 1, 2017
Any progress on this?
giannicic commentedon Aug 2, 2017
@Phlosioneer
Yes, the first point is done. Currently I'm working on the second one.
I've looked at the trait resolution and identified where to place the
default impltrait implementation check, now I'm figuring out how to properly implement this check because i need to differentiate it based on the cause of the obligation.For example, if i have a
default implthat doesn't implement all the methods of the traitFoo, like the followinga call like
MyStruct.foo_one()should pass the trait resolution because of the first point.Instead if i call
foo(MyStruct)with foo likefn foo<T: Foo>(x: T) -> &'static strthe trait resolution should fail because of the second point.It could pass if the
default implimplements all the methods of theTraitpetrochenkov commentedon Dec 2, 2017
Can inherent impls be default?
This currently compiles successfully, but the compiler has zero tests for it, so it's probably unintended.
The general idea is reasonable though, e.g.
petrochenkov commentedon Dec 2, 2017
Can auto trait impls (including negative ones) be default?
This is currently accepted:
petrochenkov commentedon Dec 3, 2017
#46455 conservatively makes both #37653 (comment) and #37653 (comment) errors.
Restioson commentedon Dec 3, 2017
Really excited for full support of specialization (esp. point 2)! +1
5 remaining items
nikomatsakis commentedon Feb 27, 2018
I am torn. On the one hand, I liked the "economy" of using
default. I feel like havingpartial implis its own sort of complexity. On the other hand, I think there is a kind of assumption that adefault impl { .. }ought to be equivalent toimpl { default ... }. I'm not sure though if that means that this is a natural rule that we ought to respect, or a matter of needing more education. Perhaps -- even -- it is thedefaultkeyword on functions and items that should be changed (e.g., tospecializableor something).Restioson commentedon Feb 27, 2018
Personally, I was slightly confused as to why
partial implwas scrapped in favour ofdefault impl-default impldoesn't really sound like it can be partial...RalfJung commentedon Apr 19, 2020
I think this is a serious limitation, because it means there is no way to have a partial impl where some items are not specializable. I do not see that limitation discussed in the RFC, nor a motivation for why it might be required. It's a big RFC though, so maybe I missed it?
Here is a short discussion with a use-case for a partial unspecializable impl. Basically the idea is to implement some associated types of the trait, and then provide some default implementations that require this particular choice of associated type. I think the current approach makes that kind of reuse fundamentally impossible.
nikomatsakis commentedon Apr 20, 2020
@RalfJung it was a deliberate simplification, but one that I'm still not 100% sure was the right call. The initial version of the RFC had
partial implanddefault fnas two distinct concepts. They were merged because it seemed pretty clear that it was going to be confusing somehow, particularly if we hadpartial default impl, but that did imply giving up on the ability to have some things fixed and some things not. You're right that the use case you cited then becomes impossible.RalfJung commentedon Apr 21, 2020
@nikomatsakis I see. IMO that limitation should be lifted before stabilization, it's rather surprising and frustrating that what seems like a purely syntactic choice makes some interesting specialization use-cases fundamentally impossible. Having
default implhave both of these effects (enable partial impl and make every item overwritable) violates orthogonality and compositionality, values that otherwise Rust considers very important.Could we get this listed as a concern to revisit in some tracking issue, maybe here?
nikomatsakis commentedon Apr 22, 2020
@RalfJung I'd be willing to revisit this prior to stabilization, yes.
nikomatsakis commentedon Apr 22, 2020
Actually, @RalfJung, it's already listed:
default impl(where all items aredefault) orpartial impl(wheredefaultis opt-in)RalfJung commentedon Apr 22, 2020
Oh, I was checking the items in this issue but not the main one. oops
Thanks for adding a pointer to this sub-discussion here. :)
mankinskin commentedon Dec 21, 2020
Will it be possible to define different default impls for differently trait bounded type parameters? e.g.
In case of
A: ToRouteandB: ToRoute + Debug,Ashould only match types not implementingDebug, i.e. the more specific trait bound has precedence.joergbrech commentedon Dec 16, 2021
I posted a suggestion on
default implin #31844 (comment) and just realized it would have been more appropriate here. I don't know if my suggestion makes sense at all and if it isn't too late, since this issue is already well on its way. But I would love to hear your thoughts.jplatte commentedon Dec 16, 2021
@joergbrech I think you would be interested in the future possibilities section of RFC 2532 (associated type defaults).
joergbrech commentedon Dec 16, 2021
@jplatte, thanks for the link. Yes default groups would be much more flexible!
Drop the GeometricProduct trait.