Skip to content

Commit

Permalink
Auto merge of #123913 - matthiaskrgr:rollup-w8stnwl, r=matthiaskrgr
Browse files Browse the repository at this point in the history
Rollup of 8 pull requests

Successful merges:

 - #123651 (Thread local updates for idiomatic examples)
 - #123699 (run-make-support: tidy up support library)
 - #123779 (OpenBSD fix long socket addresses)
 - #123875 (Doc: replace x with y for hexa-decimal fmt)
 - #123879 (Add missing `unsafe` to some internal `std` functions)
 - #123889 (reduce tidy overheads in run-make checks)
 - #123898 (Generic associated consts: Check regions earlier when comparing impl with trait item def)
 - #123902 (compiletest: Update rustfix to 0.8.1)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Apr 14, 2024
2 parents a3269e9 + ab65c68 commit 78bc0a5
Show file tree
Hide file tree
Showing 17 changed files with 289 additions and 206 deletions.
18 changes: 15 additions & 3 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ dependencies = [
"miropt-test-tools",
"once_cell",
"regex",
"rustfix",
"rustfix 0.8.1",
"serde",
"serde_json",
"tracing",
Expand Down Expand Up @@ -4855,6 +4855,18 @@ dependencies = [
"serde_json",
]

[[package]]
name = "rustfix"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81864b097046da5df3758fdc6e4822bbb70afa06317e8ca45ea1b51cb8c5e5a4"
dependencies = [
"serde",
"serde_json",
"thiserror",
"tracing",
]

[[package]]
name = "rustfmt-config_proc_macro"
version = "0.3.0"
Expand Down Expand Up @@ -5896,7 +5908,7 @@ dependencies = [
"prettydiff",
"regex",
"rustc_version",
"rustfix",
"rustfix 0.6.1",
"serde",
"serde_json",
"tempfile",
Expand All @@ -5923,7 +5935,7 @@ dependencies = [
"prettydiff",
"regex",
"rustc_version",
"rustfix",
"rustfix 0.6.1",
"serde",
"serde_json",
"spanned",
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,7 @@ pub(super) fn compare_impl_const_raw(

compare_number_of_generics(tcx, impl_const_item, trait_const_item, false)?;
compare_generic_param_kinds(tcx, impl_const_item, trait_const_item, false)?;
check_region_bounds_on_impl_item(tcx, impl_const_item, trait_const_item, false)?;
compare_const_predicate_entailment(tcx, impl_const_item, trait_const_item, impl_trait_ref)
}

Expand Down Expand Up @@ -1763,8 +1764,6 @@ fn compare_const_predicate_entailment<'tcx>(
let impl_ct_predicates = tcx.predicates_of(impl_ct.def_id);
let trait_ct_predicates = tcx.predicates_of(trait_ct.def_id);

check_region_bounds_on_impl_item(tcx, impl_ct, trait_ct, false)?;

// The predicates declared by the impl definition, the trait and the
// associated const in the trait are assumed.
let impl_predicates = tcx.predicates_of(impl_ct_predicates.parent.unwrap());
Expand Down Expand Up @@ -1866,6 +1865,7 @@ pub(super) fn compare_impl_ty<'tcx>(
let _: Result<(), ErrorGuaranteed> = try {
compare_number_of_generics(tcx, impl_ty, trait_ty, false)?;
compare_generic_param_kinds(tcx, impl_ty, trait_ty, false)?;
check_region_bounds_on_impl_item(tcx, impl_ty, trait_ty, false)?;
compare_type_predicate_entailment(tcx, impl_ty, trait_ty, impl_trait_ref)?;
check_type_bounds(tcx, trait_ty, impl_ty, impl_trait_ref)?;
};
Expand All @@ -1886,8 +1886,6 @@ fn compare_type_predicate_entailment<'tcx>(
let impl_ty_predicates = tcx.predicates_of(impl_ty.def_id);
let trait_ty_predicates = tcx.predicates_of(trait_ty.def_id);

check_region_bounds_on_impl_item(tcx, impl_ty, trait_ty, false)?;

let impl_ty_own_bounds = impl_ty_predicates.instantiate_own(tcx, impl_args);
if impl_ty_own_bounds.len() == 0 {
// Nothing to check.
Expand Down
12 changes: 6 additions & 6 deletions library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,10 +860,10 @@ pub trait Binary {
/// Basic usage with `i32`:
///
/// ```
/// let x = 42; // 42 is '2a' in hex
/// let y = 42; // 42 is '2a' in hex
///
/// assert_eq!(format!("{x:x}"), "2a");
/// assert_eq!(format!("{x:#x}"), "0x2a");
/// assert_eq!(format!("{y:x}"), "2a");
/// assert_eq!(format!("{y:#x}"), "0x2a");
///
/// assert_eq!(format!("{:x}", -16), "fffffff0");
/// ```
Expand Down Expand Up @@ -915,10 +915,10 @@ pub trait LowerHex {
/// Basic usage with `i32`:
///
/// ```
/// let x = 42; // 42 is '2A' in hex
/// let y = 42; // 42 is '2A' in hex
///
/// assert_eq!(format!("{x:X}"), "2A");
/// assert_eq!(format!("{x:#X}"), "0x2A");
/// assert_eq!(format!("{y:X}"), "2A");
/// assert_eq!(format!("{y:#X}"), "0x2A");
///
/// assert_eq!(format!("{:X}", -16), "FFFFFFF0");
/// ```
Expand Down
10 changes: 10 additions & 0 deletions library/std/src/os/unix/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ impl SocketAddr {
addr: libc::sockaddr_un,
mut len: libc::socklen_t,
) -> io::Result<SocketAddr> {
if cfg!(target_os = "openbsd") {
// on OpenBSD, getsockname(2) returns the actual size of the socket address,
// and not the len of the content. Figure out the length for ourselves.
// https://marc.info/?l=openbsd-bugs&m=170105481926736&w=2
let sun_path: &[u8] =
unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&addr.sun_path) };
len = core::slice::memchr::memchr(0, sun_path)
.map_or(len, |new_len| (new_len + sun_path_offset(&addr)) as libc::socklen_t);
}

if len == 0 {
// When there is a datagram from unnamed unix socket
// linux returns zero bytes of address
Expand Down
6 changes: 3 additions & 3 deletions library/std/src/sys/pal/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ mod cgroups {
// is created in an application with big thread-local storage requirements.
// See #6233 for rationale and details.
#[cfg(all(target_os = "linux", target_env = "gnu"))]
fn min_stack_size(attr: *const libc::pthread_attr_t) -> usize {
unsafe fn min_stack_size(attr: *const libc::pthread_attr_t) -> usize {
// We use dlsym to avoid an ELF version dependency on GLIBC_PRIVATE. (#23628)
// We shouldn't really be using such an internal symbol, but there's currently
// no other way to account for the TLS size.
Expand All @@ -723,11 +723,11 @@ fn min_stack_size(attr: *const libc::pthread_attr_t) -> usize {

// No point in looking up __pthread_get_minstack() on non-glibc platforms.
#[cfg(all(not(all(target_os = "linux", target_env = "gnu")), not(target_os = "netbsd")))]
fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
unsafe fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
libc::PTHREAD_STACK_MIN
}

#[cfg(target_os = "netbsd")]
fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
unsafe fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
2048 // just a guess
}
2 changes: 1 addition & 1 deletion library/std/src/sys/sync/rwlock/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl Node {
fn prepare(&mut self) {
// Fall back to creating an unnamed `Thread` handle to allow locking in
// TLS destructors.
self.thread.get_or_init(|| thread::try_current().unwrap_or_else(|| Thread::new(None)));
self.thread.get_or_init(|| thread::try_current().unwrap_or_else(Thread::new_unnamed));
self.completed = AtomicBool::new(false);
}

Expand Down
60 changes: 32 additions & 28 deletions library/std/src/thread/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,25 @@ use crate::fmt;
/// # Examples
///
/// ```
/// use std::cell::RefCell;
/// use std::cell::Cell;
/// use std::thread;
///
/// thread_local!(static FOO: RefCell<u32> = RefCell::new(1));
/// thread_local!(static FOO: Cell<u32> = Cell::new(1));
///
/// FOO.with_borrow(|v| assert_eq!(*v, 1));
/// FOO.with_borrow_mut(|v| *v = 2);
/// assert_eq!(FOO.get(), 1);
/// FOO.set(2);
///
/// // each thread starts out with the initial value of 1
/// let t = thread::spawn(move|| {
/// FOO.with_borrow(|v| assert_eq!(*v, 1));
/// FOO.with_borrow_mut(|v| *v = 3);
/// assert_eq!(FOO.get(), 1);
/// FOO.set(3);
/// });
///
/// // wait for the thread to complete and bail out on panic
/// t.join().unwrap();
///
/// // we retain our original value of 2 despite the child thread
/// FOO.with_borrow(|v| assert_eq!(*v, 2));
/// assert_eq!(FOO.get(), 2);
/// ```
///
/// # Platform-specific behavior
Expand Down Expand Up @@ -141,15 +141,16 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
/// Publicity and attributes for each static are allowed. Example:
///
/// ```
/// use std::cell::RefCell;
/// use std::cell::{Cell, RefCell};
///
/// thread_local! {
/// pub static FOO: RefCell<u32> = RefCell::new(1);
/// pub static FOO: Cell<u32> = Cell::new(1);
///
/// static BAR: RefCell<f32> = RefCell::new(1.0);
/// static BAR: RefCell<Vec<f32>> = RefCell::new(vec![1.0, 2.0]);
/// }
///
/// FOO.with_borrow(|v| assert_eq!(*v, 1));
/// BAR.with_borrow(|v| assert_eq!(*v, 1.0));
/// assert_eq!(FOO.get(), 1);
/// BAR.with_borrow(|v| assert_eq!(v[1], 2.0));
/// ```
///
/// Note that only shared references (`&T`) to the inner data may be obtained, so a
Expand All @@ -164,12 +165,13 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
/// track any additional state.
///
/// ```
/// use std::cell::Cell;
/// use std::cell::RefCell;
///
/// thread_local! {
/// pub static FOO: Cell<u32> = const { Cell::new(1) };
/// pub static FOO: RefCell<Vec<u32>> = const { RefCell::new(Vec::new()) };
/// }
///
/// assert_eq!(FOO.get(), 1);
/// FOO.with_borrow(|v| assert_eq!(v.len(), 0));
/// ```
///
/// See [`LocalKey` documentation][`std::thread::LocalKey`] for more
Expand Down Expand Up @@ -279,10 +281,9 @@ impl<T: 'static> LocalKey<T> {
where
F: FnOnce(&T) -> R,
{
unsafe {
let thread_local = (self.inner)(None).ok_or(AccessError)?;
Ok(f(thread_local))
}
// SAFETY: `inner` is safe to call within the lifetime of the thread
let thread_local = unsafe { (self.inner)(None).ok_or(AccessError)? };
Ok(f(thread_local))
}

/// Acquires a reference to the value in this TLS key, initializing it with
Expand All @@ -301,14 +302,17 @@ impl<T: 'static> LocalKey<T> {
where
F: FnOnce(Option<T>, &T) -> R,
{
unsafe {
let mut init = Some(init);
let reference = (self.inner)(Some(&mut init)).expect(
let mut init = Some(init);

// SAFETY: `inner` is safe to call within the lifetime of the thread
let reference = unsafe {
(self.inner)(Some(&mut init)).expect(
"cannot access a Thread Local Storage value \
during or after destruction",
);
f(init, reference)
}
)
};

f(init, reference)
}
}

Expand Down Expand Up @@ -377,7 +381,7 @@ impl<T: 'static> LocalKey<Cell<T>> {
where
T: Copy,
{
self.with(|cell| cell.get())
self.with(Cell::get)
}

/// Takes the contained value, leaving `Default::default()` in its place.
Expand Down Expand Up @@ -407,7 +411,7 @@ impl<T: 'static> LocalKey<Cell<T>> {
where
T: Default,
{
self.with(|cell| cell.take())
self.with(Cell::take)
}

/// Replaces the contained value, returning the old value.
Expand Down Expand Up @@ -578,7 +582,7 @@ impl<T: 'static> LocalKey<RefCell<T>> {
where
T: Default,
{
self.with(|cell| cell.take())
self.with(RefCell::take)
}

/// Replaces the contained value, returning the old value.
Expand Down
33 changes: 20 additions & 13 deletions library/std/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,11 @@ impl Builder {
amt
});

let my_thread = Thread::new(name.map(|name| {
CString::new(name).expect("thread name may not contain interior null bytes")
}));
let my_thread = name.map_or_else(Thread::new_unnamed, |name| unsafe {
Thread::new(
CString::new(name).expect("thread name may not contain interior null bytes"),
)
});
let their_thread = my_thread.clone();

let my_packet: Arc<Packet<'scope, T>> = Arc::new(Packet {
Expand Down Expand Up @@ -711,7 +713,7 @@ pub(crate) fn set_current(thread: Thread) {
/// In contrast to the public `current` function, this will not panic if called
/// from inside a TLS destructor.
pub(crate) fn try_current() -> Option<Thread> {
CURRENT.try_with(|current| current.get_or_init(|| Thread::new(None)).clone()).ok()
CURRENT.try_with(|current| current.get_or_init(|| Thread::new_unnamed()).clone()).ok()
}

/// Gets a handle to the thread that invokes it.
Expand Down Expand Up @@ -1307,21 +1309,26 @@ pub struct Thread {
}

impl Thread {
// Used only internally to construct a thread object without spawning
pub(crate) fn new(name: Option<CString>) -> Thread {
if let Some(name) = name {
Self::new_inner(ThreadName::Other(name))
} else {
Self::new_inner(ThreadName::Unnamed)
}
/// Used only internally to construct a thread object without spawning.
///
/// # Safety
/// `name` must be valid UTF-8.
pub(crate) unsafe fn new(name: CString) -> Thread {
unsafe { Self::new_inner(ThreadName::Other(name)) }
}

pub(crate) fn new_unnamed() -> Thread {
unsafe { Self::new_inner(ThreadName::Unnamed) }
}

// Used in runtime to construct main thread
pub(crate) fn new_main() -> Thread {
Self::new_inner(ThreadName::Main)
unsafe { Self::new_inner(ThreadName::Main) }
}

fn new_inner(name: ThreadName) -> Thread {
/// # Safety
/// If `name` is `ThreadName::Other(_)`, the contained string must be valid UTF-8.
unsafe fn new_inner(name: ThreadName) -> Thread {
// We have to use `unsafe` here to construct the `Parker` in-place,
// which is required for the UNIX implementation.
//
Expand Down
2 changes: 1 addition & 1 deletion src/tools/compiletest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ tracing-subscriber = { version = "0.3.3", default-features = false, features = [
regex = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
rustfix = "0.6.0"
rustfix = "0.8.1"
once_cell = "1.16.0"
walkdir = "2"
glob = "0.3.0"
Expand Down
Loading

0 comments on commit 78bc0a5

Please sign in to comment.