Skip to content

Conversation

@SpriteOvO
Copy link
Member

@SpriteOvO SpriteOvO commented Jan 14, 2026

Tracking issue: #146922 #![feature(type_info)]

This PR supports {bool,char,int,uint,float,str} primitive types for feature type_info reflection.

r? @oli-obk

@SpriteOvO SpriteOvO added the F-type_info #![feature(type_info)] label Jan 14, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 14, 2026

The reflection data structures are tied exactly to the implementation
in the compiler. Make sure to also adjust rustc_const_eval/src/const_eval/type_info.rs

cc @oli-obk

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 14, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 14, 2026

oli-obk is not on the review rotation at the moment.
They may take a while to respond.

@SpriteOvO SpriteOvO force-pushed the type-info-primitives branch 3 times, most recently from 7fccb29 to 1066081 Compare January 14, 2026 16:13
@SpriteOvO SpriteOvO added the A-type-system Area: Type system label Jan 14, 2026
@SpriteOvO SpriteOvO force-pushed the type-info-primitives branch from 1066081 to 8281828 Compare January 14, 2026 16:15
@rust-log-analyzer

This comment has been minimized.

@SpriteOvO SpriteOvO force-pushed the type-info-primitives branch from 8281828 to 8d00b3b Compare January 14, 2026 16:32
Copy link
Member Author

@SpriteOvO SpriteOvO left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also added a ty: TypeId to all primitive variants to represent their own type. The rationale is that users won't need to manually retrieve the TypeId when accessing type info (especially through Type::of::<T>() usage). I'm not sure if this approach is advisable, and whether it's necessary to also provide a ty: TypeId for other variants like Tuple and Array.

View changes since this review

/// The type id of signed integer type.
pub ty: TypeId,
/// The bit width of the signed integer type.
pub bit_width: usize,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether we should use usize or u64 here to represent the bit width.

Besides, as Option is not used here, reflection on isize and usize will fall into the Int and Uint variants, and users cannot distinguish between them, which may be an issue.

Is it necessary for end users to distinguish between {u,i}size and {u,i}int? If yes, should we add a separate field or add distinct variants to indicate it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having a single Int variant that has a signed: bool field is nicer. It avoids some recurring matches that do Int(_) | Uint(_)

Copy link
Contributor

@Skgland Skgland Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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::BITS123 constant that already exists on the integer types.

Footnotes

  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

Copy link
Member Author

@SpriteOvO SpriteOvO Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Skgland Nice point. I found some previous discussions 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.

If there is no objection, I will make this change in a follow-up PR later (as this PR is already r+).

UPDATE: Follow-up PR #151235

@rust-log-analyzer

This comment has been minimized.

@SpriteOvO SpriteOvO force-pushed the type-info-primitives branch from 8d00b3b to 80ae7d8 Compare January 14, 2026 18:14
Support {bool,char,int,uint,float,str} primitive types for feature
`type_info` reflection.
@SpriteOvO
Copy link
Member Author

I also added a ty: TypeId to all primitive variants to represent their own type. The rationale is that users won't need to manually retrieve the TypeId when accessing type info (especially through Type::of::<T>() usage). I'm not sure if this approach is advisable, and whether it's necessary to also provide a ty: TypeId for other variants like Tuple and Array.

In that case, it might be better to place ty: TypeId under struct Type rather than under enum TypeKind's variants I think.

Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I think we can leave off the extra TypeId field. I understand that reflection libraries have that on their Opaque variant, but they can still do that when processing the rustc reflection info. I'd rather users invoke TypeId::of manually when they also need the type id

View changes since this review

/// The type id of signed integer type.
pub ty: TypeId,
/// The bit width of the signed integer type.
pub bit_width: usize,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having a single Int variant that has a signed: bool field is nicer. It avoids some recurring matches that do Int(_) | Uint(_)

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 15, 2026
@SpriteOvO SpriteOvO force-pushed the type-info-primitives branch from ee3618c to b2a7b18 Compare January 15, 2026 22:05
@SpriteOvO
Copy link
Member Author

@rustbot review

Removed ty: TypeId from variants, merged variant Uint into Int.

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jan 15, 2026
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 15, 2026
@oli-obk
Copy link
Contributor

oli-obk commented Jan 16, 2026

@bors r+ rollup

@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 16, 2026

📌 Commit b2a7b18 has been approved by oli-obk

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 16, 2026
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 16, 2026
Support primitives in type info reflection

Tracking issue: rust-lang#146922 `#![feature(type_info)]`

This PR supports {`bool`,`char`,`int`,`uint`,`float`,`str`} primitive types for feature `type_info` reflection.

r? @oli-obk
rust-bors bot pushed a commit that referenced this pull request Jan 16, 2026
Rollup of 6 pull requests

Successful merges:

 - #145354 (Cache derive proc macro expansion with incremental query)
 - #151123 (Support primitives in type info reflection)
 - #151178 (simplify words initialization using Rc::new_zeroed)
 - #151187 (Use `default_field_values` more in `Resolver`)
 - #151197 (remove lcnr from compiler review rotation)
 - #151203 (Revert `QueryStackFrame` split)

r? @ghost
@rust-bors rust-bors bot merged commit 225ba69 into rust-lang:main Jan 16, 2026
11 checks passed
@rustbot rustbot added this to the 1.94.0 milestone Jan 16, 2026
@SpriteOvO SpriteOvO deleted the type-info-primitives branch January 16, 2026 20:22
rust-timer added a commit that referenced this pull request Jan 16, 2026
Rollup merge of #151123 - type-info-primitives, r=oli-obk

Support primitives in type info reflection

Tracking issue: #146922 `#![feature(type_info)]`

This PR supports {`bool`,`char`,`int`,`uint`,`float`,`str`} primitive types for feature `type_info` reflection.

r? @oli-obk
jhpratt added a commit to jhpratt/rust that referenced this pull request Jan 18, 2026
Change field `bit_width: usize` to `bits: u32` in type info

Follow-up rust-lang#151123 (comment). 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](rust-lang#76492 (comment)) regarding the type of `::BITS` constant. And during the stabilization of `::BITS`, the choice of `u32` affected some ecosystem crates (rust-lang#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
jhpratt added a commit to jhpratt/rust that referenced this pull request Jan 18, 2026
Change field `bit_width: usize` to `bits: u32` in type info

Follow-up rust-lang#151123 (comment). 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](rust-lang#76492 (comment)) regarding the type of `::BITS` constant. And during the stabilization of `::BITS`, the choice of `u32` affected some ecosystem crates (rust-lang#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
rust-timer added a commit that referenced this pull request Jan 18, 2026
Rollup merge of #151235 - type-info-rename-bits, r=oli-obk

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

Follow-up #151123 (comment). 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](#76492 (comment)) 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-type-system Area: Type system F-type_info #![feature(type_info)] S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants