-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Open
Labels
A-coercionsArea: implicit and explicit `expr as Type` coercionsArea: implicit and explicit `expr as Type` coercionsA-dyn-traitArea: trait objects, vtable layoutArea: trait objects, vtable layoutC-discussionCategory: Discussion or questions that doesn't represent real issues.Category: Discussion or questions that doesn't represent real issues.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.
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
Metadata
Metadata
Assignees
Labels
A-coercionsArea: implicit and explicit `expr as Type` coercionsArea: implicit and explicit `expr as Type` coercionsA-dyn-traitArea: trait objects, vtable layoutArea: trait objects, vtable layoutC-discussionCategory: Discussion or questions that doesn't represent real issues.Category: Discussion or questions that doesn't represent real issues.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.