Skip to content

Add TypeId methods variants fields field for type_info#156403

Open
SpriteOvO wants to merge 4 commits into
rust-lang:mainfrom
SpriteOvO:type-info-refactor-variant
Open

Add TypeId methods variants fields field for type_info#156403
SpriteOvO wants to merge 4 commits into
rust-lang:mainfrom
SpriteOvO:type-info-refactor-variant

Conversation

@SpriteOvO
Copy link
Copy Markdown
Member

@SpriteOvO SpriteOvO commented May 10, 2026

Tracking issue #146922

  • Adds fn TypeId::variants returns the number of variants, for struct and union and primitive types, it's always 1.
  • Adds fn TypeId::fields returns the number of fields.
  • Adds fn TypeId::field returns a field representing type FieldId.
  • Adds a new type FieldId, which is a wrapper of TypeId for fields.

For methods {fields,field}, if indexing out of bounds, a compile-time error will be raised.

Regarding the removal of Type items, this will be done in a later PR in one go.

r? @oli-obk

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 10, 2026

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

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

@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 May 10, 2026
@rust-log-analyzer

This comment has been minimized.

@SpriteOvO SpriteOvO marked this pull request as draft May 10, 2026 16:25
@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 May 10, 2026
@SpriteOvO SpriteOvO force-pushed the type-info-refactor-variant branch from c503c83 to 827c2f9 Compare May 10, 2026 16:39
@SpriteOvO SpriteOvO marked this pull request as ready for review May 10, 2026 16:41
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 10, 2026
@rust-log-analyzer

This comment has been minimized.

Comment thread library/core/src/mem/type_info.rs Outdated
/// ```
#[unstable(feature = "type_info", issue = "146922")]
#[rustc_const_unstable(feature = "type_info", issue = "146922")]
pub const fn fields(self, variant_index: usize) -> usize {
Copy link
Copy Markdown
Contributor

@fbstj fbstj May 10, 2026

Choose a reason for hiding this comment

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

are these indexes affected by specified descriminants? ie does enum Descr { Five = 5 } have a variant_index = 5 ?

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Inside the compiler variant index refers to the lexical order of variants. So Descr::Five would have variant index 0. Should probably be documented here though.

Copy link
Copy Markdown
Member Author

@SpriteOvO SpriteOvO May 11, 2026

Choose a reason for hiding this comment

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

I'll update the doc later. But this is indeed worth mentioning for SemVer and privacy. If we use the current "index" term, then reordering enum variants or struct fields will result in different reflection results.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Which may be entirely intentional. Proc macros also use source order. If you want sth else you need to sort by another key.

An alternative would be to provide fields and variants by a sorted list of names and require getting their info by names. Idk if that gets us into trouble with macros defining equal field names with different hygiene, but I hope we just error on that

@SpriteOvO SpriteOvO force-pushed the type-info-refactor-variant branch from 827c2f9 to 8313075 Compare May 17, 2026 10:29
@rust-log-analyzer

This comment has been minimized.

Comment thread library/core/src/mem/type_info.rs Outdated
@SpriteOvO SpriteOvO 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 May 20, 2026
@SpriteOvO SpriteOvO force-pushed the type-info-refactor-variant branch from 8313075 to 12dee7d Compare May 21, 2026 21:10
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 21, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 21, 2026
@rust-log-analyzer

This comment has been minimized.

@SpriteOvO SpriteOvO force-pushed the type-info-refactor-variant branch from 12dee7d to 28733aa Compare May 21, 2026 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants