diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index 3f58fc448aa68..24d6d256a9215 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -291,7 +291,7 @@ pub macro cfg_select($($tt:tt)*) { #[allow_internal_unstable(edition_panic)] macro_rules! debug_assert { ($($arg:tt)*) => { - if $crate::cfg!(debug_assertions) { + #[cfg(debug_assertions)] { $crate::assert!($($arg)*); } }; @@ -321,7 +321,7 @@ macro_rules! debug_assert { #[rustc_diagnostic_item = "debug_assert_eq_macro"] macro_rules! debug_assert_eq { ($($arg:tt)*) => { - if $crate::cfg!(debug_assertions) { + #[cfg(debug_assertions)] { $crate::assert_eq!($($arg)*); } }; @@ -351,7 +351,7 @@ macro_rules! debug_assert_eq { #[rustc_diagnostic_item = "debug_assert_ne_macro"] macro_rules! debug_assert_ne { ($($arg:tt)*) => { - if $crate::cfg!(debug_assertions) { + #[cfg(debug_assertions)] { $crate::assert_ne!($($arg)*); } }; @@ -403,7 +403,7 @@ macro_rules! debug_assert_ne { #[allow_internal_unstable(assert_matches)] #[rustc_macro_transparency = "semitransparent"] pub macro debug_assert_matches($($arg:tt)*) { - if $crate::cfg!(debug_assertions) { + #[cfg(debug_assertions)] { $crate::assert_matches::assert_matches!($($arg)*); } } diff --git a/tests/ui/macros/debug-assertation-debug-check.rs b/tests/ui/macros/debug-assertation-debug-check.rs new file mode 100644 index 0000000000000..9d195a5dc1317 --- /dev/null +++ b/tests/ui/macros/debug-assertation-debug-check.rs @@ -0,0 +1,13 @@ +//@ compile-flags: -C opt-level=3 +//@ check-pass + +fn main() { + let one = Thing; + let two = Thing; + + debug_assert_eq!(one, two); +} + +#[derive(Debug)] +#[cfg_attr(debug_assertions, derive(PartialEq))] +struct Thing; diff --git a/tests/ui/macros/debug-assertation-release-check.rs b/tests/ui/macros/debug-assertation-release-check.rs new file mode 100644 index 0000000000000..980a40544f25d --- /dev/null +++ b/tests/ui/macros/debug-assertation-release-check.rs @@ -0,0 +1,13 @@ +//@ compile-flags: -C opt-level=0 +//@ check-pass + +fn main() { + let one = Thing; + let two = Thing; + + debug_assert_eq!(one, two); +} + +#[derive(Debug)] +#[cfg_attr(debug_assertions, derive(PartialEq))] +struct Thing;