Skip to content

RFC: Associated traits#3938

Open
sandersaares wants to merge 9 commits intorust-lang:masterfrom
sandersaares:associated-traits-rfc
Open

RFC: Associated traits#3938
sandersaares wants to merge 9 commits intorust-lang:masterfrom
sandersaares:associated-traits-rfc

Conversation

@sandersaares
Copy link
Copy Markdown

@sandersaares sandersaares commented Mar 28, 2026

Allow traits to declare associated traits — named trait constraints that are defined abstractly in a trait and given concrete values in implementations. Just as associated types let a trait abstract over which type is used, associated traits let a trait abstract over which constraints are imposed. This is the trait-level analog of associated types.

#![feature(associated_traits)]

// 1. Declare an associated trait in a trait definition.
trait Container {
    trait Elem;
    fn process<T: Self::Elem>(&self, item: T);
}

// 2. Each impl chooses the concrete constraints.
impl Container for MyContainer {
    trait Elem = Send + Clone;
    fn process<T: Self::Elem>(&self, item: T) { /* ... */ }
}

// 3. Generic code outside the impl uses `C::Elem` as a bound.
//    This is the primary use case: the caller is generic over
//    the constraints without knowing what they are.
fn process_item<C: Container, T: C::Elem>(container: &C, item: T) {
    container.process(item);
}

// 4. Fully-qualified syntax also works.
fn process_qualified<C: Container, T: <C as Container>::Elem>(container: &C, item: T) {
    container.process(item);
}

Previous discussion in #2190.

Prototype implementation: https://github.com/sandersaares/rust/tree/associated-traits

Rendered

Important

When responding to RFCs, try to use inline review comments (it is possible to leave an inline review comment for the entire file at the top) instead of direct comments for normal comments and keep normal comments for procedural matters like starting FCPs.

This keeps the discussion more organized.

sandersaares and others added 7 commits March 21, 2026 13:25
Proposes associated traits — named trait constraints declared
abstractly in a trait and given concrete values in implementations.
This is the trait-level analog of associated types and the Rust
equivalent of Haskell's ConstraintKinds.

Based on rust-lang#2190 (76+ upvotes, 2017-present) and a
working prototype implementation on the rust-lang/rust associated-traits
branch with 57 tests and full UI suite passing.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
dyn T::Elem is not supported because Rust type-checks generic bodies
before monomorphization, so the vtable layout (which depends on T)
is not yet known. Remove dyn binding syntax from future possibilities.
Bound expansion in method bodies and impl Self::AssocTrait in method
arguments are both implemented. Remove from unresolved questions.
Move from unresolved questions and future possibilities to
implemented features. Add guide-level documentation.
@fmease fmease added T-types Relevant to the types team, which will review and decide on the RFC. T-lang Relevant to the language team, which will review and decide on the RFC. labels Mar 28, 2026
Replace implementation details (AST variants, HIR representations,
DefKind, solver internals) with language-level reference material:
- Associated trait declarations (grammar, declaration bounds, defaults)
- Associated trait implementations (values, validation, restrictions)
- Usage as bounds (shorthand, UFCS, where clauses, impl Trait)
- Positions where rejected (type, dyn, inherent impl)
- Generic associated traits
- Interaction with associated types, GATs, trait inheritance, auto-traits
- Cross-crate usage
- Comparison table

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-lang Relevant to the language team, which will review and decide on the RFC. T-types Relevant to the types team, which will review and decide on the RFC.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants