Rollup merge of #151235 - type-info-rename-bits, r=oli-obk

Change field `bit_width: usize` to `bits: u32` in type info

Follow-up https://github.com/rust-lang/rust/pull/151123#discussion_r2698418929. Quotes:

@Skgland:
> > I'm not sure whether we should use `usize` or `u64` here to represent the bit width.
>
> My expectation would be `u32` matching the associated `{u,i}N::BITS`[^1][^2][^3] constant that already exists on the integer types.
>
> [^1]: https://doc.rust-lang.org/std/primitive.i8.html#associatedconstant.BITS
> [^2]: https://doc.rust-lang.org/std/primitive.i128.html#associatedconstant.BITS
> [^3]: https://doc.rust-lang.org/std/primitive.usize.html#associatedconstant.BITS

@SpriteOvO:
> I found some [previous discussions](https://github.com/rust-lang/rust/pull/76492#issuecomment-700516940) regarding the type of `::BITS` constant. And during the stabilization of `::BITS`, the choice of `u32` affected some ecosystem crates (#81654), but soon after, these crates all accepted the `u32` type.
>
> So I think it makes sense to keep the type consistent with `::BITS` here. Then I'd also like to change the name from `bit_width` to `bits`, also for consistency.

r? @oli-obk
This commit is contained in:
Jacob Pratt 2026-01-18 03:16:46 -05:00 committed by GitHub
commit 80db7158af
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 34 additions and 34 deletions

View file

@ -257,8 +257,8 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
{
let field_place = self.project_field(&place, field_idx)?;
match field.name {
sym::bit_width => self.write_scalar(
ScalarInt::try_from_target_usize(bit_width, self.tcx.tcx).unwrap(),
sym::bits => self.write_scalar(
Scalar::from_u32(bit_width.try_into().expect("bit_width overflowed")),
&field_place,
)?,
sym::signed => self.write_scalar(Scalar::from_bool(signed), &field_place)?,
@ -278,8 +278,8 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
{
let field_place = self.project_field(&place, field_idx)?;
match field.name {
sym::bit_width => self.write_scalar(
ScalarInt::try_from_target_usize(bit_width, self.tcx.tcx).unwrap(),
sym::bits => self.write_scalar(
Scalar::from_u32(bit_width.try_into().expect("bit_width overflowed")),
&field_place,
)?,
other => span_bug!(self.tcx.def_span(field.did), "unimplemented field {other}"),

View file

@ -590,12 +590,12 @@ symbols! {
binaryheap_iter,
bind_by_move_pattern_guards,
bindings_after_at,
bit_width,
bitand,
bitand_assign,
bitor,
bitor_assign,
bitreverse,
bits,
bitxor,
bitxor_assign,
black_box,

View file

@ -114,7 +114,7 @@ pub struct Char {
#[unstable(feature = "type_info", issue = "146922")]
pub struct Int {
/// The bit width of the signed integer type.
pub bit_width: usize,
pub bits: u32,
/// Whether the integer type is signed.
pub signed: bool,
}
@ -125,7 +125,7 @@ pub struct Int {
#[unstable(feature = "type_info", issue = "146922")]
pub struct Float {
/// The bit width of the floating-point type.
pub bit_width: usize,
pub bits: u32,
}
/// Compile-time type information about string slice types.

View file

@ -47,8 +47,8 @@ fn test_tuples() {
match (a.ty.info().kind, b.ty.info().kind) {
(TypeKind::Int(a), TypeKind::Int(b)) => {
assert!(a.bit_width == 8 && a.signed);
assert!(b.bit_width == 8 && !b.signed);
assert!(a.bits == 8 && a.signed);
assert!(b.bits == 8 && !b.signed);
}
_ => unreachable!(),
}
@ -70,27 +70,27 @@ fn test_primitives() {
let Type { kind: Int(ty), size, .. } = (const { Type::of::<i32>() }) else { panic!() };
assert_eq!(size, Some(4));
assert_eq!(ty.bit_width, 32);
assert_eq!(ty.bits, 32);
assert!(ty.signed);
let Type { kind: Int(ty), size, .. } = (const { Type::of::<isize>() }) else { panic!() };
assert_eq!(size, Some(size_of::<isize>()));
assert_eq!(ty.bit_width, size_of::<isize>() * 8);
assert_eq!(ty.bits as usize, size_of::<isize>() * 8);
assert!(ty.signed);
let Type { kind: Int(ty), size, .. } = (const { Type::of::<u32>() }) else { panic!() };
assert_eq!(size, Some(4));
assert_eq!(ty.bit_width, 32);
assert_eq!(ty.bits, 32);
assert!(!ty.signed);
let Type { kind: Int(ty), size, .. } = (const { Type::of::<usize>() }) else { panic!() };
assert_eq!(size, Some(size_of::<usize>()));
assert_eq!(ty.bit_width, size_of::<usize>() * 8);
assert_eq!(ty.bits as usize, size_of::<usize>() * 8);
assert!(!ty.signed);
let Type { kind: Float(ty), size, .. } = (const { Type::of::<f32>() }) else { panic!() };
assert_eq!(size, Some(4));
assert_eq!(ty.bit_width, 32);
assert_eq!(ty.bits, 32);
let Type { kind: Str(_ty), size, .. } = (const { Type::of::<str>() }) else { panic!() };
assert_eq!(size, None);

View file

@ -35,7 +35,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 8,
bits: 8,
signed: true,
},
),
@ -46,7 +46,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 32,
bits: 32,
signed: true,
},
),
@ -57,7 +57,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 64,
bits: 64,
signed: true,
},
),
@ -68,7 +68,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 128,
bits: 128,
signed: true,
},
),
@ -79,7 +79,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 32,
bits: 32,
signed: true,
},
),
@ -90,7 +90,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 8,
bits: 8,
signed: false,
},
),
@ -101,7 +101,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 32,
bits: 32,
signed: false,
},
),
@ -112,7 +112,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 64,
bits: 64,
signed: false,
},
),
@ -123,7 +123,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 128,
bits: 128,
signed: false,
},
),
@ -134,7 +134,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 32,
bits: 32,
signed: false,
},
),

View file

@ -35,7 +35,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 8,
bits: 8,
signed: true,
},
),
@ -46,7 +46,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 32,
bits: 32,
signed: true,
},
),
@ -57,7 +57,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 64,
bits: 64,
signed: true,
},
),
@ -68,7 +68,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 128,
bits: 128,
signed: true,
},
),
@ -79,7 +79,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 64,
bits: 64,
signed: true,
},
),
@ -90,7 +90,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 8,
bits: 8,
signed: false,
},
),
@ -101,7 +101,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 32,
bits: 32,
signed: false,
},
),
@ -112,7 +112,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 64,
bits: 64,
signed: false,
},
),
@ -123,7 +123,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 128,
bits: 128,
signed: false,
},
),
@ -134,7 +134,7 @@ Type {
Type {
kind: Int(
Int {
bit_width: 64,
bits: 64,
signed: false,
},
),