-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Description
This is a tracking issue for the RFC 3851 rust-lang/rfcs#3851.
The feature gate for the issue is #![feature(supertrait_auto_impl)].
Motivation
We propose supertrait auto impl as one of the solutions to facilitate trait evolution in the Rust ecosystem. auto impl would reduce the effort to rewrite trait implementation by transparently resolving items from the big subtrait to supertraits and by providing a mechanism to supply default implementation of supertraits with clear syntatical notation.
Current state of the language
Rust today would require great effort to re-organise the trait hierarchy, especially concerning the downstream trait implementations. As an illustration, moving a trait associated item to a higher supertrait would require all downstream implementations to split.
Previously, we have the following.
trait BigTrait {
fn foo(..);
fn bar(..);
}
impl BigTrait for MyType {
fn foo(..) {
// implementation
}
fn bar(..) {
// implementation
}
}By splitting the BigTrait, so would one need to split all the existing implementations.
trait BigTrait: Supertrait {
fn foo(..);
}
trait Supertrait {
fn bar(..);
}
impl BigTrait for MyType {
fn foo(..) {
// implementation
}
}
impl Supertrait for MyType {
fn bar(..) {
// implementation
}
}Experiment proposal
We would like to carry out an experiment to introduce supertrait auto impl, so that the existing implementation will continue to "just work."
trait BigTrait: Supertrait {
auto impl Supertrait;
fn foo(..);
}
trait Supertrait {
fn bar(..);
}
impl BigTrait for MyType {
fn foo(..) {
// implementation
}
fn bar(..) {
// implementation for Supertrait::bar
}
}There are more motivating examples in rust-lang/rfcs#3851 which are the use cases we would like to enable when trait refactoring is concerned.
Steps
- Implement the RFC, proposed work
- Parser
- Basic AST implementation auto-impl: parser support #149335
- AST-HIR lowering
- Name resolution phase
- Propagate name resolution, supertrait auto-impl "obligation" across crate boundaries
- Well-formedness check enhancement
- Coherence check
- Well-formedness and predicates
- Trait candidate assembly and confirmation support
- Support in traditional solver
- Support in new solver
-
rustfmtsupport, seeStyle updates -
clippylints -
cargo-semversupport
- Parser
- Adjust documentation (see instructions on rustc-dev-guide)
- Style updates for any new syntax (nightly-style-procedure)
- Style team decision on new formatting
- Formatting for new syntax has been added to the Style Guide
- (non-blocking) Formatting has been implemented in
rustfmt
- Stabilization PR (see instructions on rustc-dev-guide)
Unresolved Questions
TBD