Skip to content

Commit e5fb21f

Browse files
borsgitbot
authored and
gitbot
committed
Auto merge of rust-lang#135525 - jhpratt:rollup-4gu2wpm, r=jhpratt
Rollup of 7 pull requests Successful merges: - rust-lang#132397 (Make missing_abi lint warn-by-default.) - rust-lang#133807 (ci: Enable opt-dist for dist-aarch64-linux builds) - rust-lang#134143 (Convert `struct FromBytesWithNulError` into enum) - rust-lang#134338 (Use a C-safe return type for `__rust_[ui]128_*` overflowing intrinsics) - rust-lang#134678 (Update `ReadDir::next` in `std::sys::pal::unix::fs` to use `&raw const (*p).field` instead of `p.byte_offset().cast()`) - rust-lang#135424 (Detect unstable lint docs that dont enable their feature) - rust-lang#135520 (Make sure we actually use the right trivial lifetime substs when eagerly monomorphizing drop for ADTs) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 8610d3c + 6cdf20c commit e5fb21f

File tree

5 files changed

+37
-59
lines changed

5 files changed

+37
-59
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

alloc/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ edition = "2021"
1010

1111
[dependencies]
1212
core = { path = "../core" }
13-
compiler_builtins = { version = "=0.1.141", features = ['rustc-dep-of-std'] }
13+
compiler_builtins = { version = "=0.1.143", features = ['rustc-dep-of-std'] }
1414

1515
[dev-dependencies]
1616
rand = { version = "0.8.5", default-features = false, features = ["alloc"] }

core/src/ffi/c_str.rs

+20-32
Original file line numberDiff line numberDiff line change
@@ -124,37 +124,25 @@ pub struct CStr {
124124
///
125125
/// let _: FromBytesWithNulError = CStr::from_bytes_with_nul(b"f\0oo").unwrap_err();
126126
/// ```
127-
#[derive(Clone, PartialEq, Eq, Debug)]
127+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
128128
#[stable(feature = "core_c_str", since = "1.64.0")]
129-
pub struct FromBytesWithNulError {
130-
kind: FromBytesWithNulErrorKind,
131-
}
132-
133-
#[derive(Clone, PartialEq, Eq, Debug)]
134-
enum FromBytesWithNulErrorKind {
135-
InteriorNul(usize),
129+
pub enum FromBytesWithNulError {
130+
/// Data provided contains an interior nul byte at byte `position`.
131+
InteriorNul {
132+
/// The position of the interior nul byte.
133+
position: usize,
134+
},
135+
/// Data provided is not nul terminated.
136136
NotNulTerminated,
137137
}
138138

139-
// FIXME: const stability attributes should not be required here, I think
140-
impl FromBytesWithNulError {
141-
const fn interior_nul(pos: usize) -> FromBytesWithNulError {
142-
FromBytesWithNulError { kind: FromBytesWithNulErrorKind::InteriorNul(pos) }
143-
}
144-
const fn not_nul_terminated() -> FromBytesWithNulError {
145-
FromBytesWithNulError { kind: FromBytesWithNulErrorKind::NotNulTerminated }
146-
}
147-
}
148-
149139
#[stable(feature = "frombyteswithnulerror_impls", since = "1.17.0")]
150140
impl Error for FromBytesWithNulError {
151141
#[allow(deprecated)]
152142
fn description(&self) -> &str {
153-
match self.kind {
154-
FromBytesWithNulErrorKind::InteriorNul(..) => {
155-
"data provided contains an interior nul byte"
156-
}
157-
FromBytesWithNulErrorKind::NotNulTerminated => "data provided is not nul terminated",
143+
match self {
144+
Self::InteriorNul { .. } => "data provided contains an interior nul byte",
145+
Self::NotNulTerminated => "data provided is not nul terminated",
158146
}
159147
}
160148
}
@@ -199,8 +187,8 @@ impl fmt::Display for FromBytesWithNulError {
199187
#[allow(deprecated, deprecated_in_future)]
200188
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
201189
f.write_str(self.description())?;
202-
if let FromBytesWithNulErrorKind::InteriorNul(pos) = self.kind {
203-
write!(f, " at byte pos {pos}")?;
190+
if let Self::InteriorNul { position } = self {
191+
write!(f, " at byte pos {position}")?;
204192
}
205193
Ok(())
206194
}
@@ -349,25 +337,25 @@ impl CStr {
349337
/// use std::ffi::CStr;
350338
///
351339
/// let cstr = CStr::from_bytes_with_nul(b"hello\0");
352-
/// assert!(cstr.is_ok());
340+
/// assert_eq!(cstr, Ok(c"hello"));
353341
/// ```
354342
///
355343
/// Creating a `CStr` without a trailing nul terminator is an error:
356344
///
357345
/// ```
358-
/// use std::ffi::CStr;
346+
/// use std::ffi::{CStr, FromBytesWithNulError};
359347
///
360348
/// let cstr = CStr::from_bytes_with_nul(b"hello");
361-
/// assert!(cstr.is_err());
349+
/// assert_eq!(cstr, Err(FromBytesWithNulError::NotNulTerminated));
362350
/// ```
363351
///
364352
/// Creating a `CStr` with an interior nul byte is an error:
365353
///
366354
/// ```
367-
/// use std::ffi::CStr;
355+
/// use std::ffi::{CStr, FromBytesWithNulError};
368356
///
369357
/// let cstr = CStr::from_bytes_with_nul(b"he\0llo\0");
370-
/// assert!(cstr.is_err());
358+
/// assert_eq!(cstr, Err(FromBytesWithNulError::InteriorNul { position: 2 }));
371359
/// ```
372360
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
373361
#[rustc_const_stable(feature = "const_cstr_methods", since = "1.72.0")]
@@ -379,8 +367,8 @@ impl CStr {
379367
// of the byte slice.
380368
Ok(unsafe { Self::from_bytes_with_nul_unchecked(bytes) })
381369
}
382-
Some(nul_pos) => Err(FromBytesWithNulError::interior_nul(nul_pos)),
383-
None => Err(FromBytesWithNulError::not_nul_terminated()),
370+
Some(position) => Err(FromBytesWithNulError::InteriorNul { position }),
371+
None => Err(FromBytesWithNulError::NotNulTerminated),
384372
}
385373
}
386374

std/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ cfg-if = { version = "1.0", features = ['rustc-dep-of-std'] }
1717
panic_unwind = { path = "../panic_unwind", optional = true }
1818
panic_abort = { path = "../panic_abort" }
1919
core = { path = "../core", public = true }
20-
compiler_builtins = { version = "=0.1.141" }
20+
compiler_builtins = { version = "=0.1.143" }
2121
unwind = { path = "../unwind" }
2222
hashbrown = { version = "0.15", default-features = false, features = [
2323
'rustc-dep-of-std',

std/src/sys/pal/unix/fs.rs

+13-23
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ impl Iterator for ReadDir {
709709
// thread safety for readdir() as long an individual DIR* is not accessed
710710
// concurrently, which is sufficient for Rust.
711711
super::os::set_errno(0);
712-
let entry_ptr = readdir64(self.inner.dirp.0);
712+
let entry_ptr: *const dirent64 = readdir64(self.inner.dirp.0);
713713
if entry_ptr.is_null() {
714714
// We either encountered an error, or reached the end. Either way,
715715
// the next call to next() should return None.
@@ -735,44 +735,34 @@ impl Iterator for ReadDir {
735735
// contents were "simply" partially initialized data.
736736
//
737737
// Like for uninitialized contents, converting entry_ptr to `&dirent64`
738-
// would not be legal. However, unique to dirent64 is that we don't even
739-
// get to use `&raw const (*entry_ptr).d_name` because that operation
740-
// requires the full extent of *entry_ptr to be in bounds of the same
741-
// allocation, which is not necessarily the case here.
742-
//
743-
// Instead we must access fields individually through their offsets.
744-
macro_rules! offset_ptr {
745-
($entry_ptr:expr, $field:ident) => {{
746-
const OFFSET: isize = mem::offset_of!(dirent64, $field) as isize;
747-
if true {
748-
// Cast to the same type determined by the else branch.
749-
$entry_ptr.byte_offset(OFFSET).cast::<_>()
750-
} else {
751-
#[allow(deref_nullptr)]
752-
{
753-
&raw const (*ptr::null::<dirent64>()).$field
754-
}
755-
}
756-
}};
738+
// would not be legal. However, we can use `&raw const (*entry_ptr).d_name`
739+
// to refer the fields individually, because that operation is equivalent
740+
// to `byte_offset` and thus does not require the full extent of `*entry_ptr`
741+
// to be in bounds of the same allocation, only the offset of the field
742+
// being referenced.
743+
macro_rules! entry_field_ptr {
744+
($field:ident) => {
745+
&raw const (*entry_ptr).$field
746+
};
757747
}
758748

759749
// d_name is guaranteed to be null-terminated.
760-
let name = CStr::from_ptr(offset_ptr!(entry_ptr, d_name).cast());
750+
let name = CStr::from_ptr(entry_field_ptr!(d_name).cast());
761751
let name_bytes = name.to_bytes();
762752
if name_bytes == b"." || name_bytes == b".." {
763753
continue;
764754
}
765755

766756
#[cfg(not(target_os = "vita"))]
767757
let entry = dirent64_min {
768-
d_ino: *offset_ptr!(entry_ptr, d_ino) as u64,
758+
d_ino: *entry_field_ptr!(d_ino) as u64,
769759
#[cfg(not(any(
770760
target_os = "solaris",
771761
target_os = "illumos",
772762
target_os = "aix",
773763
target_os = "nto",
774764
)))]
775-
d_type: *offset_ptr!(entry_ptr, d_type) as u8,
765+
d_type: *entry_field_ptr!(d_type) as u8,
776766
};
777767

778768
#[cfg(target_os = "vita")]

0 commit comments

Comments
 (0)