From 4609fbcc59c8d50ebfbb5a550e348463ef28c381 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 23 Jul 2026 17:12:32 +0200 Subject: [PATCH] Mark some more comptime-only things as comptime --- library/core/src/intrinsics/mod.rs | 26 ++++++++++++------- library/core/src/mem/type_info.rs | 3 ++- .../const-eval-intrinsic-promotion.rs | 4 +-- .../const-eval-intrinsic-promotion.stderr | 8 +++--- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/library/core/src/intrinsics/mod.rs b/library/core/src/intrinsics/mod.rs index fccb6ee6def..7210d850c86 100644 --- a/library/core/src/intrinsics/mod.rs +++ b/library/core/src/intrinsics/mod.rs @@ -873,7 +873,8 @@ pub const unsafe fn transmute_unchecked(src: Src) -> Dst; #[rustc_intrinsic_const_stable_indirect] #[rustc_nounwind] #[rustc_intrinsic] -pub const fn needs_drop() -> bool; +#[rustc_comptime] +pub fn needs_drop() -> bool; /// Calculates the offset from a pointer. /// @@ -2945,7 +2946,8 @@ pub unsafe fn vtable_align(ptr: *const ()) -> usize; #[unstable(feature = "core_intrinsics", issue = "none")] #[rustc_intrinsic_const_stable_indirect] #[rustc_intrinsic] -pub const fn size_of() -> usize; +#[rustc_comptime] +pub fn size_of() -> usize; /// The minimum alignment of a type. /// @@ -2964,7 +2966,8 @@ pub const fn size_of() -> usize; #[unstable(feature = "core_intrinsics", issue = "none")] #[rustc_intrinsic_const_stable_indirect] #[rustc_intrinsic] -pub const fn align_of() -> usize; +#[rustc_comptime] +pub fn align_of() -> usize; /// The offset of a field inside a type. /// @@ -2984,7 +2987,8 @@ pub const fn align_of() -> usize; #[rustc_intrinsic_const_stable_indirect] #[rustc_intrinsic] #[lang = "offset_of"] -pub const fn offset_of(variant: u32, field: u32) -> usize; +#[rustc_comptime] +pub fn offset_of(variant: u32, field: u32) -> usize; /// The offset of a field queried by its field representing type. /// @@ -2998,7 +3002,8 @@ pub const fn offset_of(variant: u32, field: u32) -> usize; #[rustc_intrinsic] #[unstable(feature = "field_projections", issue = "145383")] #[rustc_const_unstable(feature = "field_projections", issue = "145383")] -pub const fn field_offset() -> usize; +#[rustc_comptime] +pub fn field_offset() -> usize; /// Returns the number of variants of the type `T` cast to a `usize`; /// if `T` has no variants, returns `0`. Uninhabited variants will be counted. @@ -3012,7 +3017,8 @@ pub const fn field_offset() -> usize; #[rustc_nounwind] #[unstable(feature = "core_intrinsics", issue = "none")] #[rustc_intrinsic] -pub const fn variant_count() -> usize; +#[rustc_comptime] +pub fn variant_count() -> usize; /// The size of the referenced value in bytes. /// @@ -3056,9 +3062,8 @@ pub fn type_id_vtable( /// not implement it. #[rustc_intrinsic] #[unstable(feature = "core_intrinsics", issue = "none")] -pub const fn type_of(_id: crate::any::TypeId) -> crate::mem::type_info::Type { - panic!("`TypeId::info` can only be called at compile-time") -} +#[rustc_comptime] +pub fn type_of(_id: crate::any::TypeId) -> crate::mem::type_info::Type; /// Gets a static string slice containing the name of a type. /// @@ -3071,7 +3076,8 @@ pub const fn type_of(_id: crate::any::TypeId) -> crate::mem::type_info::Type { #[rustc_nounwind] #[unstable(feature = "core_intrinsics", issue = "none")] #[rustc_intrinsic] -pub const fn type_name() -> &'static str; +#[rustc_comptime] +pub fn type_name() -> &'static str; /// Gets an identifier which is globally unique to the specified type. This /// function will return the same value for a type regardless of whichever diff --git a/library/core/src/mem/type_info.rs b/library/core/src/mem/type_info.rs index 7614adbcc53..75270b2d5b1 100644 --- a/library/core/src/mem/type_info.rs +++ b/library/core/src/mem/type_info.rs @@ -37,7 +37,8 @@ impl TypeId { /// It can only be called at compile time. #[unstable(feature = "type_info", issue = "146922")] #[rustc_const_unstable(feature = "type_info", issue = "146922")] - pub const fn info(self) -> Type { + #[rustc_comptime] + pub fn info(self) -> Type { type_of(self) } } diff --git a/tests/ui/consts/const-eval/const-eval-intrinsic-promotion.rs b/tests/ui/consts/const-eval/const-eval-intrinsic-promotion.rs index bdcf537859c..0de46b3a594 100644 --- a/tests/ui/consts/const-eval/const-eval-intrinsic-promotion.rs +++ b/tests/ui/consts/const-eval/const-eval-intrinsic-promotion.rs @@ -1,6 +1,6 @@ #![feature(core_intrinsics)] fn main() { // Test that calls to intrinsics are never promoted - let x: &'static usize = - &std::intrinsics::size_of::(); //~ ERROR temporary value dropped while borrowed + let x: &'static () = + &std::intrinsics::cold_path(); //~ ERROR temporary value dropped while borrowed } diff --git a/tests/ui/consts/const-eval/const-eval-intrinsic-promotion.stderr b/tests/ui/consts/const-eval/const-eval-intrinsic-promotion.stderr index d533dcaa6f6..bb36caf64dd 100644 --- a/tests/ui/consts/const-eval/const-eval-intrinsic-promotion.stderr +++ b/tests/ui/consts/const-eval/const-eval-intrinsic-promotion.stderr @@ -1,10 +1,10 @@ error[E0716]: temporary value dropped while borrowed --> $DIR/const-eval-intrinsic-promotion.rs:5:10 | -LL | let x: &'static usize = - | -------------- type annotation requires that borrow lasts for `'static` -LL | &std::intrinsics::size_of::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use +LL | let x: &'static () = + | ----------- type annotation requires that borrow lasts for `'static` +LL | &std::intrinsics::cold_path(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use LL | } | - temporary value is freed at the end of this statement