Skip to content

Commit 9ebcd5b

Browse files
kpreidgitbot
authored and
gitbot
committed
Optionally add type names to TypeIds.
This feature is intended to provide expensive but thorough help for developers who have an unexpected `TypeId` value and need to determine what type it actually is. It causes `impl Debug for TypeId` to print the type name in addition to the opaque ID hash, and in order to do so, adds a name field to `TypeId`. The cost of this is the increased size of `TypeId` and the need to store type names in the binary; therefore, it is an optional feature. It may be enabled via `cargo -Zbuild-std -Zbuild-std-features=debug_typeid`. (Note that `-Zbuild-std-features` disables default features which you may wish to reenable in addition; see <https://doc.rust-lang.org/cargo/reference/unstable.html#build-std-features>.) Example usage and output: ``` fn main() { use std::any::{Any, TypeId}; dbg!(TypeId::of::<usize>(), drop::<usize>.type_id()); } ``` ``` TypeId::of::<usize>() = TypeId(0x763d199bccd319899208909ed1a860c6 = usize) drop::<usize>.type_id() = TypeId(0xe6a34bd13f8c92dd47806da07b8cca9a = core::mem::drop<usize>) ``` Also added feature declarations for the existing `debug_refcell` feature so it is usable from the `rust.std-features` option of `config.toml`.
1 parent 28c6d09 commit 9ebcd5b

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

Diff for: core/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ optimize_for_size = []
2323
# Make `RefCell` store additional debugging information, which is printed out when
2424
# a borrow error occurs
2525
debug_refcell = []
26+
# Make `TypeId` store a reference to the name of the type, so that it can print that name.
27+
debug_typeid = []
2628

2729
[lints.rust.unexpected_cfgs]
2830
level = "warn"

Diff for: core/src/any.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,8 @@ pub struct TypeId {
711711
// We avoid using `u128` because that imposes higher alignment requirements on many platforms.
712712
// See issue #115620 for more information.
713713
t: (u64, u64),
714+
#[cfg(feature = "debug_typeid")]
715+
name: &'static str,
714716
}
715717

716718
#[stable(feature = "rust1", since = "1.0.0")]
@@ -741,10 +743,14 @@ impl TypeId {
741743
#[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
742744
pub const fn of<T: ?Sized + 'static>() -> TypeId {
743745
let t: u128 = intrinsics::type_id::<T>();
744-
745746
let t1 = (t >> 64) as u64;
746747
let t2 = t as u64;
747-
TypeId { t: (t1, t2) }
748+
749+
TypeId {
750+
t: (t1, t2),
751+
#[cfg(feature = "debug_typeid")]
752+
name: type_name::<T>(),
753+
}
748754
}
749755

750756
fn as_u128(self) -> u128 {
@@ -775,7 +781,15 @@ impl hash::Hash for TypeId {
775781
#[stable(feature = "rust1", since = "1.0.0")]
776782
impl fmt::Debug for TypeId {
777783
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
778-
write!(f, "TypeId({:#034x})", self.as_u128())
784+
#[cfg(feature = "debug_typeid")]
785+
{
786+
write!(f, "TypeId({:#034x} = {})", self.as_u128(), self.name)?;
787+
}
788+
#[cfg(not(feature = "debug_typeid"))]
789+
{
790+
write!(f, "TypeId({:#034x})", self.as_u128())?;
791+
}
792+
Ok(())
779793
}
780794
}
781795

Diff for: coretests/tests/any.rs

+8
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ fn any_unsized() {
118118
is_any::<[i32]>();
119119
}
120120

121+
#[cfg(feature = "debug_typeid")]
122+
#[test]
123+
fn debug_typeid_includes_name() {
124+
let type_id = TypeId::of::<[usize; 2]>();
125+
let debug_str = format!("{type_id:?}");
126+
assert!(debug_str.ends_with("= [usize; 2])"), "{debug_str:?} did not match");
127+
}
128+
121129
#[test]
122130
fn distinct_type_names() {
123131
// https://github.com/rust-lang/rust/issues/84666

Diff for: std/Cargo.toml

+7
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ panic_immediate_abort = [
110110
# Choose algorithms that are optimized for binary size instead of runtime performance
111111
optimize_for_size = ["core/optimize_for_size", "alloc/optimize_for_size"]
112112

113+
# Make `RefCell` store additional debugging information, which is printed out when
114+
# a borrow error occurs
115+
debug_refcell = ["core/debug_refcell"]
116+
# Make `TypeId` store a reference to the name of the type, so that it can print that name.
117+
debug_typeid = ["core/debug_typeid"]
118+
119+
113120
# Enable std_detect default features for stdarch/crates/std_detect:
114121
# https://github.com/rust-lang/stdarch/blob/master/crates/std_detect/Cargo.toml
115122
std_detect_file_io = ["std_detect/std_detect_file_io"]

Diff for: sysroot/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ compiler-builtins-mem = ["std/compiler-builtins-mem"]
1919
compiler-builtins-no-asm = ["std/compiler-builtins-no-asm"]
2020
compiler-builtins-no-f16-f128 = ["std/compiler-builtins-no-f16-f128"]
2121
compiler-builtins-mangled-names = ["std/compiler-builtins-mangled-names"]
22+
debug_refcell = ["std/debug_refcell"]
23+
debug_typeid = ["std/debug_typeid"]
2224
llvm-libunwind = ["std/llvm-libunwind"]
2325
system-llvm-libunwind = ["std/system-llvm-libunwind"]
26+
optimize_for_size = ["std/optimize_for_size"]
2427
panic-unwind = ["std/panic_unwind"]
2528
panic_immediate_abort = ["std/panic_immediate_abort"]
26-
optimize_for_size = ["std/optimize_for_size"]
2729
profiler = ["dep:profiler_builtins"]
2830
std_detect_file_io = ["std/std_detect_file_io"]
2931
std_detect_dlsym_getauxval = ["std/std_detect_dlsym_getauxval"]

0 commit comments

Comments
 (0)