Skip to content

Commit

Permalink
Improve ZST DST error message
Browse files Browse the repository at this point in the history
Helps address #1867
  • Loading branch information
joshlf committed Oct 11, 2024
1 parent d42e46f commit 3d640c8
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/util/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,13 +638,13 @@ macro_rules! maybe_const_trait_bounded_fn {
/// The type that this expression evaluates to must be `Copy`, or else the
/// non-panicking desugaring will fail to compile.
macro_rules! const_panic {
($fmt:literal) => {{
($($arg:tt)+) => {{
#[cfg(zerocopy_panic_in_const_and_vec_try_reserve)]
panic!($fmt);
panic!($($arg)+);
#[cfg(not(zerocopy_panic_in_const_and_vec_try_reserve))]
const_panic!(@non_panic $fmt)
const_panic!(@non_panic $($arg)+)
}};
(@non_panic $fmt:expr) => {{
(@non_panic $($_arg:tt)+) => {{
// This will type check to whatever type is expected based on the call
// site.
let panic: [_; 0] = [];
Expand All @@ -669,7 +669,18 @@ macro_rules! const_assert {
let _: () = const_panic!(@non_panic concat!("assertion failed: ", stringify!($e)));
}
}
}}
}};
($e:expr, $($args:tt)+) => {{
#[cfg(zerocopy_panic_in_const_and_vec_try_reserve)]
assert!($e, $($args)+);
#[cfg(not(zerocopy_panic_in_const_and_vec_try_reserve))]
{
let e = $e;
if !e {
let _: () = const_panic!(@non_panic concat!("assertion failed: ", stringify!($e), ": ", stringify!($arg)), $($args)*);
}
}
}};
}

/// Like `const_assert!`, but relative to `debug_assert!`.
Expand Down Expand Up @@ -709,28 +720,28 @@ macro_rules! const_unreachable {
/// it cannot be evaluated in a runtime context. The condition is checked after
/// monomorphization and, upon failure, emits a compile error.
macro_rules! static_assert {
(Self $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )? => $condition:expr) => {{
(Self $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )? => $condition:expr $(, $args:tt)*) => {{
trait StaticAssert {
const ASSERT: bool;
}

impl<T $(: $(? $optbound +)* $($bound +)*)?> StaticAssert for T {
const ASSERT: bool = {
const_assert!($condition);
const_assert!($condition $(, $args)*);
$condition
};
}

const_assert!(<Self as StaticAssert>::ASSERT);
}};
($($tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?),* => $condition:expr) => {{
($($tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?),* => $condition:expr $(, $args:tt)*) => {{
trait StaticAssert {
const ASSERT: bool;
}

impl<$($tyvar $(: $(? $optbound +)* $($bound +)*)?,)*> StaticAssert for ($($tyvar,)*) {
const ASSERT: bool = {
const_assert!($condition);
const_assert!($condition $(, $args)*);
$condition
};
}
Expand All @@ -752,6 +763,6 @@ macro_rules! static_assert_dst_is_not_zst {
}
};
!dst_is_zst
});
}, "cannot call this method on a dynamically-sized type whose trailing slice element is zero-sized");
}}
}

0 comments on commit 3d640c8

Please sign in to comment.