Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/byte_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use core::{
//
// See the documentation on `util::polyfills` for more information.
#[allow(unused_imports)]
use crate::util::polyfills::{self, NonNullExt as _, NumExt as _};
use crate::util::polyfills::{self, NumExt as _};
#[cfg(doc)]
use crate::Ref;

Expand Down
2 changes: 0 additions & 2 deletions src/byteorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1280,9 +1280,7 @@ mod tests {
fn test_const_methods() {
use big_endian::*;

#[rustversion::since(1.61.0)]
const _U: U16 = U16::new(0);
#[rustversion::since(1.61.0)]
const _NATIVE: u16 = _U.get();
const _FROM_BYTES: U16 = U16::from_bytes([0, 1]);
const _BYTES: [u8; 2] = _FROM_BYTES.to_bytes();
Expand Down
58 changes: 23 additions & 35 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl DstLayout {
/// The minimum possible alignment of a type.
const MIN_ALIGN: NonZeroUsize = match NonZeroUsize::new(1) {
Some(min_align) => min_align,
None => const_unreachable!(),
None => unreachable!(),
};

/// The maximum theoretic possible alignment of a type.
Expand All @@ -113,7 +113,7 @@ impl DstLayout {
pub(crate) const THEORETICAL_MAX_ALIGN: NonZeroUsize =
match NonZeroUsize::new(1 << (POINTER_WIDTH_BITS - 1)) {
Some(max_align) => max_align,
None => const_unreachable!(),
None => unreachable!(),
};

/// The current, documented max alignment of a type \[1\].
Expand All @@ -126,14 +126,14 @@ impl DstLayout {
#[cfg(not(target_pointer_width = "16"))]
pub(crate) const CURRENT_MAX_ALIGN: NonZeroUsize = match NonZeroUsize::new(1 << 28) {
Some(max_align) => max_align,
None => const_unreachable!(),
None => unreachable!(),
};

#[cfg(not(kani))]
#[cfg(target_pointer_width = "16")]
pub(crate) const CURRENT_MAX_ALIGN: NonZeroUsize = match NonZeroUsize::new(1 << 15) {
Some(max_align) => max_align,
None => const_unreachable!(),
None => unreachable!(),
};

/// The maximum size of an allocation \[1\].
Expand Down Expand Up @@ -184,7 +184,7 @@ impl DstLayout {
None => Self::MIN_ALIGN,
};

const_assert!(align.get().is_power_of_two());
assert!(align.get().is_power_of_two());

DstLayout {
align,
Expand All @@ -210,7 +210,7 @@ impl DstLayout {
DstLayout {
align: match NonZeroUsize::new(mem::align_of::<T>()) {
Some(align) => align,
None => const_unreachable!(),
None => unreachable!(),
},
size_info: SizeInfo::Sized { size: mem::size_of::<T>() },
statically_shallow_unpadded: false,
Expand Down Expand Up @@ -247,7 +247,7 @@ impl DstLayout {
DstLayout {
align: match NonZeroUsize::new(mem::align_of::<T>()) {
Some(align) => align,
None => const_unreachable!(),
None => unreachable!(),
},
size_info: SizeInfo::SliceDst(TrailingSliceLayout {
offset: 0,
Expand Down Expand Up @@ -344,17 +344,17 @@ impl DstLayout {
None => Self::THEORETICAL_MAX_ALIGN,
};

const_assert!(max_align.get().is_power_of_two());
assert!(max_align.get().is_power_of_two());

// We use Kani to prove that this method is robust to future increases
// in Rust's maximum allowed alignment. However, if such a change ever
// actually occurs, we'd like to be notified via assertion failures.
#[cfg(not(kani))]
{
const_debug_assert!(self.align.get() <= DstLayout::CURRENT_MAX_ALIGN.get());
const_debug_assert!(field.align.get() <= DstLayout::CURRENT_MAX_ALIGN.get());
debug_assert!(self.align.get() <= DstLayout::CURRENT_MAX_ALIGN.get());
debug_assert!(field.align.get() <= DstLayout::CURRENT_MAX_ALIGN.get());
if let Some(repr_packed) = repr_packed {
const_debug_assert!(repr_packed.get() <= DstLayout::CURRENT_MAX_ALIGN.get());
debug_assert!(repr_packed.get() <= DstLayout::CURRENT_MAX_ALIGN.get());
}
}

Expand All @@ -375,7 +375,7 @@ impl DstLayout {
let (interfield_padding, size_info) = match self.size_info {
// If the layout is already a DST, we panic; DSTs cannot be extended
// with additional fields.
SizeInfo::SliceDst(..) => const_panic!("Cannot extend a DST with additional fields."),
SizeInfo::SliceDst(..) => panic!("Cannot extend a DST with additional fields."),

SizeInfo::Sized { size: preceding_size } => {
// Compute the minimum amount of inter-field padding needed to
Expand All @@ -396,7 +396,7 @@ impl DstLayout {
// exceeding `isize::MAX`).
let offset = match preceding_size.checked_add(padding) {
Some(offset) => offset,
None => const_panic!("Adding padding to `self`'s size overflows `usize`."),
None => panic!("Adding padding to `self`'s size overflows `usize`."),
};

(
Expand All @@ -416,7 +416,7 @@ impl DstLayout {
// `usize::MAX`).
let size = match offset.checked_add(field_size) {
Some(size) => size,
None => const_panic!("`field` cannot be appended without the total size overflowing `usize`"),
None => panic!("`field` cannot be appended without the total size overflowing `usize`"),
};
SizeInfo::Sized { size }
}
Expand All @@ -438,7 +438,7 @@ impl DstLayout {
// `usize::MAX`).
let offset = match offset.checked_add(trailing_offset) {
Some(offset) => offset,
None => const_panic!("`field` cannot be appended without the total size overflowing `usize`"),
None => panic!("`field` cannot be appended without the total size overflowing `usize`"),
};
SizeInfo::SliceDst(TrailingSliceLayout { offset, elem_size })
}
Expand Down Expand Up @@ -495,7 +495,7 @@ impl DstLayout {
let padding = padding_needed_for(unpadded_size, self.align);
let size = match unpadded_size.checked_add(padding) {
Some(size) => size,
None => const_panic!("Adding padding caused size to overflow `usize`."),
None => panic!("Adding padding caused size to overflow `usize`."),
};
(padding, SizeInfo::Sized { size })
}
Expand Down Expand Up @@ -611,7 +611,7 @@ impl DstLayout {
// `debug_assert!`, but with `#[allow(clippy::arithmetic_side_effects)]`.
macro_rules! __const_debug_assert {
($e:expr $(, $msg:expr)?) => {
const_debug_assert!({
debug_assert!({
#[allow(clippy::arithmetic_side_effects)]
let e = $e;
e
Expand All @@ -626,11 +626,8 @@ impl DstLayout {
// would have failed anyway for runtime reasons (such as a too-small
// memory region).
//
// FIXME(#67): Once our MSRV is 1.65, use let-else:
// https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html#let-else-statements
let size_info = match self.size_info.try_to_nonzero_elem_size() {
Some(size_info) => size_info,
None => const_panic!("attempted to cast to slice type with zero-sized element"),
let Some(size_info) = self.size_info.try_to_nonzero_elem_size() else {
panic!("attempted to cast to slice type with zero-sized element")
};

// Precondition
Expand Down Expand Up @@ -685,13 +682,9 @@ impl DstLayout {
util::round_down_to_next_multiple_of_alignment(bytes_len, self.align);
// Calculate the maximum number of bytes that could be consumed
// by the trailing slice.
//
// FIXME(#67): Once our MSRV is 1.65, use let-else:
// https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html#let-else-statements
let max_slice_and_padding_bytes = match max_total_bytes.checked_sub(offset) {
Some(max) => max,
let Some(max_slice_and_padding_bytes) = max_total_bytes.checked_sub(offset) else {
// `bytes_len` too small even for 0 trailing slice elements.
None => return Err(MetadataCastError::Size),
return Err(MetadataCastError::Size);
};

// Calculate the number of elements that fit in
Expand Down Expand Up @@ -1070,7 +1063,7 @@ mod cast_from {
const CAST_PARAMS: CastParams<Src, Dst> =
match CastParams::try_compute(&Src::LAYOUT, &Dst::LAYOUT) {
Some(params) => params,
None => const_panic!(
None => panic!(
"cannot `transmute_ref!` or `transmute_mut!` between incompatible types"
),
};
Expand All @@ -1091,11 +1084,6 @@ mod cast_from {
}
}

// FIXME(#67): For some reason, on our MSRV toolchain, this `allow` isn't
// enforced despite having `#![allow(unknown_lints)]` at the crate root, but
// putting it here works. Once our MSRV is high enough that this bug has been
// fixed, remove this `allow`.
#[allow(unknown_lints)]
#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -1364,7 +1352,7 @@ mod tests {
/// call to `validate_cast_and_convert_metadata` panics with the given
/// panic message or, if the current Rust toolchain version is too
/// early to support panicking in `const fn`s, panics with *some*
/// message. In the latter case, the `const_panic!` macro is used,
/// message. In the latter case, the `panic!` macro is used,
/// which emits code which causes a non-panicking error at const eval
/// time, but which does panic when invoked at runtime. Thus, it is
/// merely difficult to predict the *value* of this panic. We deem
Expand Down
14 changes: 5 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ pub use crate::pointer::{invariant::BecauseImmutable, Maybe, Ptr};
//
// See the documentation on `util::polyfills` for more information.
#[allow(unused_imports)]
use crate::util::polyfills::{self, NonNullExt as _, NumExt as _};
use crate::util::polyfills::{self, NumExt as _};

#[cfg(all(test, not(__ZEROCOPY_INTERNAL_USE_ONLY_DEV_MODE)))]
const _: () = {
Expand Down Expand Up @@ -890,7 +890,7 @@ where
T: KnownLayout<PointerMetadata = usize>,
{
const SIZE_INFO: TrailingSliceLayout = match T::LAYOUT.size_info {
crate::SizeInfo::Sized { .. } => const_panic!("unreachable"),
crate::SizeInfo::Sized { .. } => panic!("unreachable"),
crate::SizeInfo::SliceDst(info) => info,
};
}
Expand Down Expand Up @@ -920,7 +920,7 @@ where
CastType::Prefix,
) {
Ok((elems, _)) => elems,
Err(_) => const_panic!("unreachable"),
Err(_) => panic!("unreachable"),
},
};
}
Expand Down Expand Up @@ -1056,8 +1056,6 @@ unsafe impl<T> KnownLayout for [T] {
// refers to an object with `elems` elements by construction.
#[inline(always)]
fn raw_from_ptr_len(data: NonNull<u8>, elems: usize) -> NonNull<Self> {
// FIXME(#67): Remove this allow. See NonNullExt for more details.
#[allow(unstable_name_collisions)]
NonNull::slice_from_raw_parts(data.cast::<T>(), elems)
}

Expand Down Expand Up @@ -1332,14 +1330,12 @@ where
}
}
};
const_assert!(is_infallible);
assert!(is_infallible);
is_infallible
};
}

const_assert!(
<Projection<Self, Field, I, VARIANT_ID, FIELD_ID> as IsInfallible>::IS_INFALLIBLE
);
assert!(<Projection<Self, Field, I, VARIANT_ID, FIELD_ID> as IsInfallible>::IS_INFALLIBLE);

Ok(())
}
Expand Down
11 changes: 5 additions & 6 deletions src/pointer/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ mod def {
}
}

#[allow(unreachable_pub)] // This is a false positive on our MSRV toolchain.
pub use def::Ptr;

/// External trait implementations on [`Ptr`].
Expand Down Expand Up @@ -1503,10 +1502,10 @@ mod tests {
}

test!(ZstDst, 8, 0, Some(0));
test!(ZstDst, 7, 0, None);
test!(ZstDst, 7, 0, None::<usize>);

test!(ZstDst, 8, usize::MAX, Some(usize::MAX));
test!(ZstDst, 7, usize::MAX, None);
test!(ZstDst, 7, usize::MAX, None::<usize>);

#[derive(KnownLayout, Immutable)]
#[repr(C)]
Expand All @@ -1516,15 +1515,15 @@ mod tests {
}

test!(Dst, 8, 0, Some(0));
test!(Dst, 7, 0, None);
test!(Dst, 7, 0, None::<usize>);

test!(Dst, 9, 1, Some(1));
test!(Dst, 8, 1, None);
test!(Dst, 8, 1, None::<usize>);

// If we didn't properly check for overflow, this would cause the
// metadata to overflow to 0, and thus the cast would spuriously
// succeed.
test!(Dst, 8, usize::MAX - 8 + 1, None);
test!(Dst, 8, usize::MAX - 8 + 1, None::<usize>);
}

#[test]
Expand Down
1 change: 0 additions & 1 deletion src/ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ mod def {
impl<B: CopyableByteSlice + Copy, T: ?Sized> Copy for Ref<B, T> {}
}

#[allow(unreachable_pub)] // This is a false positive on our MSRV toolchain.
pub use def::Ref;

use crate::pointer::{
Expand Down
13 changes: 2 additions & 11 deletions src/util/macro_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,7 @@ pub const ALIGNED_64K_ALLOCATION: NonNull<[u8]> = {
//
// FIXME(#429): Once `NonNull::new_unchecked` docs document that it
// preserves provenance, cite those docs.
// FIXME: Replace this `as` with `ptr.cast_mut()` once our MSRV >= 1.65
#[allow(clippy::as_conversions)]
unsafe {
NonNull::new_unchecked(ptr as *mut _)
}
unsafe { NonNull::new_unchecked(ptr.cast_mut()) }
};

/// Computes the offset of the base of the field `$trailing_field_name` within
Expand Down Expand Up @@ -729,12 +725,7 @@ impl<'a, Src, Dst> Wrap<&'a Src, &'a Dst> {
// - We know that the returned lifetime will not outlive the input
// lifetime thanks to the lifetime bounds on this function.
//
// FIXME(#67): Once our MSRV is 1.58, replace this `transmute` with
// `&*dst`.
#[allow(clippy::transmute_ptr_to_ref)]
unsafe {
mem::transmute(dst)
}
unsafe { &*dst }
}

#[inline(always)]
Expand Down
Loading
Loading