The code works, but I don't know why rustc knows T is i32.
the i32 is passed from dynamic_bar, which has lost the information.
One explain of this is b.b() is called via vtable, but if there is another call like static_foo(&7), it is absolutely called directly, and there will be two versions of static_foo::<i32>.
trait Trait {
fn a(&self)
where
Self: Sized,
{
println!("fuck");
}
fn b(&self) {
println!("fuck");
}
}
fn static_foo<T: Trait + ?Sized>(b: &T) {
b.b()
}
fn dynamic_bar(a: &dyn Trait) {
static_foo(a)
}
fn main() {
impl Trait for i32 {
fn b(&self) where Self: Sized {
println!("fuck2");
}
}
dynamic_bar(&9);
9.a()
}