Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Rust rejects dyn Trait with E0038, the trait cannot be made into an object

Your error

error[E0038]: the trait `Trait` is not dyn compatible3 more lines it can show

Fix it fast

Most common cause, 1 of 4

A method has generic type parameters

If trait objects never need to call that method, add where Self: Sized to it so it is left out of the vtable.

Fix
trait Renderer {
    fn draw(&self);
    fn draw_with<T: std::fmt::Display>(&self, label: T) where Self: Sized;
    // or, callable on dyn Renderer:
    fn draw_labeled(&self, label: &dyn std::fmt::Display);
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

error[E0038] means the trait cannot become dyn Trait, since a trait object needs a vtable. Rust 1.100 says not dyn compatible; older compilers said cannot be made into an object. The because note names why: a generic method, Self in a signature, a Sized supertrait such as Clone, or an async fn. Add where Self: Sized to that method, or change it.

  • Applies to Rust 1.100.0 (stable), Rust 1.80 (old wording)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Print the full E0038 block. The line that starts with ...because names the method and the rule it breaks: generic type parameters (cause 1), Self or no self parameter (cause 2), requires Self: Sized (cause 3), or is async (cause 4).

Run this first
cargo build 2>&1 | grep -A14 "E0038" | grep "because"

Then find the line you see in this list and open its fix:

  1. 1A method has generic type parameters...because method `bar` has generic type parameters
  2. 2A method uses Self in its signature or has no self parameter...because method `foo` references the `Self` type in its return type, or ...because associated function `new` has no `self` parameter
  3. 3The trait requires Self: Sized, often through Clone...because it requires `Self: Sized`, and the trait is declared like trait Shape: Clone or trait Shape: Sized
  4. 4The trait has an async fn or returns impl Trait...because method `foo` is `async`, or the method is declared fn foo(&self) -> impl Trait

What does the error look like?

  • error[E0038]: the trait `Trait` is not dyn compatible
  • note: for a trait to be dyn compatible it needs to allow building a vtable
  • error[E0038]: the trait `Trait` cannot be made into an object
  • ...because method `foo` references the `Self` type in its return type

Cause 1 of 4

A method has generic type parameters

You see...because method `bar` has generic type parameters

Check

E0038 with a generic-method note means a method such as fn bar<T>(&self, x: T) blocks dyn Trait. A vtable holds one entry per method, and a generic method would need a separate copy for every T it might be called with, so Rust cannot build that table.

Check
grep -nE "fn [a-z_]+<" src/shapes.rs

Fix

If trait objects never need to call that method, add where Self: Sized to it so it is left out of the vtable. If they do, replace the type parameter with a trait object such as &dyn OtherTrait.

Fix
trait Renderer {
    fn draw(&self);
    fn draw_with<T: std::fmt::Display>(&self, label: T) where Self: Sized;
    // or, callable on dyn Renderer:
    fn draw_labeled(&self, label: &dyn std::fmt::Display);
}

Verify

The crate builds and a Box<dyn Renderer> can call draw and draw_labeled.

Verify
cargo build

Confirmed by doc.rust-lang.org

Did this fix it?

Cause 2 of 4

A method uses Self in its signature or has no self parameter

You see...because method `foo` references the `Self` type in its return type, or ...because associated function `new` has no `self` parameter

Check

A method that returns Self (or takes another Self argument) leaves the compiler unable to know the concrete type behind dyn Trait, and a function without a self receiver, such as a constructor, cannot be reached through a vtable at all. Either one makes the trait fail the dyn-compatibility rules.

Check
grep -nE -e "-> Self|fn new\(" src/shapes.rs

Fix

Add where Self: Sized to those functions. They stay available on concrete types, and the rest of the trait becomes usable as dyn Trait.

Fix
trait Shape {
    fn area(&self) -> f64;
    fn scaled(&self, k: f64) -> Self where Self: Sized;
    fn unit() -> Self where Self: Sized;
}

fn total(shapes: &[Box<dyn Shape>]) -> f64 {
    shapes.iter().map(|s| s.area()).sum()
}

Verify

total compiles, and calling scaled or unit on a concrete type still works.

Verify
cargo test

Confirmed by doc.rust-lang.org

Did this fix it?

Cause 3 of 4

The trait requires Self: Sized, often through Clone

You see...because it requires `Self: Sized`, and the trait is declared like trait Shape: Clone or trait Shape: Sized

Check

A trait whose supertraits include Sized cannot be dyn compatible, because every dyn Trait is unsized. Clone is declared as pub trait Clone: Sized in the standard library, so trait Shape: Clone inherits the same restriction.

Check
grep -nE "trait [A-Za-z]+ *: *.*(Clone|Sized)" src/

Fix

Drop Clone (or Sized) from the supertrait list. If trait objects need to be copied, add a method that returns a boxed copy, which takes &self and returns Box<dyn Shape>, so it follows the receiver rules.

Fix
trait Shape {
    fn area(&self) -> f64;
    fn clone_box(&self) -> Box<dyn Shape>;
}

#[derive(Clone)]
struct Circle(f64);

impl Shape for Circle {
    fn area(&self) -> f64 { std::f64::consts::PI * self.0 * self.0 }
    fn clone_box(&self) -> Box<dyn Shape> { Box::new(self.clone()) }
}

Verify

Box<dyn Shape> builds, and clone_box returns a second, independent shape.

Verify
cargo build

Confirmed by doc.rust-lang.org

Did this fix it?

Cause 4 of 4

The trait has an async fn or returns impl Trait

You see...because method `foo` is `async`, or the method is declared fn foo(&self) -> impl Trait

Check

The Rust Reference lists an async fn, with its hidden Future type, and a return-position impl Trait as opaque return types that a dispatchable method must not have. A trait with either one cannot be turned into dyn Trait as written.

Check
grep -nE "async fn|-> impl " src/store.rs

Fix

Mark the async method where Self: Sized when only concrete types call it. When trait objects must call it, return a boxed future instead, which is a normal type rather than an opaque one.

Fix
use std::future::Future;
use std::pin::Pin;

trait Store {
    fn get<'a>(&'a self, key: &'a str)
        -> Pin<Box<dyn Future<Output = Option<String>> + Send + 'a>>;
}

Verify

Arc<dyn Store> compiles, and awaiting store.get("k") returns the value.

Verify
cargo build

Confirmed by doc.rust-lang.org

Did this fix it?

How do I stop it coming back?

  • Decide early whether a trait is meant for dyn use, and add a test that builds Box<dyn YourTrait> so a new generic or async method fails CI immediately.
  • Put constructors and Self-returning helpers behind where Self: Sized from the start.
  • Keep a second, generic-free trait for the dynamic API if the main trait needs generic methods.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.