[Pre-RFC] Implicit number type widening

Unfortunately, the examples that have come up in the thread are making me less confident than I originally was that I'd like widening coercions in Rust.

I think that, overall, the scenarios that have come up make me want to see how well they can be done with more trait implementations before contemplating adding coercions.

Note that the cases where that works aren't the ones that I find interesting, because from works decently. There isn't a "larger" type when comparing i128 to u128, though, so I strongly think additional trait impls are a better way to make comparisons work than coercions would be.

I think this is again better done with additional impls than with coercions, especially looking at the portability point. a[i] always has the possibility of panicking, so if additional impls mean that that ends up really just being a[usize::try_from(i).unwrap()], I'd have no problem with that. (Continuing the "if the right way is longer than as, just do it automatically" idea.)


Can you show an example where it's a bug and it's only used in the comparison? (The bit representation is pretty irrelevant for such a comparison, because LLVM will probably have sign-extended them both into i32 registers anyway.)

Two things on this:

  1. I feel like they usually will fix such an error today by a[i.into()] or a[i as usize] --- not by changing the type of a variable that they clearly wanted to be something badly enough to annotate it --- and thus they have the same problem anyway

  2. I would expect literal comparisons like that to get the same "warning: comparison is useless due to type limits" that you get today with things like i <= 255, so having the mixed mode could actually be better for finding errors, since today the way to "just work stupid compiler" is (i as u16) < 256_u16, which doesn't give warnings.

2 Likes