From 48e85ddc6dfb6160b365b5ee4316cfbf32c0413d Mon Sep 17 00:00:00 2001 From: sgasho Date: Fri, 20 Nov 2026 12:51:11 +0000 Subject: [PATCH] reflection: erase bound regions in function_ptr() * reflection: erase bound regions in function_ptr() * leave FIXME for handling bound lifetimes --- .../src/const_eval/type_info/intrinsics.rs | 3 ++- library/coretests/tests/mem/fn_ptr.rs | 18 ++++++++++++------ library/coretests/tests/mem/trait_info_of.rs | 11 +++++++++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_const_eval/src/const_eval/type_info/intrinsics.rs b/compiler/rustc_const_eval/src/const_eval/type_info/intrinsics.rs index 081d6715dd1d1..9c395242ffef6 100644 --- a/compiler/rustc_const_eval/src/const_eval/type_info/intrinsics.rs +++ b/compiler/rustc_const_eval/src/const_eval/type_info/intrinsics.rs @@ -166,7 +166,8 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> { }, dest, |ecx, place, (sig, fn_header)| { - let sig = sig.skip_binder(); // FIXME: handle lifetime bounds + // FIXME: handle lifetime bounds + let sig = ecx.tcx.instantiate_bound_regions_with_erased(*sig); ecx.write_fn_ptr_type_info(place, &sig, fn_header) }, )? diff --git a/library/coretests/tests/mem/fn_ptr.rs b/library/coretests/tests/mem/fn_ptr.rs index a4313ac744e43..6e89b0000feea 100644 --- a/library/coretests/tests/mem/fn_ptr.rs +++ b/library/coretests/tests/mem/fn_ptr.rs @@ -1,10 +1,11 @@ use std::any::TypeId; -use std::mem::type_info::{Abi, FnPtr, TypeKind}; +use std::mem::type_info::{Abi, TypeKind}; const STRING_TY: TypeId = const { TypeId::of::() }; const U8_TY: TypeId = const { TypeId::of::() }; const UNIT_TY: TypeId = const { TypeId::of::<()>() }; const TUPLE_STRING_U8_TY: TypeId = const { TypeId::of::<(String, u8)>() }; +const STATIC_REGION_UNIT_TY: TypeId = const { TypeId::of::<&'static ()>() }; #[test] fn test_fn_ptrs() { @@ -28,11 +29,16 @@ fn test_type() { #[test] fn test_ref() { - // references are tricky because the lifetimes give the references different type ids - // so we check the pointees instead - const F: FnPtr = TypeId::of::().function_ptr().unwrap(); - assert_eq!(const { F.inputs()[0].points_to() }, Some(U8_TY)); - assert_eq!(const { F.inputs()[1].points_to() }, Some(U8_TY)); + // function_ptr replaces bound regions with erased ones. + // So, check if TypeId of &'a () is identical to &'static () + let f1 = const { TypeId::of:: fn(&'a ()) -> &'a ()>().function_ptr().unwrap() }; + assert_eq!(f1.inputs(), [STATIC_REGION_UNIT_TY]); + assert_eq!(f1.output(), STATIC_REGION_UNIT_TY); + + // function_ptr() does not replace inner bound regions of a nested fn + let f2 = + const { TypeId::of:: fn(for<'x> fn(&'x u8, &'a u8))>().function_ptr().unwrap() }; + assert_eq!(f2.inputs(), [const { TypeId::of:: fn(&'x u8, &'static u8)>() }]); } #[test] diff --git a/library/coretests/tests/mem/trait_info_of.rs b/library/coretests/tests/mem/trait_info_of.rs index 4fd4a013693d9..4116e038601e6 100644 --- a/library/coretests/tests/mem/trait_info_of.rs +++ b/library/coretests/tests/mem/trait_info_of.rs @@ -12,6 +12,9 @@ impl Blah for Garlic { } unsafe impl Send for Garlic {} +trait RefUnit {} +impl<'a> RefUnit for &'a () {} + #[test] fn test_implements_trait() { const { @@ -19,6 +22,14 @@ fn test_implements_trait() { assert!(TypeId::of::().trait_info_of::().is_some()); assert!(TypeId::of::<*const Box>().trait_info_of::().is_none()); assert!(TypeId::of::().trait_info_of_trait_type_id(TypeId::of::()).is_none()); + assert!( + TypeId::of:: fn(&'a ()) -> &'a ()>() + .function_ptr() + .unwrap() + .output() + .trait_info_of::() + .is_some() + ); } }