Repository navigation
reflection and comptime #406
Description
Activity
This issue is intended for status updates only.
For general questions or comments, please contact the owner(s) directly.
- locked and limited conversation to collaborators
on Sep 16, 2025 I implemented an initial MVP supporting only tuples and primitives (tho those are just opaque things you can't interact with further), and getting offsets for the tuple fields as well as the size of the tuple: rust-lang/rust#146923
There are two designs of how to expose this from a libs perspective, but after a sync meeting with scottmcm yesterday we came to the conclusion that neither is objectively better at this stage so we're just going to go with the nice end-user UX version for now. For details see the PR description.
Once the MVP lands, I will mentor various interested contributors who will keep adding fields to the Type struct and variants the TypeKind enum.
The next major step is restricting what information you can get from structs outside of the current module or crate. We want to honor visibility, so an initial step would be to just never show private fields, but we want to explore allowing private fields to be shown either just within the current module or via some opt-in marker trait
Another related PR:
Updates
- Add very basic "comptime" fn implementation rust#148820 adds a way to mark functions and intrinsics as only callable during CTFE
- [WIP] Add downcast_trait and downcast_trait_mut rust#144363 has been unblocked and just needs some minor cosmetic work
Blockers
- Reflection MVP rust#146923 (reflection MVP) has not been reviewed yet
- Reflection MVP rust#146923 has landed, and we even got the first contribs adding array support to reflection.
- there are lots more types and type information that we could support, and it's rather easy to add more. Happy to review any work here.
- Add try_as_dyn and try_as_dyn_mut rust#150033 has landed, and I'm working on removing the
'staticrequirement in Remove 'static requirement on try_as_dyn rust#150161
- Reflection MVP rust#146923 has landed, and we even got the first contribs adding array support to reflection.
- @BD103 added slices, arrays and raw pointer support
- @SpriteOvO added all of our primitives
- @clubby789 gave us references
- @izagawd made it possible to extract some info from dyn Trait
There is ongoing work for Adts and function pointers, both of which will land as MVPs and will need some work to make them respect semver or generally become useful in practice
Removing the
'staticbound fromtry_as_dynturned out to have many warts, so I'm limiting it to a much smaller subset and will have borrowck emit the'staticrequirement if the other rules do not apply (instead of having an unconditional'staticrequirement)- I added support for getting reflection information of any type, not just
'staticones - @9SonSteroids added a function pointer MVP and trait object support
- @SpriteOvO added basic struct/enum/union support
- I added support for getting reflection information of any type, not just
No changes since last time.
I'm writing a document for the lang team meeting on reflection next week
2 remaining items
This is a continuing project goal, and the updates below this comment will be for the new period 2026
- addedex-2025h2Goals that were previously in 2025h2 (for reporting)Goals that were previously in 2025h2 (for reporting)and removed
on May 11, 2026 We're changing the design away from one big enum with lots of structs inside to a more "many small functions giving you pieces" system. This has various reasons:
- we stop computing the layout ahead of time for reflection, so you can use reflection to analyze the fields of a struct within an array length of a field of that struct. Arguably niche, but I expect there are more realistic layout cycle errors we would encounter in practical usage
- it's so much simpler inside the compiler impl
- the reflection libraries want their own representation anyway and they often just want a subset, so now they only pay for what they use
@SpriteOvO has started this work in
- Refactor
Type::sizefield toTypeId::sizemethod fortype_inforust#156223 - Add
TypeIdmethodsvariantsfieldsfieldfortype_inforust#156403
I landed an MVP of comptime fns in
and started using it for the reflection methods in
There's still lots to figure out around comptime and how it interacts with const traits, because in contrast to
const Traitbounds, which are essentially sugar forTrait + rustc_comptime!(Trait)bounds, we do actually need something that adds bounds that can only be used at compile time (therustc_comptime!macro doesn't actually exist).has landed. You can now
try_as_dynon anyT, even without a'staticbound. On the negative side, this now means we returnNonein more cases. See the documentation oftry_as_dynfor details.moves over some more information from the big enum to individual methods. This made me realize we need the ability to fetch information directly for enum variants (name, non_exhaustive, ...). While we could just add a variant index to all the intrinsics, it would be nicer if we did the FRT thing and just had pattern types for individual variants, and were able to fetch information from them. Short term we'll just use the variant index and have a
Variantdata structure that wraps the index and the base type's typeid.@yara-blue has made good progress on moving to the "many intrinsics" design (in contrast to the less flexible "one big enum" design):
- Remove fields from TypeKind: Array, Slice rust#161896
- Reflection refactor ptrs rust#162294
- Remove fields from TypeKind: Struct, Enum, Union and Tuple rust#161114
- Remove fields from TypeKind: Bool, Char, Float and Int rust#161401
- Add
TypeId::is_signedmethod rust#160925
Once the transformation is complete, we'll add more methods until we have parity with at least bevy-reflect. Then we'll do an experiment to see if we can make the bevy-reflect derives be no-ops and instead have a single reflection based impl for
bevy_reflect::ReflectYara continues cleaning up the API and the compiler impl
She's also creating demos (e.g. a debug printer that uses
Debugwhere it is there and reflection for debugging otherwise), and discovering what APIs are missing to enable some use cases.
Summary
Finish the implemented reflection scheme based on
const fnthat can only be called at compile time.Validate it against existing reflection libraries by giving them a nightly feature that obsoletes having derives and makes the derives no-ops.
Obtain T-lang and T-libs buy-in for the scheme and write an RFC.
This proposal is solely for producing const eval values, not for putting types back into the type system.
That will be a follow-up once this proposal has a merged MVP.
This issue is intended for status updates only.
For general questions or comments, please contact a goal owner or post in the Zulip topic.