Re: [PATCH 1/2] rust: add BitInt integer wrapping type

Miguel Ojeda
2025-11-03T14:54:22Z

On Mon, Nov 3, 2025 at 3:26 PM Yury Norov <yury.norov@gmail.com> wrote:
>
> This is exactly what the patch does:

No, there are no arithmetic conversions going on here in the sense of
C. It defines a particular operation for a set of types.

What you are seeing there is that literals, in Rust, do type
inference, and so the compiler picks a type:

https://doc.rust-lang.org/reference/expressions/literal-expr.html#r-expr.literal.int.infer

Thus if you do:

let v1 = BitInt::<u8, 4>::from_expr(15);
let v2 = BitInt::<u16, 4>::from_expr(15);
let i = 5;
assert_eq!(v1 + i, 20);
assert_eq!(v2 + i, 20);

That will not build, because `i` cannot have two types. But it will if
you comment one of the two asserts.

And if you do:

let v = BitInt::<u16, 4>::from_expr(15);
assert_eq!(v + 5u8, 20);

It will not build either -- there is not even "widening" going on from
`u8` to `u16` in this last example.

Cheers,
Miguel