rust-lang / rust-lang/rust

Tracking Issue for Supertrait `auto impl`

Open
#149,556 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

C-tracking-issue I-lang-radar T-lang T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

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 #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
    • 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with RFC 3851 and the feature gate #![feature(supertrait_auto_impl)], then review the tracking checklist. The initial parser work is tracked in #149335; remaining areas include AST-HIR lowering, name resolution, well-formedness, trait solving, rustfmt, clippy, cargo-semver, documentation, and stabilization. Done requires completing the relevant implementation and follow-up steps across those areas.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.