Blog: A Shifty Riddle

Wherein we uncover a conundrum with how bit shifts are implemented in Rust.

2 Likes

Wow! That is some fancy type-dueling right there!

I had to read it a couple of times to figure it out, even though you line it up quite nicely. Dark wizardry indeed!

Do I understand correctly that the compiler applies two phases of type-inference to the <<? First the method, which defaults to i32 therefore *3 also i32, then it finds the implementation, so u32, but doesn't "update" the inference for *3?
That seems like it should require a compiler-side fix?

In the compiler source you link, I found this comment:

// Supply type inference hints if relevant. Probably these
// hints should be enforced during select as part of the
// `consider_unification_despite_ambiguity` routine, but this
// more convenient for now.

Especially the "more convenient for now" seems to imply there is room for improvement here. Is there any issue filed for this?