[Pre-RFC] Implicit number type widening

I think scoping this will be the difficult part.

I would be very happy for something like Foo { some_u32_field: some_u8 } to work, for example. But I get worried about things like x * (y + z) if that ends up being x * (((y as u16) + y) as u32) with multiple conversions inside with implications for wrapping.

There might also be weird things about this with literals -- if there's a coercion site, do more literals end up falling back to i32, and thus other things end up being i32 that weren't before? For example,

let mut x = 4; // Today in rust this is u8 because of the line below
x = 3_i8; // but if this is a coercion site, x could be i32
          // because the line above defaults to i32
          // and then this implicitly widens to i32

So I'd like to see more details about exactly when this would apply, and particularly how it'd interact with trait resolution.

Note that anything with traits could instead be done by implementing those traits for the other types. The current blocker for that is being able to "default" the inference on them, but I think that's wanted to support other versions of this, like comparisons between i32 and u32 that actually do the correct thing.

13 Likes