Trait impls on `dyn` objects result in combinatorically increasing autotrait bounds

Open
#157,506 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

No implementation files, tests, or compiler entry points are identified in the issue. Start by locating Rust's trait-object and auto-trait handling, then determine whether the proposed behavior belongs in the language, trait solver, or compiler. Done should include an agreed design, implementation scope, and tests covering the relevant dyn and auto-trait combinations.

Written by the indexing model from the issue text.

Description

A-auto-traits A-dyn-trait C-feature-request T-lang T-types

Oftentimes, a trait is dyn-incompatible but can still be implemented on a dyn object by selecting conservative default values for missing items. A common pattern resembles:

trait Foo {
    // "i promise this impl does at least this many of $something"
    const MIN_SOMETHING: u32;
    fn okay(&self);
}
impl(self) trait Bar {    // or sealed
    fn dyn_okay(&self)
}
impl<T: Foo> Bar for Foo {
    fn dyn_okay(&self) { self.okay() }
}
impl Foo for dyn Bar {
    const MIN_SOMETHING: u32 = 0;
    fn okay(&self) { self.dyn_okay() }
}

However, this pattern is problematic since the impl on dyn Bar does not automatically also apply to dyn Bar + Send, dyn Bar + Sync, or dyn Bar + Send + Sync. As more autotraits are added to the language, this problem can get combinatorically worse as the number of possible autotrait combinations increases.

A couple of potential solutions come to mind; on the one hand, if there were a builtin trait IsDynObject, the latter impl in the example could be rephrased to impl<T: Bar + IsDynObject> Foo for T.

Alternatively, having syntax for impl Foo for dyn Bar + ... or similar and autogenerating the relevant impls may also work but removes the option of specifying whether certain autotraits should be excluded. I'm not sure if this has concrete usecases, though.

Dominant language
Rust
Stars
119k
Forks
16.2k
Avg merge
2d 11h
Merged PRs (30d)
510

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.

More from rust-lang/rust

All issues in rust-lang/rust

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.