Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ZST DST error message #1873

Merged
merged 1 commit into from
Oct 11, 2024
Merged
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
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");
}}
}