Skip to content

Commit 6b06843

Browse files
committed
Address address comments, improve comments slightly
1 parent ea21169 commit 6b06843

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

library/std/src/io/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ enum ErrorData<C> {
8383
Custom(C),
8484
}
8585

86+
// `#[repr(align(4))]` is probably redundant, it should have that value or
87+
// higher already. We include it just because repr_bitpacked.rs's encoding
88+
// requires an alignment >= 4 (note that `#[repr(align)]` will not reduce the
89+
// alignment required by the struct, only increase it).
8690
#[repr(align(4))]
8791
#[doc(hidden)]
8892
pub(crate) struct SimpleMessage {
@@ -106,6 +110,9 @@ pub(crate) macro const_io_error($kind:expr, $message:expr $(,)?) {
106110
})
107111
}
108112

113+
// As with `SimpleMessage`: `#[repr(align(4))]` here is just because
114+
// repr_bitpacked's encoding requires it. In practice it almost certainly be
115+
// already be this high or higher.
109116
#[derive(Debug)]
110117
#[repr(align(4))]
111118
struct Custom {

library/std/src/io/error/repr_bitpacked.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
//!
5757
//! Conceptually you might think of this more like:
5858
//!
59-
//! ```ignore
59+
//! ```ignore (exposition-only)
6060
//! union Repr {
6161
//! // holds integer (Simple/Os) variants, and
6262
//! // provides access to the tag bits.
@@ -159,7 +159,7 @@ impl Repr {
159159

160160
#[inline]
161161
pub(super) const fn new_simple_message(m: &'static SimpleMessage) -> Self {
162-
// Safety: We're a Repr, decode_repr is fine.
162+
// Safety: References are never null.
163163
Self(unsafe { NonNull::new_unchecked(m as *const _ as *mut ()) })
164164
}
165165

@@ -213,7 +213,7 @@ where
213213
TAG_SIMPLE => {
214214
let kind_bits = (bits >> 32) as u32;
215215
let kind = kind_from_prim(kind_bits).unwrap_or_else(|| {
216-
debug_assert!(false, "Invalid io::error::Repr bits: `Repr({:#016x})`", bits);
216+
debug_assert!(false, "Invalid io::error::Repr bits: `Repr({:#018x})`", bits);
217217
// This means the `ptr` passed in was not valid, which voilates
218218
// the unsafe contract of `decode_repr`.
219219
//
@@ -299,8 +299,11 @@ fn kind_from_prim(ek: u32) -> Option<ErrorKind> {
299299
}
300300

301301
// Some static checking to alert us if a change breaks any of the assumptions
302-
// that our encoding relies on. If any of these are hit on a platform that
303-
// libstd supports, we should just make sure `repr_unpacked.rs` is used.
302+
// that our encoding relies on for correctness and soundness. (Some of these are
303+
// a bit overly thorough/cautious, admittedly)
304+
//
305+
// If any of these are hit on a platform that libstd supports, we should just
306+
// make sure `repr_unpacked.rs` is used instead.
304307
macro_rules! static_assert {
305308
($condition:expr) => {
306309
const _: [(); 0] = [(); (!$condition) as usize];
@@ -332,6 +335,11 @@ static_assert!(TAG_SIMPLE != 0);
332335
static_assert!(TAG_SIMPLE_MESSAGE == 0);
333336

334337
// Check that the point of all of this still holds.
338+
//
339+
// We'd check against `io::Error`, but *technically* it's allowed to vary,
340+
// as it's not `#[repr(transparent)]`/`#[repr(C)]`. We could add that, but
341+
// the `#[repr()]` would show up in rustdoc, which might be seen as a stable
342+
// commitment.
335343
static_assert!(size_of::<Repr>() == 8);
336344
static_assert!(size_of::<Option<Repr>>() == 8);
337345
static_assert!(size_of::<Result<(), Repr>>() == 8);

0 commit comments

Comments
 (0)