From 1567d4d8504ed7bb57e77063fbc10a2d2f56c57a Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Mon, 15 Apr 2024 23:46:27 -0400 Subject: [PATCH 1/2] Remove libc from more tests --- tests/ui/abi/segfault-no-out-of-stack.rs | 14 ++++----- .../unix_sigpipe-and-child-processes.rs | 2 -- .../ui/extern-flag/auxiliary/panic_handler.rs | 7 ----- .../rfc-1014-2.rs | 29 +++++++++---------- .../rfc-1014.rs | 23 +++++++-------- 5 files changed, 30 insertions(+), 45 deletions(-) diff --git a/tests/ui/abi/segfault-no-out-of-stack.rs b/tests/ui/abi/segfault-no-out-of-stack.rs index fb1f8303f9ffd..113c82c30e927 100644 --- a/tests/ui/abi/segfault-no-out-of-stack.rs +++ b/tests/ui/abi/segfault-no-out-of-stack.rs @@ -1,31 +1,29 @@ //@ run-pass - -#![allow(unused_imports)] //@ ignore-wasm32 can't run commands //@ ignore-sgx no processes //@ ignore-fuchsia must translate zircon signal to SIGSEGV/SIGBUS, FIXME (#58590) -#![feature(rustc_private)] -extern crate libc; +#![feature(rustc_private)] use std::env; +use std::ffi::c_char; use std::process::{Command, ExitStatus}; #[link(name = "rust_test_helpers", kind = "static")] extern "C" { - fn rust_get_null_ptr() -> *mut ::libc::c_char; + fn rust_get_null_ptr() -> *mut c_char; } #[cfg(unix)] -fn check_status(status: std::process::ExitStatus) { - use libc; +fn check_status(status: ExitStatus) { + extern crate libc; use std::os::unix::process::ExitStatusExt; assert!(status.signal() == Some(libc::SIGSEGV) || status.signal() == Some(libc::SIGBUS)); } #[cfg(not(unix))] -fn check_status(status: std::process::ExitStatus) { +fn check_status(status: ExitStatus) { assert!(!status.success()); } diff --git a/tests/ui/attributes/unix_sigpipe/unix_sigpipe-and-child-processes.rs b/tests/ui/attributes/unix_sigpipe/unix_sigpipe-and-child-processes.rs index f96bd634876fd..9d1bd9f9607ec 100644 --- a/tests/ui/attributes/unix_sigpipe/unix_sigpipe-and-child-processes.rs +++ b/tests/ui/attributes/unix_sigpipe/unix_sigpipe-and-child-processes.rs @@ -11,10 +11,8 @@ // processes with and without the attribute. Search for // `unix_sigpipe_attr_specified()` in the code base to learn more. -#![feature(rustc_private)] #![cfg_attr(any(sig_dfl, sig_ign, inherit), feature(unix_sigpipe))] -extern crate libc; extern crate sigpipe_utils; use sigpipe_utils::*; diff --git a/tests/ui/extern-flag/auxiliary/panic_handler.rs b/tests/ui/extern-flag/auxiliary/panic_handler.rs index a625761a838a8..9953b783c62db 100644 --- a/tests/ui/extern-flag/auxiliary/panic_handler.rs +++ b/tests/ui/extern-flag/auxiliary/panic_handler.rs @@ -1,13 +1,6 @@ #![feature(lang_items)] #![no_std] -// Since `rustc` generally passes `-nodefaultlibs` to the linker, -// Rust programs link necessary system libraries via `#[link()]` -// attributes in the `libc` crate. `libc` is a dependency of `std`, -// but as we are `#![no_std]`, we need to include it manually. -#![feature(rustc_private)] -extern crate libc; - #[panic_handler] pub fn begin_panic_handler(_info: &core::panic::PanicInfo<'_>) -> ! { loop {} diff --git a/tests/ui/rfcs/rfc-1014-stdout-existential-crisis/rfc-1014-2.rs b/tests/ui/rfcs/rfc-1014-stdout-existential-crisis/rfc-1014-2.rs index 4944de0b82f33..eb512bb40aa37 100644 --- a/tests/ui/rfcs/rfc-1014-stdout-existential-crisis/rfc-1014-2.rs +++ b/tests/ui/rfcs/rfc-1014-stdout-existential-crisis/rfc-1014-2.rs @@ -1,31 +1,28 @@ +// Test that println! to a closed stdout does not panic. +// On Windows, close via SetStdHandle to 0. //@ run-pass -#![allow(dead_code)] #![feature(rustc_private)] -extern crate libc; - -type DWORD = u32; -type HANDLE = *mut u8; -type BOOL = i32; - -#[cfg(windows)] -extern "system" { - fn SetStdHandle(nStdHandle: DWORD, nHandle: HANDLE) -> BOOL; -} - #[cfg(windows)] fn close_stdout() { + type DWORD = u32; + type HANDLE = *mut u8; + type BOOL = i32; + + extern "system" { + fn SetStdHandle(nStdHandle: DWORD, nHandle: HANDLE) -> BOOL; + } + const STD_OUTPUT_HANDLE: DWORD = -11i32 as DWORD; unsafe { SetStdHandle(STD_OUTPUT_HANDLE, 0 as HANDLE); } } -#[cfg(windows)] +#[cfg(not(windows))] +fn close_stdout() {} + fn main() { close_stdout(); println!("hello"); println!("world"); } - -#[cfg(not(windows))] -fn main() {} diff --git a/tests/ui/rfcs/rfc-1014-stdout-existential-crisis/rfc-1014.rs b/tests/ui/rfcs/rfc-1014-stdout-existential-crisis/rfc-1014.rs index cc2d9bad02983..950c4dee329ff 100644 --- a/tests/ui/rfcs/rfc-1014-stdout-existential-crisis/rfc-1014.rs +++ b/tests/ui/rfcs/rfc-1014-stdout-existential-crisis/rfc-1014.rs @@ -1,28 +1,27 @@ +// Test that println! to a closed stdout does not panic. +// On Windows, close via CloseHandle. //@ run-pass -#![allow(dead_code)] //@ ignore-sgx no libc #![feature(rustc_private)] -extern crate libc; - -type DWORD = u32; -type HANDLE = *mut u8; - -#[cfg(windows)] -extern "system" { - fn GetStdHandle(which: DWORD) -> HANDLE; - fn CloseHandle(handle: HANDLE) -> i32; -} - #[cfg(windows)] fn close_stdout() { + type DWORD = u32; + type HANDLE = *mut u8; + + extern "system" { + fn GetStdHandle(which: DWORD) -> HANDLE; + fn CloseHandle(handle: HANDLE) -> i32; + } + const STD_OUTPUT_HANDLE: DWORD = -11i32 as DWORD; unsafe { CloseHandle(GetStdHandle(STD_OUTPUT_HANDLE)); } } #[cfg(not(windows))] fn close_stdout() { + extern crate libc; unsafe { libc::close(1); } } From 8047fadbf31985914b8ca55c98d7230e8e62694b Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Thu, 18 Apr 2024 09:52:00 -0400 Subject: [PATCH 2/2] Add an exception for windows-msvc --- tests/ui/extern-flag/auxiliary/panic_handler.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/ui/extern-flag/auxiliary/panic_handler.rs b/tests/ui/extern-flag/auxiliary/panic_handler.rs index 9953b783c62db..5ca32fa992b05 100644 --- a/tests/ui/extern-flag/auxiliary/panic_handler.rs +++ b/tests/ui/extern-flag/auxiliary/panic_handler.rs @@ -1,6 +1,15 @@ #![feature(lang_items)] #![no_std] +// Since `rustc` generally passes `-nodefaultlibs` to the linker, +// Rust programs link necessary system libraries via `#[link()]` +// attributes in the `libc` crate. `libc` is a dependency of `std`, +// but as we are `#![no_std]`, we need to include it manually. +// Except on windows-msvc. +#![feature(rustc_private)] +#[cfg(not(all(windows, target_env = "msvc")))] +extern crate libc; + #[panic_handler] pub fn begin_panic_handler(_info: &core::panic::PanicInfo<'_>) -> ! { loop {}