Skip to content

Commit 1e118ae

Browse files
authored
Rollup merge of rust-lang#128131 - ChrisDenton:stuff, r=workingjubilee
Import `c_void` rather than using the full path Follow up to rust-lang#128092. As requested, this imports `c_void` in more places. I also fixed up some imports to use `core` for core types instead of `crate`. While that is not strictly necessary, I think ideally things in `sys/pal` should only depend on itself or core so that the code is less spaghetti. We're far away from that ideal at the moment but I can at least try to slowly move in that direction. Also this forbids `unsafe_op_in_unsafe_fn` for library/std/src/sys/pal/windows by fixing up the remaining unsafe bits that are just punting their unsafe requirements onto the caller of the `unsafe` function (or definition macro). <!-- r? workingjubilee -->
2 parents cea2ca9 + 8456a97 commit 1e118ae

File tree

5 files changed

+35
-32
lines changed

5 files changed

+35
-32
lines changed

std/src/sys/pal/windows/alloc.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ windows_targets::link!("kernel32.dll" "system" fn GetProcessHeap() -> c::HANDLE)
3737
// Note that `dwBytes` is allowed to be zero, contrary to some other allocators.
3838
//
3939
// See https://docs.microsoft.com/windows/win32/api/heapapi/nf-heapapi-heapalloc
40-
windows_targets::link!("kernel32.dll" "system" fn HeapAlloc(hheap: c::HANDLE, dwflags: u32, dwbytes: usize) -> *mut core::ffi::c_void);
40+
windows_targets::link!("kernel32.dll" "system" fn HeapAlloc(hheap: c::HANDLE, dwflags: u32, dwbytes: usize) -> *mut c_void);
4141

4242
// Reallocate a block of memory behind a given pointer `lpMem` from a given heap `hHeap`,
4343
// to a block of at least `dwBytes` bytes, either shrinking the block in place,
@@ -61,9 +61,9 @@ windows_targets::link!("kernel32.dll" "system" fn HeapAlloc(hheap: c::HANDLE, dw
6161
windows_targets::link!("kernel32.dll" "system" fn HeapReAlloc(
6262
hheap: c::HANDLE,
6363
dwflags : u32,
64-
lpmem: *const core::ffi::c_void,
64+
lpmem: *const c_void,
6565
dwbytes: usize
66-
) -> *mut core::ffi::c_void);
66+
) -> *mut c_void);
6767

6868
// Free a block of memory behind a given pointer `lpMem` from a given heap `hHeap`.
6969
// Returns a nonzero value if the operation is successful, and zero if the operation fails.
@@ -79,7 +79,7 @@ windows_targets::link!("kernel32.dll" "system" fn HeapReAlloc(
7979
// Note that `lpMem` is allowed to be null, which will not cause the operation to fail.
8080
//
8181
// See https://docs.microsoft.com/windows/win32/api/heapapi/nf-heapapi-heapfree
82-
windows_targets::link!("kernel32.dll" "system" fn HeapFree(hheap: c::HANDLE, dwflags: u32, lpmem: *const core::ffi::c_void) -> c::BOOL);
82+
windows_targets::link!("kernel32.dll" "system" fn HeapFree(hheap: c::HANDLE, dwflags: u32, lpmem: *const c_void) -> c::BOOL);
8383

8484
// Cached handle to the default heap of the current process.
8585
// Either a non-null handle returned by `GetProcessHeap`, or null when not yet initialized or `GetProcessHeap` failed.

std/src/sys/pal/windows/c.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
#![cfg_attr(test, allow(dead_code))]
55
#![unstable(issue = "none", feature = "windows_c")]
66
#![allow(clippy::style)]
7-
#![allow(unsafe_op_in_unsafe_fn)]
87

9-
use crate::ffi::CStr;
10-
use crate::mem;
11-
use crate::os::raw::{c_uint, c_ulong, c_ushort, c_void};
12-
use crate::ptr;
8+
use core::ffi::{c_uint, c_ulong, c_ushort, c_void, CStr};
9+
use core::mem;
10+
use core::ptr;
1311

1412
pub(super) mod windows_targets;
1513

@@ -136,26 +134,26 @@ compat_fn_with_fallback! {
136134
// >= Win10 1607
137135
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreaddescription
138136
pub fn SetThreadDescription(hthread: HANDLE, lpthreaddescription: PCWSTR) -> HRESULT {
139-
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as u32); E_NOTIMPL
137+
unsafe { SetLastError(ERROR_CALL_NOT_IMPLEMENTED as u32); E_NOTIMPL }
140138
}
141139

142140
// >= Win10 1607
143141
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreaddescription
144142
pub fn GetThreadDescription(hthread: HANDLE, lpthreaddescription: *mut PWSTR) -> HRESULT {
145-
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as u32); E_NOTIMPL
143+
unsafe { SetLastError(ERROR_CALL_NOT_IMPLEMENTED as u32); E_NOTIMPL }
146144
}
147145

148146
// >= Win8 / Server 2012
149147
// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime
150148
#[cfg(target_vendor = "win7")]
151149
pub fn GetSystemTimePreciseAsFileTime(lpsystemtimeasfiletime: *mut FILETIME) -> () {
152-
GetSystemTimeAsFileTime(lpsystemtimeasfiletime)
150+
unsafe { GetSystemTimeAsFileTime(lpsystemtimeasfiletime) }
153151
}
154152

155153
// >= Win11 / Server 2022
156154
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppath2a
157155
pub fn GetTempPath2W(bufferlength: u32, buffer: PWSTR) -> u32 {
158-
GetTempPathW(bufferlength, buffer)
156+
unsafe { GetTempPathW(bufferlength, buffer) }
159157
}
160158
}
161159

@@ -188,12 +186,12 @@ extern "system" {
188186
compat_fn_optional! {
189187
crate::sys::compat::load_synch_functions();
190188
pub fn WaitOnAddress(
191-
address: *const ::core::ffi::c_void,
192-
compareaddress: *const ::core::ffi::c_void,
189+
address: *const c_void,
190+
compareaddress: *const c_void,
193191
addresssize: usize,
194192
dwmilliseconds: u32
195193
) -> BOOL;
196-
pub fn WakeByAddressSingle(address: *const ::core::ffi::c_void);
194+
pub fn WakeByAddressSingle(address: *const c_void);
197195
}
198196

199197
#[cfg(any(target_vendor = "win7", target_vendor = "uwp"))]
@@ -240,7 +238,7 @@ compat_fn_with_fallback! {
240238
shareaccess: FILE_SHARE_MODE,
241239
createdisposition: NTCREATEFILE_CREATE_DISPOSITION,
242240
createoptions: NTCREATEFILE_CREATE_OPTIONS,
243-
eabuffer: *const ::core::ffi::c_void,
241+
eabuffer: *const c_void,
244242
ealength: u32
245243
) -> NTSTATUS {
246244
STATUS_NOT_IMPLEMENTED
@@ -250,9 +248,9 @@ compat_fn_with_fallback! {
250248
filehandle: HANDLE,
251249
event: HANDLE,
252250
apcroutine: PIO_APC_ROUTINE,
253-
apccontext: *const core::ffi::c_void,
251+
apccontext: *const c_void,
254252
iostatusblock: *mut IO_STATUS_BLOCK,
255-
buffer: *mut core::ffi::c_void,
253+
buffer: *mut c_void,
256254
length: u32,
257255
byteoffset: *const i64,
258256
key: *const u32
@@ -264,9 +262,9 @@ compat_fn_with_fallback! {
264262
filehandle: HANDLE,
265263
event: HANDLE,
266264
apcroutine: PIO_APC_ROUTINE,
267-
apccontext: *const core::ffi::c_void,
265+
apccontext: *const c_void,
268266
iostatusblock: *mut IO_STATUS_BLOCK,
269-
buffer: *const core::ffi::c_void,
267+
buffer: *const c_void,
270268
length: u32,
271269
byteoffset: *const i64,
272270
key: *const u32

std/src/sys/pal/windows/compat.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,10 @@ macro_rules! compat_fn_with_fallback {
158158
static PTR: AtomicPtr<c_void> = AtomicPtr::new(load as *mut _);
159159

160160
unsafe extern "system" fn load($($argname: $argtype),*) -> $rettype {
161-
let func = load_from_module(Module::new($module));
162-
func($($argname),*)
161+
unsafe {
162+
let func = load_from_module(Module::new($module));
163+
func($($argname),*)
164+
}
163165
}
164166

165167
fn load_from_module(module: Option<Module>) -> F {
@@ -182,8 +184,10 @@ macro_rules! compat_fn_with_fallback {
182184

183185
#[inline(always)]
184186
pub unsafe fn call($($argname: $argtype),*) -> $rettype {
185-
let func: F = mem::transmute(PTR.load(Ordering::Relaxed));
186-
func($($argname),*)
187+
unsafe {
188+
let func: F = mem::transmute(PTR.load(Ordering::Relaxed));
189+
func($($argname),*)
190+
}
187191
}
188192
}
189193
#[allow(unused)]
@@ -225,7 +229,7 @@ macro_rules! compat_fn_optional {
225229
}
226230
#[inline]
227231
pub unsafe extern "system" fn $symbol($($argname: $argtype),*) $(-> $rettype)? {
228-
$symbol::option().unwrap()($($argname),*)
232+
unsafe { $symbol::option().unwrap()($($argname),*) }
229233
}
230234
)+
231235
)

std/src/sys/pal/windows/handle.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
#[cfg(test)]
44
mod tests;
55

6-
use crate::cmp;
76
use crate::io::{self, BorrowedCursor, ErrorKind, IoSlice, IoSliceMut, Read};
8-
use crate::mem;
97
use crate::os::windows::io::{
108
AsHandle, AsRawHandle, BorrowedHandle, FromRawHandle, IntoRawHandle, OwnedHandle, RawHandle,
119
};
12-
use crate::ptr;
1310
use crate::sys::c;
1411
use crate::sys::cvt;
1512
use crate::sys_common::{AsInner, FromInner, IntoInner};
13+
use core::cmp;
14+
use core::ffi::c_void;
15+
use core::mem;
16+
use core::ptr;
1617

1718
/// An owned container for `HANDLE` object, closing them on Drop.
1819
///
@@ -255,7 +256,7 @@ impl Handle {
255256
None,
256257
ptr::null_mut(),
257258
&mut io_status,
258-
buf.cast::<core::ffi::c_void>(),
259+
buf.cast::<c_void>(),
259260
len,
260261
offset.as_ref().map(|n| ptr::from_ref(n).cast::<i64>()).unwrap_or(ptr::null()),
261262
ptr::null(),
@@ -305,7 +306,7 @@ impl Handle {
305306
None,
306307
ptr::null_mut(),
307308
&mut io_status,
308-
buf.as_ptr().cast::<core::ffi::c_void>(),
309+
buf.as_ptr().cast::<c_void>(),
309310
len,
310311
offset.as_ref().map(|n| ptr::from_ref(n).cast::<i64>()).unwrap_or(ptr::null()),
311312
ptr::null(),

std/src/sys/pal/windows/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![allow(missing_docs, nonstandard_style)]
2-
#![deny(unsafe_op_in_unsafe_fn)]
2+
#![forbid(unsafe_op_in_unsafe_fn)]
33

44
use crate::ffi::{OsStr, OsString};
55
use crate::io::ErrorKind;

0 commit comments

Comments
 (0)