rust-lang / rust-lang/rust

Marker trait sidecasting?

Open
#147,587 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-coercions A-dyn-trait C-discussion T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Is this supposed to be covered by trait upcasting?
Both Foo and Qux are only marker traits, they are always implemented if their dependencies are satisfied, so a Foo can ALWAYS be casted to a Qux.

Is this a limitation of the compiler, something that is in the works, or something that can be worked around in other ways? I'd like to simplify my generic parameters so that they require an implementation of Foo or and not Bar + Baz for sake of readability.

use std::sync::Arc;


trait Bar {}
trait Baz {}

trait Foo: Bar + Baz {
    
}

trait Qux: Bar {
    
}

impl<T> Foo for T where T: Bar + Baz {}
impl<T> Qux for T where T: Bar {}

struct X;

impl Bar for X {}
impl Baz for X {}

fn main() {

    let t_bar: Arc<dyn Bar> = Arc::new(X);
    let t_baz: Arc<dyn Baz> = Arc::new(X);
    let t_foo: Arc<dyn Foo> = Arc::new(X);
    let t_qux: Arc<dyn Qux> = Arc::new(X);
    
    let t_bar_foo: Arc<dyn Bar> = t_foo;
    let t_baz_foo: Arc<dyn Bar> = t_foo;
    let t_bar_qux: Arc<dyn Bar> = t_qux;
    
    let t_qux_foo: Arc<dyn Qux> = t_foo;  // Error, although casting should be always possible
}

I expected to see this happen: compiles

Instead, this happened: compiler cannot prove / create the trait object for Qux from the trait object of Foo.

rustc --version --verbose:

rustc 1.92.0-nightly (b6f0945e4 2025-10-08)
binary: rustc
commit-hash: b6f0945e4681bc4d2faa7c22c5f61dc36abf7dd2
commit-date: 2025-10-08
host: x86_64-unknown-linux-gnu
release: 1.92.0-nightly
LLVM version: 21.1.2

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

Reproduce the supplied minimal Rust program with the stated rustc 1.92.0-nightly version and inspect the failing conversion from Arc to Arc. Compare the behavior with the issue's trait-upcasting expectation; done means establishing whether this is supported, a compiler limitation, or requires a documented workaround.

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
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.