Skip to content

Commit 44f5180

Browse files
committed
Auto merge of #106610 - euclio:windows-rs, r=ChrisDenton
migrate compiler, bootstrap and compiletest to windows-rs This PR migrates the compiler, bootstrap, and compiletest to use [windows-rs](https://github.com/microsoft/windows-rs) instead of winapi-rs. windows-rs is the bindings crate provided by Microsoft, and is actively maintained compared to winapi-rs. Not all ecosystem crates have migrated over yet, so there will be a period of time where both crates are used. windows-rs also provides some nice ergonomics over winapi-rs to convert return values to `Result`s (which found a case where we forgot to check the return value of `CreateFileW`).
2 parents febd59e + bb7c373 commit 44f5180

File tree

22 files changed

+379
-280
lines changed

22 files changed

+379
-280
lines changed

Cargo.lock

+31-22
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ dependencies = [
898898
"tracing-subscriber",
899899
"unified-diff",
900900
"walkdir",
901-
"winapi",
901+
"windows 0.46.0",
902902
]
903903

904904
[[package]]
@@ -2273,7 +2273,7 @@ dependencies = [
22732273
"dirs",
22742274
"gix-path",
22752275
"libc",
2276-
"windows",
2276+
"windows 0.43.0",
22772277
]
22782278

22792279
[[package]]
@@ -4529,7 +4529,7 @@ dependencies = [
45294529
"tempfile",
45304530
"thin-vec",
45314531
"tracing",
4532-
"winapi",
4532+
"windows 0.46.0",
45334533
]
45344534

45354535
[[package]]
@@ -4588,7 +4588,7 @@ dependencies = [
45884588
"rustc_ty_utils",
45894589
"serde_json",
45904590
"tracing",
4591-
"winapi",
4591+
"windows 0.46.0",
45924592
]
45934593

45944594
[[package]]
@@ -4636,7 +4636,7 @@ dependencies = [
46364636
"termize",
46374637
"tracing",
46384638
"unicode-width",
4639-
"winapi",
4639+
"windows 0.46.0",
46404640
]
46414641

46424642
[[package]]
@@ -5277,7 +5277,7 @@ dependencies = [
52775277
"smallvec",
52785278
"termize",
52795279
"tracing",
5280-
"winapi",
5280+
"windows 0.46.0",
52815281
]
52825282

52835283
[[package]]
@@ -6908,6 +6908,15 @@ dependencies = [
69086908
"windows_x86_64_msvc",
69096909
]
69106910

6911+
[[package]]
6912+
name = "windows"
6913+
version = "0.46.0"
6914+
source = "registry+https://github.com/rust-lang/crates.io-index"
6915+
checksum = "cdacb41e6a96a052c6cb63a144f24900236121c6f63f4f8219fef5977ecb0c25"
6916+
dependencies = [
6917+
"windows-targets",
6918+
]
6919+
69116920
[[package]]
69126921
name = "windows-sys"
69136922
version = "0.42.0"
@@ -6934,9 +6943,9 @@ dependencies = [
69346943

69356944
[[package]]
69366945
name = "windows-targets"
6937-
version = "0.42.1"
6946+
version = "0.42.2"
69386947
source = "registry+https://github.com/rust-lang/crates.io-index"
6939-
checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7"
6948+
checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071"
69406949
dependencies = [
69416950
"windows_aarch64_gnullvm",
69426951
"windows_aarch64_msvc",
@@ -6949,45 +6958,45 @@ dependencies = [
69496958

69506959
[[package]]
69516960
name = "windows_aarch64_gnullvm"
6952-
version = "0.42.1"
6961+
version = "0.42.2"
69536962
source = "registry+https://github.com/rust-lang/crates.io-index"
6954-
checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608"
6963+
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
69556964

69566965
[[package]]
69576966
name = "windows_aarch64_msvc"
6958-
version = "0.42.1"
6967+
version = "0.42.2"
69596968
source = "registry+https://github.com/rust-lang/crates.io-index"
6960-
checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7"
6969+
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
69616970

69626971
[[package]]
69636972
name = "windows_i686_gnu"
6964-
version = "0.42.1"
6973+
version = "0.42.2"
69656974
source = "registry+https://github.com/rust-lang/crates.io-index"
6966-
checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640"
6975+
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
69676976

69686977
[[package]]
69696978
name = "windows_i686_msvc"
6970-
version = "0.42.1"
6979+
version = "0.42.2"
69716980
source = "registry+https://github.com/rust-lang/crates.io-index"
6972-
checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605"
6981+
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
69736982

69746983
[[package]]
69756984
name = "windows_x86_64_gnu"
6976-
version = "0.42.1"
6985+
version = "0.42.2"
69776986
source = "registry+https://github.com/rust-lang/crates.io-index"
6978-
checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45"
6987+
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
69796988

69806989
[[package]]
69816990
name = "windows_x86_64_gnullvm"
6982-
version = "0.42.1"
6991+
version = "0.42.2"
69836992
source = "registry+https://github.com/rust-lang/crates.io-index"
6984-
checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463"
6993+
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
69856994

69866995
[[package]]
69876996
name = "windows_x86_64_msvc"
6988-
version = "0.42.1"
6997+
version = "0.42.2"
69896998
source = "registry+https://github.com/rust-lang/crates.io-index"
6990-
checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd"
6999+
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
69917000

69927001
[[package]]
69937002
name = "writeable"

compiler/rustc_data_structures/Cargo.toml

+9-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,15 @@ elsa = "1.8"
3636
[dependencies.parking_lot]
3737
version = "0.11"
3838

39-
[target.'cfg(windows)'.dependencies]
40-
winapi = { version = "0.3", features = ["fileapi", "psapi", "winerror"] }
39+
[target.'cfg(windows)'.dependencies.windows]
40+
version = "0.46.0"
41+
features = [
42+
"Win32_Foundation",
43+
"Win32_Storage_FileSystem",
44+
"Win32_System_IO",
45+
"Win32_System_ProcessStatus",
46+
"Win32_System_Threading",
47+
]
4148

4249
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
4350
memmap2 = "0.2.1"

compiler/rustc_data_structures/src/flock.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
//! green/native threading. This is just a bare-bones enough solution for
55
//! librustdoc, it is not production quality at all.
66
7-
#![allow(non_camel_case_types)]
8-
#![allow(nonstandard_style)]
9-
107
cfg_if! {
118
if #[cfg(target_os = "linux")] {
129
mod linux;
@@ -16,7 +13,7 @@ cfg_if! {
1613
use unix as imp;
1714
} else if #[cfg(windows)] {
1815
mod windows;
19-
use windows as imp;
16+
use self::windows as imp;
2017
} else {
2118
mod unsupported;
2219
use unsupported as imp;

compiler/rustc_data_structures/src/flock/windows.rs

+38-26
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
use std::fs::{File, OpenOptions};
22
use std::io;
3-
use std::mem;
43
use std::os::windows::prelude::*;
54
use std::path::Path;
65

7-
use winapi::shared::winerror::ERROR_INVALID_FUNCTION;
8-
use winapi::um::fileapi::LockFileEx;
9-
use winapi::um::minwinbase::{LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY, OVERLAPPED};
10-
use winapi::um::winnt::{FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE};
6+
use windows::{
7+
Win32::Foundation::{ERROR_INVALID_FUNCTION, HANDLE},
8+
Win32::Storage::FileSystem::{
9+
LockFileEx, FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE, LOCKFILE_EXCLUSIVE_LOCK,
10+
LOCKFILE_FAIL_IMMEDIATELY, LOCK_FILE_FLAGS,
11+
},
12+
Win32::System::IO::OVERLAPPED,
13+
};
1114

1215
#[derive(Debug)]
1316
pub struct Lock {
@@ -25,7 +28,7 @@ impl Lock {
2528
let share_mode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
2629

2730
let mut open_options = OpenOptions::new();
28-
open_options.read(true).share_mode(share_mode);
31+
open_options.read(true).share_mode(share_mode.0);
2932

3033
if create {
3134
open_options.create(true).write(true);
@@ -43,33 +46,42 @@ impl Lock {
4346
}
4447
};
4548

46-
let ret = unsafe {
47-
let mut overlapped: OVERLAPPED = mem::zeroed();
49+
let mut flags = LOCK_FILE_FLAGS::default();
50+
if !wait {
51+
flags |= LOCKFILE_FAIL_IMMEDIATELY;
52+
}
4853

49-
let mut dwFlags = 0;
50-
if !wait {
51-
dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
52-
}
54+
if exclusive {
55+
flags |= LOCKFILE_EXCLUSIVE_LOCK;
56+
}
5357

54-
if exclusive {
55-
dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
56-
}
58+
let mut overlapped = OVERLAPPED::default();
5759

58-
debug!("attempting to acquire lock on lock file `{}`", p.display());
59-
LockFileEx(file.as_raw_handle(), dwFlags, 0, 0xFFFF_FFFF, 0xFFFF_FFFF, &mut overlapped)
60-
};
61-
if ret == 0 {
62-
let err = io::Error::last_os_error();
63-
debug!("failed acquiring file lock: {}", err);
64-
Err(err)
65-
} else {
66-
debug!("successfully acquired lock");
67-
Ok(Lock { _file: file })
60+
debug!("attempting to acquire lock on lock file `{}`", p.display());
61+
62+
unsafe {
63+
LockFileEx(
64+
HANDLE(file.as_raw_handle() as isize),
65+
flags,
66+
0,
67+
u32::MAX,
68+
u32::MAX,
69+
&mut overlapped,
70+
)
6871
}
72+
.ok()
73+
.map_err(|e| {
74+
let err = io::Error::from_raw_os_error(e.code().0);
75+
debug!("failed acquiring file lock: {}", err);
76+
err
77+
})?;
78+
79+
debug!("successfully acquired lock");
80+
Ok(Lock { _file: file })
6981
}
7082

7183
pub fn error_unsupported(err: &io::Error) -> bool {
72-
err.raw_os_error() == Some(ERROR_INVALID_FUNCTION as i32)
84+
err.raw_os_error() == Some(ERROR_INVALID_FUNCTION.0 as i32)
7385
}
7486
}
7587

compiler/rustc_data_structures/src/profiling.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -796,21 +796,26 @@ fn get_thread_id() -> u32 {
796796
cfg_if! {
797797
if #[cfg(windows)] {
798798
pub fn get_resident_set_size() -> Option<usize> {
799-
use std::mem::{self, MaybeUninit};
800-
use winapi::shared::minwindef::DWORD;
801-
use winapi::um::processthreadsapi::GetCurrentProcess;
802-
use winapi::um::psapi::{GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS};
803-
804-
let mut pmc = MaybeUninit::<PROCESS_MEMORY_COUNTERS>::uninit();
805-
match unsafe {
806-
GetProcessMemoryInfo(GetCurrentProcess(), pmc.as_mut_ptr(), mem::size_of_val(&pmc) as DWORD)
807-
} {
808-
0 => None,
809-
_ => {
810-
let pmc = unsafe { pmc.assume_init() };
811-
Some(pmc.WorkingSetSize as usize)
812-
}
799+
use std::mem;
800+
801+
use windows::{
802+
Win32::System::ProcessStatus::{K32GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS},
803+
Win32::System::Threading::GetCurrentProcess,
804+
};
805+
806+
let mut pmc = PROCESS_MEMORY_COUNTERS::default();
807+
let pmc_size = mem::size_of_val(&pmc);
808+
unsafe {
809+
K32GetProcessMemoryInfo(
810+
GetCurrentProcess(),
811+
&mut pmc,
812+
pmc_size as u32,
813+
)
813814
}
815+
.ok()
816+
.ok()?;
817+
818+
Some(pmc.WorkingSetSize)
814819
}
815820
} else if #[cfg(target_os = "macos")] {
816821
pub fn get_resident_set_size() -> Option<usize> {

compiler/rustc_driver_impl/Cargo.toml

+5-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ rustc_hir_analysis = { path = "../rustc_hir_analysis" }
5454
[target.'cfg(unix)'.dependencies]
5555
libc = "0.2"
5656

57-
[target.'cfg(windows)'.dependencies]
58-
winapi = { version = "0.3", features = ["consoleapi", "debugapi", "processenv"] }
57+
[target.'cfg(windows)'.dependencies.windows]
58+
version = "0.46.0"
59+
features = [
60+
"Win32_System_Diagnostics_Debug",
61+
]
5962

6063
[features]
6164
llvm = ['rustc_interface/llvm']

compiler/rustc_driver_impl/src/lib.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1246,11 +1246,9 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
12461246
interface::try_print_query_stack(&handler, num_frames);
12471247

12481248
#[cfg(windows)]
1249-
unsafe {
1250-
if env::var("RUSTC_BREAK_ON_ICE").is_ok() {
1251-
// Trigger a debugger if we crashed during bootstrap
1252-
winapi::um::debugapi::DebugBreak();
1253-
}
1249+
if env::var("RUSTC_BREAK_ON_ICE").is_ok() {
1250+
// Trigger a debugger if we crashed during bootstrap
1251+
unsafe { windows::Win32::System::Diagnostics::Debug::DebugBreak() };
12541252
}
12551253
}
12561254

compiler/rustc_errors/Cargo.toml

+8-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ termize = "0.1.1"
2525
serde = { version = "1.0.125", features = [ "derive" ] }
2626
serde_json = "1.0.59"
2727

28-
[target.'cfg(windows)'.dependencies]
29-
winapi = { version = "0.3", features = [ "handleapi", "synchapi", "winbase" ] }
28+
[target.'cfg(windows)'.dependencies.windows]
29+
version = "0.46.0"
30+
features = [
31+
"Win32_Foundation",
32+
"Win32_Security",
33+
"Win32_System_Threading",
34+
"Win32_System_WindowsProgramming",
35+
]
3036

3137
[features]
3238
rustc_use_parallel_compiler = ['rustc_error_messages/rustc_use_parallel_compiler']

0 commit comments

Comments
 (0)