-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Support primitives in type info reflection #151123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
|
7fccb29 to
1066081
Compare
1066081 to
8281828
Compare
This comment has been minimized.
This comment has been minimized.
8281828 to
8d00b3b
Compare
There was a problem hiding this 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.
| /// The type id of signed integer type. | ||
| pub ty: TypeId, | ||
| /// The bit width of the signed integer type. | ||
| pub bit_width: usize, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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(_)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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
This comment has been minimized.
This comment has been minimized.
8d00b3b to
80ae7d8
Compare
Support {bool,char,int,uint,float,str} primitive types for feature
`type_info` reflection.
80ae7d8 to
79ec275
Compare
In that case, it might be better to place |
There was a problem hiding this 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
| /// The type id of signed integer type. | ||
| pub ty: TypeId, | ||
| /// The bit width of the signed integer type. | ||
| pub bit_width: usize, |
There was a problem hiding this comment.
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(_)
ee3618c to
b2a7b18
Compare
|
@rustbot review Removed |
|
@bors r+ rollup |
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
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
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
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
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
Tracking issue: #146922
#![feature(type_info)]This PR supports {
bool,char,int,uint,float,str} primitive types for featuretype_inforeflection.r? @oli-obk