Skip to content

Conversation

@coolcatcoder
Copy link

@coolcatcoder coolcatcoder commented Jan 17, 2026

This is for #146922.

It allows checking if a TypeId implements a trait.

(I've never used git from the terminal before, so that was a fun challenge. I hope I did everything correctly.)
(I haven't added any tests yet, I'll do that soon.)
(Zulip.)

@rustbot
Copy link
Collaborator

rustbot commented Jan 17, 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

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 17, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 17, 2026

r? @chenyukang

rustbot has assigned @chenyukang.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@coolcatcoder
Copy link
Author

Also, because this pull request is pretty much just copying and pasting from vtable_for's implementation, should we de-duplicate the code by putting the implementation in one location that both intrinsics can call?

@rust-log-analyzer

This comment has been minimized.

@xonx4l
Copy link
Contributor

xonx4l commented Jan 17, 2026

Also, because this pull request is pretty much just copying and pasting from vtable_for's implementation, should we de-duplicate the code by putting the implementation in one location that both intrinsics can call?

Sure , This way it's easier to maintain.

@rustbot
Copy link
Collaborator

rustbot commented Jan 17, 2026

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

@SpriteOvO SpriteOvO added the F-type_info #![feature(type_info)] label Jan 17, 2026
@rustbot

This comment was marked as outdated.

@rustbot rustbot added O-unix Operating system: Unix-like T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Jan 17, 2026
@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 17, 2026
@coolcatcoder

This comment was marked as off-topic.

@coolcatcoder

This comment was marked as off-topic.

@coolcatcoder

This comment was marked as off-topic.

@coolcatcoder

This comment was marked as off-topic.

@SpriteOvO

This comment was marked as off-topic.

@coolcatcoder

This comment was marked as off-topic.

@SpriteOvO SpriteOvO force-pushed the type_id_implements_trait branch from f603826 to 2f97f63 Compare January 17, 2026 11:13
@rustbot rustbot removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. has-merge-commits PR has merge commits, merge with caution. labels Jan 17, 2026
@SpriteOvO

This comment was marked as outdated.

@SpriteOvO SpriteOvO force-pushed the type_id_implements_trait branch from 2f97f63 to da054c7 Compare January 17, 2026 11:19
@SpriteOvO

This comment was marked as outdated.

@SpriteOvO SpriteOvO removed T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) O-unix Operating system: Unix-like T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Jan 17, 2026
@coolcatcoder

This comment was marked as off-topic.

Comment on lines +29 to +51
/// Checks if the type represented by the `TypeId` implements the trait.
/// It can only be called at compile time.
pub const fn implements_trait<
T: ptr::Pointee<Metadata = ptr::DynMetadata<T>> + ?Sized + 'static,
>(
self,
) -> bool {
type_id_implements_trait(self, TypeId::of::<T>())
}

/// Checks if the type represented by the `TypeId` implements the trait represented by the secondary `TypeId`.
/// Returns `None` if the `trait_represented_by_type_id` is not a trait represented by type id.
/// It can only be called at compile time.
pub const fn implements_trait_represented_by_type_id(
self,
trait_represented_by_type_id: Self,
) -> Option<bool> {
if type_id_is_trait(trait_represented_by_type_id) {
Some(type_id_implements_trait(self, trait_represented_by_type_id))
} else {
None
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Basically, type_info feature gate mainly introduces a struct Type, which contains all the type information reflected from a TypeId.

Regarding whether a struct implements a trait, I think this also relates to "reflection". Based on this, I would suggest that associating them with struct Type (becoming methods of it) might be more appropriate.

For naming, personally, has_trait feels more intuitive than implements_trait to me, though it's still too early to do name decisions.

Copy link
Author

Choose a reason for hiding this comment

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

Good points. I agree that has_trait does make more sense, especially when considering that the trait might not be implemented by the type specifically, and rather have come from a blanket.

Comment on lines +2849 to +2856
/// Check if a type represented by a `TypeId` is a dyn Trait.
/// It can only be called at compile time, the backends do
/// not implement it.
#[rustc_intrinsic]
#[unstable(feature = "core_intrinsics", issue = "none")]
pub const fn type_id_is_trait(_trait: crate::any::TypeId) -> bool {
panic!("`TypeId::implements_trait_represented_by_type_id` can only be called at compile-time")
}
Copy link
Member

Choose a reason for hiding this comment

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

This likely doesn't require a dedicated intrinsic. we can determine it through variants of enum TypeKind. Someone happens to be working on this, see #151239. Maybe you can wait for it to land.

mod step;
mod traits;
mod util;
pub mod util;
Copy link
Member

Choose a reason for hiding this comment

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

I haven't looked closely at how CompileTimeMachine interacts with InterpCx, but I believe there's a better way to share code than putting it into the util mod.

@SpriteOvO
Copy link
Member

@rustbot author

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 17, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 17, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jan 17, 2026
@SpriteOvO SpriteOvO added the S-blocked Status: Blocked on something else such as an RFC or other implementation work. label Jan 19, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 19, 2026

☔ The latest upstream changes (presumably #151363) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-type_info #![feature(type_info)] S-blocked Status: Blocked on something else such as an RFC or other implementation work. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

7 participants