diff --git a/src/util/macros.rs b/src/util/macros.rs index da2c37798d..af751e7523 100644 --- a/src/util/macros.rs +++ b/src/util/macros.rs @@ -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 @@ -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!`. @@ -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 StaticAssert for T { const ASSERT: bool = { - const_assert!($condition); + const_assert!($condition $(, $args)*); $condition }; } const_assert!(::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 }; } @@ -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"); }} }