Skip to content

Update compiletest to Edition 2024 #139606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/tools/compiletest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "compiletest"
version = "0.0.0"
edition = "2021"
edition = "2024"

[lib]
doctest = false
Expand Down
4 changes: 3 additions & 1 deletion src/tools/compiletest/src/debuggers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ pub(crate) fn configure_gdb(config: &Config) -> Option<Arc<Config>> {
//
// we should figure out how to lift this restriction! (run them all
// on different ports allocated dynamically).
env::set_var("RUST_TEST_THREADS", "1");
//
// SAFETY: at this point we are still single-threaded.
unsafe { env::set_var("RUST_TEST_THREADS", "1") };
}

Some(Arc::new(Config { debugger: Some(Debugger::Gdb), ..config.clone() }))
Expand Down
12 changes: 8 additions & 4 deletions src/tools/compiletest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,10 +529,14 @@ pub fn run_tests(config: Arc<Config>) {
}
// Prevent issue #21352 UAC blocking .exe containing 'patch' etc. on Windows
// If #11207 is resolved (adding manifest to .exe) this becomes unnecessary
env::set_var("__COMPAT_LAYER", "RunAsInvoker");

// Let tests know which target they're running as
env::set_var("TARGET", &config.target);
//
// SAFETY: at this point we're still single-threaded.
unsafe { env::set_var("__COMPAT_LAYER", "RunAsInvoker") };

// Let tests know which target they're running as.
//
// SAFETY: at this point we're still single-threaded.
unsafe { env::set_var("TARGET", &config.target) };

let mut configs = Vec::new();
if let Mode::DebugInfo = config.mode {
Expand Down
13 changes: 9 additions & 4 deletions src/tools/compiletest/src/raise_fd_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/// This fixes issue #7772.
#[cfg(target_vendor = "apple")]
#[allow(non_camel_case_types)]
// FIXME(#139616): document caller contract.
pub unsafe fn raise_fd_limit() {
use std::ptr::null_mut;
use std::{cmp, io};
Expand All @@ -21,16 +22,19 @@ pub unsafe fn raise_fd_limit() {
let mut mib: [libc::c_int; 2] = [CTL_KERN, KERN_MAXFILESPERPROC];
let mut maxfiles: libc::c_int = 0;
let mut size: libc::size_t = size_of_val(&maxfiles) as libc::size_t;
if libc::sysctl(&mut mib[0], 2, &mut maxfiles as *mut _ as *mut _, &mut size, null_mut(), 0)
!= 0
// FIXME(#139616): justify why this is sound.
if unsafe {
libc::sysctl(&mut mib[0], 2, &mut maxfiles as *mut _ as *mut _, &mut size, null_mut(), 0)
} != 0
Comment on lines +25 to +28
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: there are some non-trivial unsafety usages, but those are pre-existing and this PR is only a mechanical transformation of adding unsafe blocks. I left FIXMEs pointing to #139616 to track the missing soundness justifications.

{
let err = io::Error::last_os_error();
panic!("raise_fd_limit: error calling sysctl: {}", err);
}

// Fetch the current resource limits
let mut rlim = libc::rlimit { rlim_cur: 0, rlim_max: 0 };
if libc::getrlimit(libc::RLIMIT_NOFILE, &mut rlim) != 0 {
// FIXME(#139616): justify why this is sound.
if unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &mut rlim) } != 0 {
let err = io::Error::last_os_error();
panic!("raise_fd_limit: error calling getrlimit: {}", err);
}
Expand All @@ -41,7 +45,8 @@ pub unsafe fn raise_fd_limit() {
rlim.rlim_cur = cmp::min(maxfiles as libc::rlim_t, rlim.rlim_max);

// Set our newly-increased resource limit.
if libc::setrlimit(libc::RLIMIT_NOFILE, &rlim) != 0 {
// FIXME(#139616): justify why this is sound.
if unsafe { libc::setrlimit(libc::RLIMIT_NOFILE, &rlim) } != 0 {
let err = io::Error::last_os_error();
panic!("raise_fd_limit: error calling setrlimit: {}", err);
}
Expand Down
28 changes: 23 additions & 5 deletions src/tools/compiletest/src/read2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ mod imp {
mut err_pipe: ChildStderr,
data: &mut dyn FnMut(bool, &mut Vec<u8>, bool),
) -> io::Result<()> {
// FIXME(#139616): justify why this is sound.
unsafe {
libc::fcntl(out_pipe.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK);
libc::fcntl(err_pipe.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK);
Expand All @@ -175,6 +176,7 @@ mod imp {
let mut out = Vec::new();
let mut err = Vec::new();

// FIXME(#139616): justify why this is sound.
let mut fds: [libc::pollfd; 2] = unsafe { mem::zeroed() };
fds[0].fd = out_pipe.as_raw_fd();
fds[0].events = libc::POLLIN;
Expand All @@ -185,6 +187,7 @@ mod imp {

while nfds > 0 {
// wait for either pipe to become readable using `select`
// FIXME(#139616): justify why this is sound.
let r = unsafe { libc::poll(fds.as_mut_ptr(), nfds, -1) };
if r == -1 {
let err = io::Error::last_os_error();
Expand Down Expand Up @@ -256,6 +259,7 @@ mod imp {
port.add_handle(0, &out_pipe)?;
port.add_handle(1, &err_pipe)?;

// FIXME(#139616): justify why this is sound.
unsafe {
let mut out_pipe = Pipe::new(out_pipe, &mut out);
let mut err_pipe = Pipe::new(err_pipe, &mut err);
Expand Down Expand Up @@ -284,18 +288,23 @@ mod imp {
}

impl<'a> Pipe<'a> {
// FIXME(#139616): document caller contract.
unsafe fn new<P: IntoRawHandle>(p: P, dst: &'a mut Vec<u8>) -> Pipe<'a> {
Pipe {
dst,
pipe: NamedPipe::from_raw_handle(p.into_raw_handle()),
// FIXME(#139616): justify why this is sound.
pipe: unsafe { NamedPipe::from_raw_handle(p.into_raw_handle()) },
overlapped: Overlapped::zero(),
done: false,
}
}

// FIXME(#139616): document caller contract.
unsafe fn read(&mut self) -> io::Result<()> {
let dst = slice_to_end(self.dst);
match self.pipe.read_overlapped(dst, self.overlapped.raw()) {
// FIXME(#139616): justify why this is sound.
let dst = unsafe { slice_to_end(self.dst) };
// FIXME(#139616): justify why this is sound.
match unsafe { self.pipe.read_overlapped(dst, self.overlapped.raw()) } {
Ok(_) => Ok(()),
Err(e) => {
if e.raw_os_error() == Some(ERROR_BROKEN_PIPE.0 as i32) {
Expand All @@ -308,22 +317,31 @@ mod imp {
}
}

// FIXME(#139616): document caller contract.
unsafe fn complete(&mut self, status: &CompletionStatus) {
let prev = self.dst.len();
self.dst.set_len(prev + status.bytes_transferred() as usize);
// FIXME(#139616): justify why this is sound.
unsafe { self.dst.set_len(prev + status.bytes_transferred() as usize) };
if status.bytes_transferred() == 0 {
self.done = true;
}
}
}

// FIXME(#139616): document caller contract.
unsafe fn slice_to_end(v: &mut Vec<u8>) -> &mut [u8] {
if v.capacity() == 0 {
v.reserve(16);
}
if v.capacity() == v.len() {
v.reserve(1);
}
slice::from_raw_parts_mut(v.as_mut_ptr().offset(v.len() as isize), v.capacity() - v.len())
// FIXME(#139616): justify why this is sound.
unsafe {
slice::from_raw_parts_mut(
v.as_mut_ptr().offset(v.len() as isize),
v.capacity() - v.len(),
)
}
}
}
Loading