Skip to content

Commit cf9de23

Browse files
committed
Auto merge of rust-lang#126205 - jieyouxu:rollup-s64z5ng, r=jieyouxu
Rollup of 4 pull requests Successful merges: - rust-lang#126172 (Weekly `cargo update`) - rust-lang#126176 (rustdoc-search: use lowercase, non-normalized name for type search) - rust-lang#126190 (Autolabel run-make tests, remind to update tracking issue) - rust-lang#126194 (Migrate more things to `WinError`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents c4bd74c + c5da756 commit cf9de23

File tree

7 files changed

+69
-30
lines changed

7 files changed

+69
-30
lines changed

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

+36
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,39 @@ pub fn get_last_error() -> WinError {
251251
pub struct WinError {
252252
pub code: u32,
253253
}
254+
impl WinError {
255+
const fn new(code: u32) -> Self {
256+
Self { code }
257+
}
258+
}
259+
260+
// Error code constants.
261+
// The constant names should be the same as the winapi constants except for the leading `ERROR_`.
262+
// Due to the sheer number of codes, error codes should only be added here on an as-needed basis.
263+
// However, they should never be removed as the assumption is they may be useful again in the future.
264+
#[allow(unused)]
265+
impl WinError {
266+
/// Success is not an error.
267+
/// Some Windows APIs do use this to distinguish between a zero return and an error return
268+
/// but we should never return this to users as an error.
269+
pub const SUCCESS: Self = Self::new(c::ERROR_SUCCESS);
270+
// tidy-alphabetical-start
271+
pub const ACCESS_DENIED: Self = Self::new(c::ERROR_ACCESS_DENIED);
272+
pub const ALREADY_EXISTS: Self = Self::new(c::ERROR_ALREADY_EXISTS);
273+
pub const CANT_ACCESS_FILE: Self = Self::new(c::ERROR_CANT_ACCESS_FILE);
274+
pub const DELETE_PENDING: Self = Self::new(c::ERROR_DELETE_PENDING);
275+
pub const DIRECTORY: Self = Self::new(c::ERROR_DIRECTORY);
276+
pub const FILE_NOT_FOUND: Self = Self::new(c::ERROR_FILE_NOT_FOUND);
277+
pub const INSUFFICIENT_BUFFER: Self = Self::new(c::ERROR_INSUFFICIENT_BUFFER);
278+
pub const INVALID_FUNCTION: Self = Self::new(c::ERROR_INVALID_FUNCTION);
279+
pub const INVALID_HANDLE: Self = Self::new(c::ERROR_INVALID_HANDLE);
280+
pub const INVALID_PARAMETER: Self = Self::new(c::ERROR_INVALID_PARAMETER);
281+
pub const NO_MORE_FILES: Self = Self::new(c::ERROR_NO_MORE_FILES);
282+
pub const NOT_FOUND: Self = Self::new(c::ERROR_NOT_FOUND);
283+
pub const NOT_SUPPORTED: Self = Self::new(c::ERROR_NOT_SUPPORTED);
284+
pub const OPERATION_ABORTED: Self = Self::new(c::ERROR_OPERATION_ABORTED);
285+
pub const PATH_NOT_FOUND: Self = Self::new(c::ERROR_PATH_NOT_FOUND);
286+
pub const SHARING_VIOLATION: Self = Self::new(c::ERROR_SHARING_VIOLATION);
287+
pub const TIMEOUT: Self = Self::new(c::ERROR_TIMEOUT);
288+
// tidy-alphabetical-end
289+
}

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

+15-15
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use crate::sys::{c, cvt, Align8};
1818
use crate::sys_common::{AsInner, FromInner, IntoInner};
1919
use crate::thread;
2020

21-
use super::{api, to_u16s, IoResult};
21+
use super::api::{self, WinError};
22+
use super::{to_u16s, IoResult};
2223
use crate::sys::path::maybe_verbatim;
2324

2425
pub struct File {
@@ -130,10 +131,11 @@ impl Iterator for ReadDir {
130131
let mut wfd = mem::zeroed();
131132
loop {
132133
if c::FindNextFileW(self.handle.0, &mut wfd) == 0 {
133-
if api::get_last_error().code == c::ERROR_NO_MORE_FILES {
134-
return None;
135-
} else {
136-
return Some(Err(Error::last_os_error()));
134+
match api::get_last_error() {
135+
WinError::NO_MORE_FILES => return None,
136+
WinError { code } => {
137+
return Some(Err(Error::from_raw_os_error(code as i32)));
138+
}
137139
}
138140
}
139141
if let Some(e) = DirEntry::new(&self.root, &wfd) {
@@ -244,8 +246,6 @@ impl OpenOptions {
244246
}
245247

246248
fn get_access_mode(&self) -> io::Result<c::DWORD> {
247-
const ERROR_INVALID_PARAMETER: i32 = 87;
248-
249249
match (self.read, self.write, self.append, self.access_mode) {
250250
(.., Some(mode)) => Ok(mode),
251251
(true, false, false, None) => Ok(c::GENERIC_READ),
@@ -255,23 +255,23 @@ impl OpenOptions {
255255
(true, _, true, None) => {
256256
Ok(c::GENERIC_READ | (c::FILE_GENERIC_WRITE & !c::FILE_WRITE_DATA))
257257
}
258-
(false, false, false, None) => Err(Error::from_raw_os_error(ERROR_INVALID_PARAMETER)),
258+
(false, false, false, None) => {
259+
Err(Error::from_raw_os_error(c::ERROR_INVALID_PARAMETER as i32))
260+
}
259261
}
260262
}
261263

262264
fn get_creation_mode(&self) -> io::Result<c::DWORD> {
263-
const ERROR_INVALID_PARAMETER: i32 = 87;
264-
265265
match (self.write, self.append) {
266266
(true, false) => {}
267267
(false, false) => {
268268
if self.truncate || self.create || self.create_new {
269-
return Err(Error::from_raw_os_error(ERROR_INVALID_PARAMETER));
269+
return Err(Error::from_raw_os_error(c::ERROR_INVALID_PARAMETER as i32));
270270
}
271271
}
272272
(_, true) => {
273273
if self.truncate && !self.create_new {
274-
return Err(Error::from_raw_os_error(ERROR_INVALID_PARAMETER));
274+
return Err(Error::from_raw_os_error(c::ERROR_INVALID_PARAMETER as i32));
275275
}
276276
}
277277
}
@@ -315,7 +315,7 @@ impl File {
315315
// Manual truncation. See #115745.
316316
if opts.truncate
317317
&& creation == c::OPEN_ALWAYS
318-
&& unsafe { c::GetLastError() } == c::ERROR_ALREADY_EXISTS
318+
&& api::get_last_error() == WinError::ALREADY_EXISTS
319319
{
320320
unsafe {
321321
// This originally used `FileAllocationInfo` instead of
@@ -845,7 +845,7 @@ fn open_link_no_reparse(parent: &File, name: &[u16], access: u32) -> io::Result<
845845
// We make a special exception for `STATUS_DELETE_PENDING` because
846846
// otherwise this will be mapped to `ERROR_ACCESS_DENIED` which is
847847
// very unhelpful.
848-
Err(io::Error::from_raw_os_error(c::ERROR_DELETE_PENDING as _))
848+
Err(io::Error::from_raw_os_error(c::ERROR_DELETE_PENDING as i32))
849849
} else if status == c::STATUS_INVALID_PARAMETER
850850
&& ATTRIBUTES.load(Ordering::Relaxed) == c::OBJ_DONT_REPARSE
851851
{
@@ -1097,7 +1097,7 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
10971097
//
10981098
// See issue #120040: https://github.com/rust-lang/rust/issues/120040.
10991099
let last_error = api::get_last_error();
1100-
if last_error.code == c::ERROR_FILE_NOT_FOUND {
1100+
if last_error == WinError::FILE_NOT_FOUND {
11011101
return Ok(ReadDir {
11021102
handle: FindNextFileHandle(find_handle),
11031103
root: Arc::new(root),

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::api;
1+
use super::api::{self, WinError};
22
use crate::sys::c;
33
use crate::sys::dur2timeout;
44
use core::ffi::c_void;
@@ -72,7 +72,7 @@ pub fn wake_by_address_all<T>(address: &T) {
7272

7373
pub fn futex_wait<W: Waitable>(futex: &W::Atomic, expected: W, timeout: Option<Duration>) -> bool {
7474
// return false only on timeout
75-
wait_on_address(futex, expected, timeout) || api::get_last_error().code != c::ERROR_TIMEOUT
75+
wait_on_address(futex, expected, timeout) || api::get_last_error() != WinError::TIMEOUT
7676
}
7777

7878
pub fn futex_wake<T>(futex: &T) -> bool {

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use crate::ptr;
1717
use crate::slice;
1818
use crate::sys::{c, cvt};
1919

20-
use super::{api, to_u16s};
20+
use super::api::{self, WinError};
21+
use super::to_u16s;
2122

2223
pub fn errno() -> i32 {
2324
api::get_last_error().code as i32
@@ -333,7 +334,7 @@ fn home_dir_crt() -> Option<PathBuf> {
333334
buf,
334335
&mut sz,
335336
) {
336-
0 if api::get_last_error().code != c::ERROR_INSUFFICIENT_BUFFER => 0,
337+
0 if api::get_last_error() != WinError::INSUFFICIENT_BUFFER => 0,
337338
0 => sz,
338339
_ => sz - 1, // sz includes the null terminator
339340
}
@@ -358,7 +359,7 @@ fn home_dir_crt() -> Option<PathBuf> {
358359
super::fill_utf16_buf(
359360
|buf, mut sz| {
360361
match c::GetUserProfileDirectoryW(token, buf, &mut sz) {
361-
0 if api::get_last_error().code != c::ERROR_INSUFFICIENT_BUFFER => 0,
362+
0 if api::get_last_error() != WinError::INSUFFICIENT_BUFFER => 0,
362363
0 => sz,
363364
_ => sz - 1, // sz includes the null terminator
364365
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::sys::c;
1212
use crate::sys::fs::{File, OpenOptions};
1313
use crate::sys::handle::Handle;
1414
use crate::sys::hashmap_random_keys;
15+
use crate::sys::pal::windows::api::{self, WinError};
1516
use crate::sys_common::{FromInner, IntoInner};
1617

1718
////////////////////////////////////////////////////////////////////////////////
@@ -124,20 +125,19 @@ pub fn anon_pipe(ours_readable: bool, their_handle_inheritable: bool) -> io::Res
124125
// testing strategy
125126
// For more info, see https://github.com/rust-lang/rust/pull/37677.
126127
if handle == c::INVALID_HANDLE_VALUE {
127-
let err = io::Error::last_os_error();
128-
let raw_os_err = err.raw_os_error();
128+
let error = api::get_last_error();
129129
if tries < 10 {
130-
if raw_os_err == Some(c::ERROR_ACCESS_DENIED as i32) {
130+
if error == WinError::ACCESS_DENIED {
131131
continue;
132132
} else if reject_remote_clients_flag != 0
133-
&& raw_os_err == Some(c::ERROR_INVALID_PARAMETER as i32)
133+
&& error == WinError::INVALID_PARAMETER
134134
{
135135
reject_remote_clients_flag = 0;
136136
tries -= 1;
137137
continue;
138138
}
139139
}
140-
return Err(err);
140+
return Err(io::Error::from_raw_os_error(error.code as i32));
141141
}
142142
ours = Handle::from_raw_handle(handle);
143143
break;

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ use crate::sys_common::IntoInner;
3131

3232
use core::ffi::c_void;
3333

34+
use super::api::{self, WinError};
35+
3436
////////////////////////////////////////////////////////////////////////////////
3537
// Command
3638
////////////////////////////////////////////////////////////////////////////////
@@ -645,12 +647,12 @@ impl Process {
645647
pub fn kill(&mut self) -> io::Result<()> {
646648
let result = unsafe { c::TerminateProcess(self.handle.as_raw_handle(), 1) };
647649
if result == c::FALSE {
648-
let error = unsafe { c::GetLastError() };
650+
let error = api::get_last_error();
649651
// TerminateProcess returns ERROR_ACCESS_DENIED if the process has already been
650652
// terminated (by us, or for any other reason). So check if the process was actually
651653
// terminated, and if so, do not return an error.
652-
if error != c::ERROR_ACCESS_DENIED || self.try_wait().is_err() {
653-
return Err(crate::io::Error::from_raw_os_error(error as i32));
654+
if error != WinError::ACCESS_DENIED || self.try_wait().is_err() {
655+
return Err(crate::io::Error::from_raw_os_error(error.code as i32));
654656
}
655657
}
656658
Ok(())

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![unstable(issue = "none", feature = "windows_stdio")]
22

3-
use super::api;
3+
use super::api::{self, WinError};
44
use crate::cmp;
55
use crate::io;
66
use crate::mem::MaybeUninit;
@@ -370,7 +370,7 @@ fn read_u16s(handle: c::HANDLE, buf: &mut [MaybeUninit<u16>]) -> io::Result<usiz
370370

371371
// ReadConsoleW returns success with ERROR_OPERATION_ABORTED for Ctrl-C or Ctrl-Break.
372372
// Explicitly check for that case here and try again.
373-
if amount == 0 && api::get_last_error().code == c::ERROR_OPERATION_ABORTED {
373+
if amount == 0 && api::get_last_error() == WinError::OPERATION_ABORTED {
374374
continue;
375375
}
376376
break;

0 commit comments

Comments
 (0)