Skip to content

Commit 78bc0a5

Browse files
committed
Auto merge of #123913 - matthiaskrgr:rollup-w8stnwl, r=matthiaskrgr
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
2 parents a3269e9 + ab65c68 commit 78bc0a5

File tree

17 files changed

+289
-206
lines changed

17 files changed

+289
-206
lines changed

Cargo.lock

+15-3
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ dependencies = [
766766
"miropt-test-tools",
767767
"once_cell",
768768
"regex",
769-
"rustfix",
769+
"rustfix 0.8.1",
770770
"serde",
771771
"serde_json",
772772
"tracing",
@@ -4855,6 +4855,18 @@ dependencies = [
48554855
"serde_json",
48564856
]
48574857

4858+
[[package]]
4859+
name = "rustfix"
4860+
version = "0.8.1"
4861+
source = "registry+https://github.com/rust-lang/crates.io-index"
4862+
checksum = "81864b097046da5df3758fdc6e4822bbb70afa06317e8ca45ea1b51cb8c5e5a4"
4863+
dependencies = [
4864+
"serde",
4865+
"serde_json",
4866+
"thiserror",
4867+
"tracing",
4868+
]
4869+
48584870
[[package]]
48594871
name = "rustfmt-config_proc_macro"
48604872
version = "0.3.0"
@@ -5896,7 +5908,7 @@ dependencies = [
58965908
"prettydiff",
58975909
"regex",
58985910
"rustc_version",
5899-
"rustfix",
5911+
"rustfix 0.6.1",
59005912
"serde",
59015913
"serde_json",
59025914
"tempfile",
@@ -5923,7 +5935,7 @@ dependencies = [
59235935
"prettydiff",
59245936
"regex",
59255937
"rustc_version",
5926-
"rustfix",
5938+
"rustfix 0.6.1",
59275939
"serde",
59285940
"serde_json",
59295941
"spanned",

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,7 @@ pub(super) fn compare_impl_const_raw(
17231723

17241724
compare_number_of_generics(tcx, impl_const_item, trait_const_item, false)?;
17251725
compare_generic_param_kinds(tcx, impl_const_item, trait_const_item, false)?;
1726+
check_region_bounds_on_impl_item(tcx, impl_const_item, trait_const_item, false)?;
17261727
compare_const_predicate_entailment(tcx, impl_const_item, trait_const_item, impl_trait_ref)
17271728
}
17281729

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

1766-
check_region_bounds_on_impl_item(tcx, impl_ct, trait_ct, false)?;
1767-
17681767
// The predicates declared by the impl definition, the trait and the
17691768
// associated const in the trait are assumed.
17701769
let impl_predicates = tcx.predicates_of(impl_ct_predicates.parent.unwrap());
@@ -1866,6 +1865,7 @@ pub(super) fn compare_impl_ty<'tcx>(
18661865
let _: Result<(), ErrorGuaranteed> = try {
18671866
compare_number_of_generics(tcx, impl_ty, trait_ty, false)?;
18681867
compare_generic_param_kinds(tcx, impl_ty, trait_ty, false)?;
1868+
check_region_bounds_on_impl_item(tcx, impl_ty, trait_ty, false)?;
18691869
compare_type_predicate_entailment(tcx, impl_ty, trait_ty, impl_trait_ref)?;
18701870
check_type_bounds(tcx, trait_ty, impl_ty, impl_trait_ref)?;
18711871
};
@@ -1886,8 +1886,6 @@ fn compare_type_predicate_entailment<'tcx>(
18861886
let impl_ty_predicates = tcx.predicates_of(impl_ty.def_id);
18871887
let trait_ty_predicates = tcx.predicates_of(trait_ty.def_id);
18881888

1889-
check_region_bounds_on_impl_item(tcx, impl_ty, trait_ty, false)?;
1890-
18911889
let impl_ty_own_bounds = impl_ty_predicates.instantiate_own(tcx, impl_args);
18921890
if impl_ty_own_bounds.len() == 0 {
18931891
// Nothing to check.

library/core/src/fmt/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -860,10 +860,10 @@ pub trait Binary {
860860
/// Basic usage with `i32`:
861861
///
862862
/// ```
863-
/// let x = 42; // 42 is '2a' in hex
863+
/// let y = 42; // 42 is '2a' in hex
864864
///
865-
/// assert_eq!(format!("{x:x}"), "2a");
866-
/// assert_eq!(format!("{x:#x}"), "0x2a");
865+
/// assert_eq!(format!("{y:x}"), "2a");
866+
/// assert_eq!(format!("{y:#x}"), "0x2a");
867867
///
868868
/// assert_eq!(format!("{:x}", -16), "fffffff0");
869869
/// ```
@@ -915,10 +915,10 @@ pub trait LowerHex {
915915
/// Basic usage with `i32`:
916916
///
917917
/// ```
918-
/// let x = 42; // 42 is '2A' in hex
918+
/// let y = 42; // 42 is '2A' in hex
919919
///
920-
/// assert_eq!(format!("{x:X}"), "2A");
921-
/// assert_eq!(format!("{x:#X}"), "0x2A");
920+
/// assert_eq!(format!("{y:X}"), "2A");
921+
/// assert_eq!(format!("{y:#X}"), "0x2A");
922922
///
923923
/// assert_eq!(format!("{:X}", -16), "FFFFFFF0");
924924
/// ```

library/std/src/os/unix/net/addr.rs

+10
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ impl SocketAddr {
107107
addr: libc::sockaddr_un,
108108
mut len: libc::socklen_t,
109109
) -> io::Result<SocketAddr> {
110+
if cfg!(target_os = "openbsd") {
111+
// on OpenBSD, getsockname(2) returns the actual size of the socket address,
112+
// and not the len of the content. Figure out the length for ourselves.
113+
// https://marc.info/?l=openbsd-bugs&m=170105481926736&w=2
114+
let sun_path: &[u8] =
115+
unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&addr.sun_path) };
116+
len = core::slice::memchr::memchr(0, sun_path)
117+
.map_or(len, |new_len| (new_len + sun_path_offset(&addr)) as libc::socklen_t);
118+
}
119+
110120
if len == 0 {
111121
// When there is a datagram from unnamed unix socket
112122
// linux returns zero bytes of address

library/std/src/sys/pal/unix/thread.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ mod cgroups {
709709
// is created in an application with big thread-local storage requirements.
710710
// See #6233 for rationale and details.
711711
#[cfg(all(target_os = "linux", target_env = "gnu"))]
712-
fn min_stack_size(attr: *const libc::pthread_attr_t) -> usize {
712+
unsafe fn min_stack_size(attr: *const libc::pthread_attr_t) -> usize {
713713
// We use dlsym to avoid an ELF version dependency on GLIBC_PRIVATE. (#23628)
714714
// We shouldn't really be using such an internal symbol, but there's currently
715715
// no other way to account for the TLS size.
@@ -723,11 +723,11 @@ fn min_stack_size(attr: *const libc::pthread_attr_t) -> usize {
723723

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

730730
#[cfg(target_os = "netbsd")]
731-
fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
731+
unsafe fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
732732
2048 // just a guess
733733
}

library/std/src/sys/sync/rwlock/queue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl Node {
202202
fn prepare(&mut self) {
203203
// Fall back to creating an unnamed `Thread` handle to allow locking in
204204
// TLS destructors.
205-
self.thread.get_or_init(|| thread::try_current().unwrap_or_else(|| Thread::new(None)));
205+
self.thread.get_or_init(|| thread::try_current().unwrap_or_else(Thread::new_unnamed));
206206
self.completed = AtomicBool::new(false);
207207
}
208208

library/std/src/thread/local.rs

+32-28
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,25 @@ use crate::fmt;
5353
/// # Examples
5454
///
5555
/// ```
56-
/// use std::cell::RefCell;
56+
/// use std::cell::Cell;
5757
/// use std::thread;
5858
///
59-
/// thread_local!(static FOO: RefCell<u32> = RefCell::new(1));
59+
/// thread_local!(static FOO: Cell<u32> = Cell::new(1));
6060
///
61-
/// FOO.with_borrow(|v| assert_eq!(*v, 1));
62-
/// FOO.with_borrow_mut(|v| *v = 2);
61+
/// assert_eq!(FOO.get(), 1);
62+
/// FOO.set(2);
6363
///
6464
/// // each thread starts out with the initial value of 1
6565
/// let t = thread::spawn(move|| {
66-
/// FOO.with_borrow(|v| assert_eq!(*v, 1));
67-
/// FOO.with_borrow_mut(|v| *v = 3);
66+
/// assert_eq!(FOO.get(), 1);
67+
/// FOO.set(3);
6868
/// });
6969
///
7070
/// // wait for the thread to complete and bail out on panic
7171
/// t.join().unwrap();
7272
///
7373
/// // we retain our original value of 2 despite the child thread
74-
/// FOO.with_borrow(|v| assert_eq!(*v, 2));
74+
/// assert_eq!(FOO.get(), 2);
7575
/// ```
7676
///
7777
/// # Platform-specific behavior
@@ -141,15 +141,16 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
141141
/// Publicity and attributes for each static are allowed. Example:
142142
///
143143
/// ```
144-
/// use std::cell::RefCell;
144+
/// use std::cell::{Cell, RefCell};
145+
///
145146
/// thread_local! {
146-
/// pub static FOO: RefCell<u32> = RefCell::new(1);
147+
/// pub static FOO: Cell<u32> = Cell::new(1);
147148
///
148-
/// static BAR: RefCell<f32> = RefCell::new(1.0);
149+
/// static BAR: RefCell<Vec<f32>> = RefCell::new(vec![1.0, 2.0]);
149150
/// }
150151
///
151-
/// FOO.with_borrow(|v| assert_eq!(*v, 1));
152-
/// BAR.with_borrow(|v| assert_eq!(*v, 1.0));
152+
/// assert_eq!(FOO.get(), 1);
153+
/// BAR.with_borrow(|v| assert_eq!(v[1], 2.0));
153154
/// ```
154155
///
155156
/// Note that only shared references (`&T`) to the inner data may be obtained, so a
@@ -164,12 +165,13 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
164165
/// track any additional state.
165166
///
166167
/// ```
167-
/// use std::cell::Cell;
168+
/// use std::cell::RefCell;
169+
///
168170
/// thread_local! {
169-
/// pub static FOO: Cell<u32> = const { Cell::new(1) };
171+
/// pub static FOO: RefCell<Vec<u32>> = const { RefCell::new(Vec::new()) };
170172
/// }
171173
///
172-
/// assert_eq!(FOO.get(), 1);
174+
/// FOO.with_borrow(|v| assert_eq!(v.len(), 0));
173175
/// ```
174176
///
175177
/// See [`LocalKey` documentation][`std::thread::LocalKey`] for more
@@ -279,10 +281,9 @@ impl<T: 'static> LocalKey<T> {
279281
where
280282
F: FnOnce(&T) -> R,
281283
{
282-
unsafe {
283-
let thread_local = (self.inner)(None).ok_or(AccessError)?;
284-
Ok(f(thread_local))
285-
}
284+
// SAFETY: `inner` is safe to call within the lifetime of the thread
285+
let thread_local = unsafe { (self.inner)(None).ok_or(AccessError)? };
286+
Ok(f(thread_local))
286287
}
287288

288289
/// Acquires a reference to the value in this TLS key, initializing it with
@@ -301,14 +302,17 @@ impl<T: 'static> LocalKey<T> {
301302
where
302303
F: FnOnce(Option<T>, &T) -> R,
303304
{
304-
unsafe {
305-
let mut init = Some(init);
306-
let reference = (self.inner)(Some(&mut init)).expect(
305+
let mut init = Some(init);
306+
307+
// SAFETY: `inner` is safe to call within the lifetime of the thread
308+
let reference = unsafe {
309+
(self.inner)(Some(&mut init)).expect(
307310
"cannot access a Thread Local Storage value \
308311
during or after destruction",
309-
);
310-
f(init, reference)
311-
}
312+
)
313+
};
314+
315+
f(init, reference)
312316
}
313317
}
314318

@@ -377,7 +381,7 @@ impl<T: 'static> LocalKey<Cell<T>> {
377381
where
378382
T: Copy,
379383
{
380-
self.with(|cell| cell.get())
384+
self.with(Cell::get)
381385
}
382386

383387
/// Takes the contained value, leaving `Default::default()` in its place.
@@ -407,7 +411,7 @@ impl<T: 'static> LocalKey<Cell<T>> {
407411
where
408412
T: Default,
409413
{
410-
self.with(|cell| cell.take())
414+
self.with(Cell::take)
411415
}
412416

413417
/// Replaces the contained value, returning the old value.
@@ -578,7 +582,7 @@ impl<T: 'static> LocalKey<RefCell<T>> {
578582
where
579583
T: Default,
580584
{
581-
self.with(|cell| cell.take())
585+
self.with(RefCell::take)
582586
}
583587

584588
/// Replaces the contained value, returning the old value.

library/std/src/thread/mod.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,11 @@ impl Builder {
487487
amt
488488
});
489489

490-
let my_thread = Thread::new(name.map(|name| {
491-
CString::new(name).expect("thread name may not contain interior null bytes")
492-
}));
490+
let my_thread = name.map_or_else(Thread::new_unnamed, |name| unsafe {
491+
Thread::new(
492+
CString::new(name).expect("thread name may not contain interior null bytes"),
493+
)
494+
});
493495
let their_thread = my_thread.clone();
494496

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

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

13091311
impl Thread {
1310-
// Used only internally to construct a thread object without spawning
1311-
pub(crate) fn new(name: Option<CString>) -> Thread {
1312-
if let Some(name) = name {
1313-
Self::new_inner(ThreadName::Other(name))
1314-
} else {
1315-
Self::new_inner(ThreadName::Unnamed)
1316-
}
1312+
/// Used only internally to construct a thread object without spawning.
1313+
///
1314+
/// # Safety
1315+
/// `name` must be valid UTF-8.
1316+
pub(crate) unsafe fn new(name: CString) -> Thread {
1317+
unsafe { Self::new_inner(ThreadName::Other(name)) }
1318+
}
1319+
1320+
pub(crate) fn new_unnamed() -> Thread {
1321+
unsafe { Self::new_inner(ThreadName::Unnamed) }
13171322
}
13181323

13191324
// Used in runtime to construct main thread
13201325
pub(crate) fn new_main() -> Thread {
1321-
Self::new_inner(ThreadName::Main)
1326+
unsafe { Self::new_inner(ThreadName::Main) }
13221327
}
13231328

1324-
fn new_inner(name: ThreadName) -> Thread {
1329+
/// # Safety
1330+
/// If `name` is `ThreadName::Other(_)`, the contained string must be valid UTF-8.
1331+
unsafe fn new_inner(name: ThreadName) -> Thread {
13251332
// We have to use `unsafe` here to construct the `Parker` in-place,
13261333
// which is required for the UNIX implementation.
13271334
//

src/tools/compiletest/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ tracing-subscriber = { version = "0.3.3", default-features = false, features = [
2020
regex = "1.0"
2121
serde = { version = "1.0", features = ["derive"] }
2222
serde_json = "1.0"
23-
rustfix = "0.6.0"
23+
rustfix = "0.8.1"
2424
once_cell = "1.16.0"
2525
walkdir = "2"
2626
glob = "0.3.0"

0 commit comments

Comments
 (0)