Skip to content

Commit

Permalink
Improve ZST DST error message (#1873)
Browse files Browse the repository at this point in the history
Helps address #1867
  • Loading branch information
joshlf authored Oct 11, 2024
1 parent d42e46f commit aa9db4f
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/util/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,20 +638,20 @@ 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) => {{
#[cfg(zerocopy_panic_in_const_and_vec_try_reserve)]
panic!($fmt);
#[cfg(not(zerocopy_panic_in_const_and_vec_try_reserve))]
const_panic!(@non_panic $fmt)
}};
(@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] = [];
// This will always fail (since we're indexing into an array of size 0.
#[allow(unconditional_panic)]
panic[0]
}}
}};
($($arg:tt)+) => {{
#[cfg(zerocopy_panic_in_const_and_vec_try_reserve)]
panic!($($arg)+);
#[cfg(not(zerocopy_panic_in_const_and_vec_try_reserve))]
const_panic!(@non_panic $($arg)+)
}};
}

/// Either assert (if the current Rust toolchain supports panicking in `const
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 aa9db4f

Please sign in to comment.