From 40be1db6b89dcf027cad553e85e767aadfa75a7f Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 23 May 2025 22:02:00 +0200 Subject: [PATCH] Fix new `function_casts_as_integer` lint errors in core, std, panic_unwind and compiler crates --- compiler/rustc_driver_impl/src/signal_handler.rs | 3 ++- compiler/rustc_session/src/filesearch.rs | 5 ++++- library/core/src/intrinsics/mod.rs | 2 +- library/panic_unwind/src/seh.rs | 2 ++ library/std/src/backtrace.rs | 4 ++-- library/std/src/sys/pal/unix/stack_overflow.rs | 4 +++- library/std/src/sys/pal/windows/compat.rs | 4 ++-- 7 files changed, 16 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_driver_impl/src/signal_handler.rs b/compiler/rustc_driver_impl/src/signal_handler.rs index e7bc57c9749b..3d5df19bf67e 100644 --- a/compiler/rustc_driver_impl/src/signal_handler.rs +++ b/compiler/rustc_driver_impl/src/signal_handler.rs @@ -152,7 +152,8 @@ pub(super) fn install() { libc::sigaltstack(&alt_stack, ptr::null_mut()); let mut sa: libc::sigaction = mem::zeroed(); - sa.sa_sigaction = print_stack_trace as libc::sighandler_t; + sa.sa_sigaction = + print_stack_trace as unsafe extern "C" fn(libc::c_int) as libc::sighandler_t; sa.sa_flags = libc::SA_NODEFER | libc::SA_RESETHAND | libc::SA_ONSTACK; libc::sigemptyset(&mut sa.sa_mask); for (signum, _signame) in KILL_SIGNALS { diff --git a/compiler/rustc_session/src/filesearch.rs b/compiler/rustc_session/src/filesearch.rs index f83685b2ad85..12b294e124c8 100644 --- a/compiler/rustc_session/src/filesearch.rs +++ b/compiler/rustc_session/src/filesearch.rs @@ -151,7 +151,10 @@ fn current_dll_path() -> Result { unsafe { GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, - PCWSTR(current_dll_path as *mut u16), + PCWSTR( + current_dll_path as fn() -> Result + as *mut u16, + ), &mut module, ) } diff --git a/library/core/src/intrinsics/mod.rs b/library/core/src/intrinsics/mod.rs index 4f6a425a0983..6b13b4c4f56c 100644 --- a/library/core/src/intrinsics/mod.rs +++ b/library/core/src/intrinsics/mod.rs @@ -622,7 +622,7 @@ pub const fn forget(_: T); /// // Crucially, we `as`-cast to a raw pointer before `transmute`ing to a function pointer. /// // This avoids an integer-to-pointer `transmute`, which can be problematic. /// // Transmuting between raw pointers and function pointers (i.e., two pointer types) is fine. -/// let pointer = foo as *const (); +/// let pointer = foo as fn() -> i32 as *const (); /// let function = unsafe { /// std::mem::transmute::<*const (), fn() -> i32>(pointer) /// }; diff --git a/library/panic_unwind/src/seh.rs b/library/panic_unwind/src/seh.rs index a5d67dbb6a9f..257916c4d5cd 100644 --- a/library/panic_unwind/src/seh.rs +++ b/library/panic_unwind/src/seh.rs @@ -336,6 +336,7 @@ unsafe fn throw_exception(data: Option>) -> ! { // In any case, we basically need to do something like this until we can // express more operations in statics (and we may never be able to). unsafe { + #[allow(function_casts_as_integer)] atomic_store::<_, { AtomicOrdering::SeqCst }>( (&raw mut THROW_INFO.pmfnUnwind).cast(), ptr_t::new(exception_cleanup as *mut u8).raw(), @@ -352,6 +353,7 @@ unsafe fn throw_exception(data: Option>) -> ! { (&raw mut CATCHABLE_TYPE.pType).cast(), ptr_t::new((&raw mut TYPE_DESCRIPTOR).cast()).raw(), ); + #[allow(function_casts_as_integer)] atomic_store::<_, { AtomicOrdering::SeqCst }>( (&raw mut CATCHABLE_TYPE.copyFunction).cast(), ptr_t::new(exception_copy as *mut u8).raw(), diff --git a/library/std/src/backtrace.rs b/library/std/src/backtrace.rs index c3fcb0e2e42b..99724e29e02b 100644 --- a/library/std/src/backtrace.rs +++ b/library/std/src/backtrace.rs @@ -293,7 +293,7 @@ impl Backtrace { if !Backtrace::enabled() { return Backtrace { inner: Inner::Disabled }; } - Backtrace::create(Backtrace::capture as usize) + Backtrace::create(Backtrace::capture as fn() -> Backtrace as usize) } /// Forcibly captures a full backtrace, regardless of environment variable @@ -309,7 +309,7 @@ impl Backtrace { #[stable(feature = "backtrace", since = "1.65.0")] #[inline(never)] // want to make sure there's a frame here to remove pub fn force_capture() -> Backtrace { - Backtrace::create(Backtrace::force_capture as usize) + Backtrace::create(Backtrace::force_capture as fn() -> Backtrace as usize) } /// Forcibly captures a disabled backtrace, regardless of environment diff --git a/library/std/src/sys/pal/unix/stack_overflow.rs b/library/std/src/sys/pal/unix/stack_overflow.rs index 062f031189fc..226b6bce01b5 100644 --- a/library/std/src/sys/pal/unix/stack_overflow.rs +++ b/library/std/src/sys/pal/unix/stack_overflow.rs @@ -174,7 +174,9 @@ mod imp { } action.sa_flags = SA_SIGINFO | SA_ONSTACK; - action.sa_sigaction = signal_handler as sighandler_t; + action.sa_sigaction = signal_handler + as unsafe extern "C" fn(i32, *mut libc::siginfo_t, *mut libc::c_void) + as sighandler_t; // SAFETY: only overriding signals if the default is set unsafe { sigaction(signal, &action, ptr::null_mut()) }; } diff --git a/library/std/src/sys/pal/windows/compat.rs b/library/std/src/sys/pal/windows/compat.rs index 14f2c8d881cf..c465ceb2301c 100644 --- a/library/std/src/sys/pal/windows/compat.rs +++ b/library/std/src/sys/pal/windows/compat.rs @@ -155,7 +155,7 @@ macro_rules! compat_fn_with_fallback { /// When that is called it attempts to load the requested symbol. /// If it succeeds, `PTR` is set to the address of that symbol. /// If it fails, then `PTR` is set to `fallback`. - static PTR: Atomic<*mut c_void> = AtomicPtr::new(load as *mut _); + static PTR: Atomic<*mut c_void> = AtomicPtr::new(load as unsafe extern "system" fn($($argname: $argtype),*) -> $rettype as *mut _); unsafe extern "system" fn load($($argname: $argtype),*) -> $rettype { unsafe { @@ -171,7 +171,7 @@ macro_rules! compat_fn_with_fallback { PTR.store(f.as_ptr(), Ordering::Relaxed); mem::transmute(f) } else { - PTR.store(fallback as *mut _, Ordering::Relaxed); + PTR.store(fallback as unsafe extern "system" fn($($argname: $argtype),*) -> $rettype as *mut _, Ordering::Relaxed); fallback } }