From 38f4f4aeac996dc6d3312f6edbf5fccdcea80962 Mon Sep 17 00:00:00 2001 From: Hanna Kruppe Date: Mon, 15 Jun 2026 22:11:23 +0200 Subject: [PATCH] Use constant for detecting thin pointer formatting This allows codegen to prune the unnecessary side of the `if` for each pointer type during monomorphization. LLVM optimizations can clean it up too, but it's better to not emit unnecessary code in the first place. --- library/core/src/fmt/mod.rs | 2 +- library/core/src/unit.rs | 15 +++++---------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index c4bab254c146c..80b607f4834c9 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -3000,7 +3000,7 @@ impl Pointer for *const T { // metadata type to reduce the amount of codegen work needed for each distinct type. let ptr: *const T = *self; let ptr_addr = ptr.expose_provenance(); - if <::Metadata as core::unit::IsUnit>::is_unit() { + if <::Metadata as core::unit::IsUnit>::IS_UNIT { pointer_fmt_inner(ptr_addr, f) } else { wide_pointer_fmt_inner(ptr_addr, &core::ptr::metadata(ptr), f) diff --git a/library/core/src/unit.rs b/library/core/src/unit.rs index d54816c444bc4..5d9c79538e371 100644 --- a/library/core/src/unit.rs +++ b/library/core/src/unit.rs @@ -1,3 +1,5 @@ +use crate::intrinsics::type_id; + /// Collapses all unit items from an iterator into one. /// /// This is more useful when combined with higher-level abstractions, like @@ -19,17 +21,10 @@ impl FromIterator<()> for () { } pub(crate) trait IsUnit { - fn is_unit() -> bool; + const IS_UNIT: bool; } impl IsUnit for T { - default fn is_unit() -> bool { - false - } -} - -impl IsUnit for () { - fn is_unit() -> bool { - true - } + // `type_id` erases lifetimes, but that's OK here because "is it ()" never depends on lifetimes + const IS_UNIT: bool = type_id::() == type_id::<()>(); }