Skip to content

Commit 16da3ab

Browse files
Rollup merge of #152381 - oli-obk:non_static_reflection, r=Mark-Simulacrum
Do not require `'static` for obtaining reflection information. tracking issue #142577 This does not affect the stable `TypeId::of`, as that has its own `'static` bound. But it will allow obtaining `TypeId`s for non-static types via the reflection API. To obtain such a `TypeId` for any type, just use `Type::of::<(T,)>().kind` to extract the first field of a tuple. This effectively reintroduces #41875, which @rust-lang/lang decided against allowing back in 2018 due to lack of sound use cases. We will thus need to have a T-lang meeting specifically about `TypeId` for non-static types before *stabilizing* any part of reflection (in addition to T-lang meetings about reflection in general). I'm adding an explicit point about this to the tracking issue. cc @scottmcm @joshtriplett @9SonSteroids @SpriteOvO @izagawd @BD103
2 parents 33c2a6e + 3339b06 commit 16da3ab

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

library/core/src/intrinsics/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2886,7 +2886,7 @@ pub const fn type_name<T: ?Sized>() -> &'static str;
28862886
#[rustc_nounwind]
28872887
#[unstable(feature = "core_intrinsics", issue = "none")]
28882888
#[rustc_intrinsic]
2889-
pub const fn type_id<T: ?Sized + 'static>() -> crate::any::TypeId;
2889+
pub const fn type_id<T: ?Sized>() -> crate::any::TypeId;
28902890

28912891
/// Tests (at compile-time) if two [`crate::any::TypeId`] instances identify the
28922892
/// same type. This is necessary because at const-eval time the actual discriminating

library/core/src/mem/type_info.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! runtime or const-eval processable way.
33
44
use crate::any::TypeId;
5-
use crate::intrinsics::type_of;
5+
use crate::intrinsics::{type_id, type_of};
66

77
/// Compile-time type information.
88
#[derive(Debug)]
@@ -28,11 +28,17 @@ impl TypeId {
2828

2929
impl Type {
3030
/// Returns the type information of the generic type parameter.
31+
///
32+
/// Note: Unlike `TypeId`s obtained via `TypeId::of`, the `Type`
33+
/// struct and its fields contain `TypeId`s that are not necessarily
34+
/// derived from types that outlive `'static`. This means that using
35+
/// the `TypeId`s (transitively) obtained from this function will
36+
/// be able to break invariants that other `TypeId` consuming crates
37+
/// may have assumed to hold.
3138
#[unstable(feature = "type_info", issue = "146922")]
3239
#[rustc_const_unstable(feature = "type_info", issue = "146922")]
33-
// FIXME(reflection): don't require the 'static bound
34-
pub const fn of<T: ?Sized + 'static>() -> Self {
35-
const { TypeId::of::<T>().info() }
40+
pub const fn of<T: ?Sized>() -> Self {
41+
const { type_id::<T>().info() }
3642
}
3743
}
3844

0 commit comments

Comments
 (0)