A Rust `Plugin` trait with a generic `configure<T>` method and a `duplicate(&self) -> Self` method fails with E0038 at `Box<dyn Plugin>`: why, and how do you fix it?
answer
- formerly called object safety
- one vtable slot per method
- Self has no known size
- where Self: Sized opts out
basics
~20 sA vtable needs one function pointer per method: a generic method has unboundedly many instantiations and a Self return has no size behind dyn, so Plugin is not dyn compatible. Adding where Self: Sized to those two methods removes them from the vtable.
solid answer
~40 sA trait object calls methods through a vtable with one function pointer per method. `configure<T>` is not one function but a family, one per `T`, so it has no single slot; `duplicate` returns `Self`, whose size is unknown behind `dyn Plugin`. rustc 1.99 reports E0038 "the trait `Plugin` is not dyn compatible" and names each method. Adding `where Self: Sized` to `configure` and `duplicate` marks them **explicitly non-dispatchable**: they leave the vtable, the trait becomes dyn compatible, and they stay callable on concrete types, while calling them on a `Box<dyn Plugin>` is rejected with "the `configure` method cannot be invoked on a trait object". Where the trait object needs the behaviour, add a dispatchable form, such as `fn clone_box(&self) -> Box<dyn Plugin>`.
code
rust · 16 linestrait Plugin {
fn name(&self) -> &str;
fn configure<T: std::fmt::Debug>(&mut self, option: T);
fn duplicate(&self) -> Self;
}
struct Echo;
impl Plugin for Echo {
fn name(&self) -> &str { "echo" }
fn configure<T: std::fmt::Debug>(&mut self, option: T) { println!("{option:?}"); }
fn duplicate(&self) -> Self { Echo }
}
fn load() -> Box<dyn Plugin> { Box::new(Echo) }
fn main() { println!("{}", load().name()); }go deeper
Know that not every trait can be used as dyn Trait, that the error is E0038, and that generic methods and Self returns are the usual cause.
Explain why each rule follows from the vtable: one slot per method, no size for Self, no receiver for constructors. Recite where Self: Sized as the escape.
Read the diagnostic, choose between where Self: Sized, a dispatchable clone_box or &dyn argument, and splitting the trait, and know which calls on the trait object stop compiling.
For a public trait, decide early whether dyn use is part of its contract, since adding a generic method or associated const later breaks every user who writes dyn Plugin.
## What dyn compatibility protects A `Box<dyn Plugin>` dispatches through a **vtable**: a static table with one function pointer per method of the trait. A trait can only become a trait object if such a table can be built, and the compiler checks this whenever `dyn Plugin` is written. The property is called **dyn compatibility** (the Reference notes it was formerly known as *object safety*). Violating it is **E0038**: ```plaintext error[E0038]: the trait `Plugin` is not dyn compatible 3 | fn configure<T: std::fmt::Debug>(&mut self, option: T); | ^^^^^^^^^ ...because method `configure` has generic type parameters 4 | fn duplicate(&self) -> Self; | ^^^^ ...because method `duplicate` references the `Self` type in its return type ``` ## Why each method breaks it - **`configure<T>`**: a generic method is monomorphized, one copy per `T` a caller uses. The vtable is built when a concrete plugin is coerced to `dyn Plugin`, long before anyone decides which `T` to pass, so there is no single function to point at. Lifetime parameters are fine; type parameters are not. - **`duplicate(&self) -> Self`**: the caller of a method on `dyn Plugin` does not know the concrete type, so it cannot reserve space for a returned `Self` or even name its type. The same applies to `Self` in a parameter, such as `fn merge(&self, other: Self)`, where `other` might be a different concrete type from the receiver. The Reference's rules, in short and checked against rustc 1.99.1: a dyn-compatible trait 1. has only dyn-compatible supertraits, and does not have `Sized` as a supertrait (so `trait Plugin: Clone` fails: `Clone` requires `Sized`); 2. has no associated constants (`const VERSION: u32;` fails); 3. has no generic associated types; 4. makes every associated function either dispatchable or explicitly non-dispatchable. A **dispatchable** method has no type parameters, uses `Self` only in the receiver, takes `&self`, `&mut self`, `self: Box<Self>`, `Rc<Self>`, `Arc<Self>` or `Pin<P>` of one of those, and has no opaque return type (no `-> impl Trait`, no `async fn`). A function with no receiver at all, such as `fn create() -> Self`, is not dispatchable either. ## The fix: where Self: Sized An **explicitly non-dispatchable** method carries a `where Self: Sized` bound. `dyn Plugin` is never `Sized`, so the bound means "this method does not exist on the trait object", and the compiler leaves it out of the vtable: ```rust trait Plugin { fn name(&self) -> &str; fn configure<T: Debug>(&mut self, option: T) where Self: Sized; fn duplicate(&self) -> Self where Self: Sized; } ``` Now `Box<dyn Plugin>` compiles. On a concrete `Echo`, `configure(42)` and `duplicate()` work as before. On the trait object, `p.configure(1)` is rejected with "the `configure` method cannot be invoked on a trait object", an error rustc prints without an E-code. A method taking `self` by value implies `Self: Sized` the same way. ## Why a Sized supertrait is fatal The E0038 index page explains the least obvious rule. Rust defines that every trait object type `dyn Plugin` automatically implements `Plugin`, so generic code written as `fn run<T: Plugin + ?Sized>(p: &T)` accepts a `&dyn Plugin` as readily as a concrete plugin. A trait object is never `Sized`, so it could never satisfy `trait Plugin: Sized`, or `trait Plugin: Clone`, since `Clone` requires `Sized`. Rather than allow a `dyn Plugin` that does not implement `Plugin`, the compiler forbids that trait object altogether, and rustc 1.99 says so: "...because it requires `Self: Sized`". ## Other repairs | Situation | Repair | |---|---| | The trait object genuinely needs the operation | Add a dispatchable form: `fn clone_box(&self) -> Box<dyn Plugin>` instead of `Clone`, or `fn configure_debug(&mut self, option: &dyn Debug)` instead of a generic | | Constructors and factories | Keep `fn create() -> Self where Self: Sized`, or move them to a separate factory trait | | An associated constant | Turn it into a method, `fn version(&self) -> u32`, or move it to another trait (rustc's own help suggests moving it) | | `Clone` or another `Sized` supertrait | Drop the supertrait and add `clone_box` | rustc 1.99's help text for this error says "consider moving `configure` to another trait"; splitting the trait is a valid design, but `where Self: Sized` keeps one trait when the generic convenience method still belongs on it. ## Edges that belong elsewhere `-> impl Trait` and `async fn` in a trait also make it dyn-incompatible, because their hidden return types have no fixed size; the details live with opaque impl types and with async trait methods. The rule itself is about Rust's vtables, not about interfaces in general.
- Why does `trait Plugin: Clone` make `dyn Plugin` impossible?`Clone` has `Sized` as a supertrait, so `Plugin: Clone` requires `Self: Sized`. Every `dyn Plugin` must implement `Plugin`, yet a trait object is never `Sized`, so rustc refuses the trait object with E0038 "...because it requires `Self: Sized`". The usual repair is a dispatchable `fn clone_box(&self) -> Box<dyn Plugin>`, implemented in each plugin with `Box::new(self.clone())`.
- What happens if you call a `where Self: Sized` method on a `Box<dyn Plugin>`?It does not compile. The method has no vtable slot, so rustc reports "the `configure` method cannot be invoked on a trait object" and points at the `Sized` requirement, without an E-code. Calling it on a concrete plugin type, or inside generic code over `T: Plugin`, still works.
- Says any trait can become a trait object as long as you box it.
- Thinks a where Self: Sized bound on one method makes the whole trait unusable as dyn.
- Believes lifetime parameters on a method also break dyn compatibility.
- Thinks a where Self: Sized method can still be called on the trait object.
- Adds Clone as a supertrait to make Box<dyn Plugin> cloneable.
- Calls the rule a borrow-checker or lifetime error rather than a vtable limit.