Skip to content

Tracking Issue for Supertrait auto impl #149556

@dingxiangfei2009

Description

@dingxiangfei2009

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
    • 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
    • rustfmt support, see Style updates
    • clippy lints
    • cargo-semver support
  • 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

Implementation history

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCI-lang-nominatedNominated for discussion during a lang team meeting.I-lang-radarItems that are on lang's radar and will need eventual work or consideration.P-lang-drag-1Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-langT-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions