I am struggling with the basics of object safety. If I have this code
struct S {
x: i32
}
trait Trait: Sized {
fn f(&self) -> i32 where Self:
Making Trait a supertype of Sized doesn't help - in fact it is not permitted, as the error message says. Each implementation of Trait will still have a different size, so your function object_safety_dynamic cannot be compiled. Monomorphization cannot be used here because there is no generic parameter, so the compiled function must work for all implementations of Trait.
However, references do have a fixed size, so making the argument into a reference will work:
trait Trait {
fn f(&self) -> i32;
}
fn object_safety_dynamic(x: &Trait) {}
A trait object is always a reference of some kind, e.g. a Box or &T. This is precisely because the size of implementations of the trait will be different, while a reference type has a known, fixed size.