Do not require 'static for obtaining reflection information.

This commit is contained in:
Oli Scherer 2026-02-07 19:24:59 +00:00
parent 06cafcbe08
commit 3339b061af
2 changed files with 11 additions and 5 deletions

View file

@ -2887,7 +2887,7 @@ pub const fn type_name<T: ?Sized>() -> &'static str;
#[rustc_nounwind]
#[unstable(feature = "core_intrinsics", issue = "none")]
#[rustc_intrinsic]
pub const fn type_id<T: ?Sized + 'static>() -> crate::any::TypeId;
pub const fn type_id<T: ?Sized>() -> crate::any::TypeId;
/// Tests (at compile-time) if two [`crate::any::TypeId`] instances identify the
/// same type. This is necessary because at const-eval time the actual discriminating

View file

@ -2,7 +2,7 @@
//! runtime or const-eval processable way.
use crate::any::TypeId;
use crate::intrinsics::type_of;
use crate::intrinsics::{type_id, type_of};
/// Compile-time type information.
#[derive(Debug)]
@ -28,11 +28,17 @@ impl TypeId {
impl Type {
/// Returns the type information of the generic type parameter.
///
/// Note: Unlike `TypeId`s obtained via `TypeId::of`, the `Type`
/// struct and its fields contain `TypeId`s that are not necessarily
/// derived from types that outlive `'static`. This means that using
/// the `TypeId`s (transitively) obtained from this function will
/// be able to break invariants that other `TypeId` consuming crates
/// may have assumed to hold.
#[unstable(feature = "type_info", issue = "146922")]
#[rustc_const_unstable(feature = "type_info", issue = "146922")]
// FIXME(reflection): don't require the 'static bound
pub const fn of<T: ?Sized + 'static>() -> Self {
const { TypeId::of::<T>().info() }
pub const fn of<T: ?Sized>() -> Self {
const { type_id::<T>().info() }
}
}