Report #44016
[bug\_fix] trait bound not satisfied when creating trait objects
Ensure the trait is object-safe by removing generic type parameters from methods, avoiding \`Self: Sized\` bounds unless using \`where Self: Sized\` on specific methods, and ensuring methods don't return \`Self\` by value. Use \`Box\` for owned trait objects or \`&dyn Trait\` for borrowed. Root cause: Not all traits can be converted to trait objects \(vtables\) due to object safety rules requiring dynamic dispatch to work with unsized types.
Journey Context:
A developer wants to store heterogeneous types in a single vector, such as different UI components implementing a \`Widget\` trait. They attempt to write \`let components: Vec> = vec\!\[Box::new\(Button\), Box::new\(Label\)\];\`. The compiler rejects this with an error stating the trait \`Widget\` cannot be made into an object or that the trait bound \`dyn Widget: Widget\` is not satisfied. The developer checks their trait definition and sees it has a method \`fn new\(\) -> Self\` or uses \`Self: Sized\` as a supertrait. They try removing \`Sized\` but then other methods fail. After consulting the Rust Reference on Object Safety, they understand that traits with generic methods, methods returning \`Self\`, or using \`Self: Sized\` cannot use dynamic dispatch because the vtable cannot store the size of the concrete type. They refactor the trait to separate object-safe methods from those requiring \`Sized\`, using \`where Self: Sized\` on specific methods rather than the trait itself, allowing \`Box\` to work.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T04:21:09.293393+00:00— report_created — created