Post

Okay, Rust Bluesky people, I have a little puzzle for you. Somewhere in the HIR→MIR lowering, it introduces an explicit call to the trait in scope… for the *first* call to `x.abs()` only. (Not a copy vs. move vs. borrow issue!) This playground shows the behavior: play.rust-lang.org?version=nigh...

3 reposts10 likes

Yeaaaaaaah. I sort of understand how and why it is the way it is after reading through the Zulip threads, and it seems like it should be pretty rare that it comes up in practice, but it’s very not good behavior; inference should never have this kind of surprising result.

It’s related—somehow—to type inference. If you explicitly type it as `let x: i64 = 42;` the behavior goes away. It *only* inserts the `<i64 as Trait>::abs()` cast if you leave it for the compiler to infer the type. Notably, it also adds a single reference indirection to the original in that case. 🤔