@@ -168,6 +168,29 @@ pub macro assert_matches {
168
168
} ,
169
169
}
170
170
171
+ /// Part of an experiment to enable debug assertions in consteval regardless of actual setting.
172
+ ///
173
+ /// This returns `true` in consteval context and `cfg!(debug_assertions)` at runtime.
174
+ #[ macro_export]
175
+ #[ unstable ( feature = "consteval_debug_assertions" , issue = "none" ) ]
176
+ #[ allow_internal_unstable ( const_eval_select) ]
177
+ #[ allow ( unused_unsafe) ]
178
+ #[ rustc_macro_transparency = "semitransparent" ]
179
+ pub macro use_debug_assertions( ) { {
180
+ // FIXME: Currently no unsafe hygiene inside macros; see https://github.com/rust-lang/rust/issues/74838
181
+ const fn use_debug_assertions ( ) -> bool {
182
+ const fn always_true ( ) -> bool {
183
+ true
184
+ }
185
+
186
+ // SAFETY: Code isn't going to rely on these values being the same, so, we're okay here.
187
+ unsafe {
188
+ $crate:: intrinsics:: const_eval_select ( ( ) , always_true, || $crate:: cfg!( debug_assertions) )
189
+ }
190
+ }
191
+ use_debug_assertions ( )
192
+ } }
193
+
171
194
/// Asserts that a boolean expression is `true` at runtime.
172
195
///
173
196
/// This will invoke the [`panic!`] macro if the provided expression cannot be
@@ -212,10 +235,10 @@ pub macro assert_matches {
212
235
#[ macro_export]
213
236
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
214
237
#[ rustc_diagnostic_item = "debug_assert_macro" ]
215
- #[ allow_internal_unstable ( edition_panic) ]
238
+ #[ allow_internal_unstable( edition_panic, consteval_debug_assertions ) ]
216
239
macro_rules! debug_assert {
217
240
( $( $arg: tt) * ) => {
218
- if $crate:: cfg! ( debug_assertions ) {
241
+ if $crate:: use_debug_assertions :: use_debug_assertions! ( ) {
219
242
$crate:: assert!( $( $arg) * ) ;
220
243
}
221
244
} ;
@@ -243,9 +266,10 @@ macro_rules! debug_assert {
243
266
#[ macro_export]
244
267
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
245
268
#[ cfg_attr( not( test) , rustc_diagnostic_item = "debug_assert_eq_macro" ) ]
269
+ #[ allow_internal_unstable( consteval_debug_assertions) ]
246
270
macro_rules! debug_assert_eq {
247
271
( $( $arg: tt) * ) => {
248
- if $crate:: cfg! ( debug_assertions ) {
272
+ if $crate:: use_debug_assertions :: use_debug_assertions! ( ) {
249
273
$crate:: assert_eq!( $( $arg) * ) ;
250
274
}
251
275
} ;
@@ -273,9 +297,10 @@ macro_rules! debug_assert_eq {
273
297
#[ macro_export]
274
298
#[ stable( feature = "assert_ne" , since = "1.13.0" ) ]
275
299
#[ cfg_attr( not( test) , rustc_diagnostic_item = "debug_assert_ne_macro" ) ]
300
+ #[ allow_internal_unstable( consteval_debug_assertions) ]
276
301
macro_rules! debug_assert_ne {
277
302
( $( $arg: tt) * ) => {
278
- if $crate:: cfg! ( debug_assertions ) {
303
+ if $crate:: use_debug_assertions :: use_debug_assertions! ( ) {
279
304
$crate:: assert_ne!( $( $arg) * ) ;
280
305
}
281
306
} ;
@@ -314,10 +339,10 @@ macro_rules! debug_assert_ne {
314
339
/// ```
315
340
#[ macro_export]
316
341
#[ unstable( feature = "assert_matches" , issue = "82775" ) ]
317
- #[ allow_internal_unstable( assert_matches) ]
342
+ #[ allow_internal_unstable( assert_matches, consteval_debug_assertions ) ]
318
343
#[ rustc_macro_transparency = "semitransparent" ]
319
344
pub macro debug_assert_matches( $( $arg: tt) * ) {
320
- if $crate:: cfg! ( debug_assertions ) {
345
+ if $crate:: use_debug_assertions :: use_debug_assertions! ( ) {
321
346
$crate:: assert_matches:: assert_matches!( $( $arg) * ) ;
322
347
}
323
348
}
0 commit comments