Skip to content

Commit fb55d43

Browse files
committed
Experiment: force debug_assertions in consteval context
1 parent ebbcbfc commit fb55d43

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

library/core/src/char/convert.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::convert::TryFrom;
55
use crate::fmt;
66
use crate::mem::transmute;
77
use crate::str::FromStr;
8+
use crate::use_debug_assertions::use_debug_assertions;
89

910
/// Converts a `u32` to a `char`. See [`char::from_u32`].
1011
#[must_use]
@@ -23,7 +24,7 @@ pub(super) const fn from_u32(i: u32) -> Option<char> {
2324
#[must_use]
2425
pub(super) const unsafe fn from_u32_unchecked(i: u32) -> char {
2526
// SAFETY: the caller must guarantee that `i` is a valid char value.
26-
if cfg!(debug_assertions) { char::from_u32(i).unwrap() } else { unsafe { transmute(i) } }
27+
if use_debug_assertions!() { char::from_u32(i).unwrap() } else { unsafe { transmute(i) } }
2728
}
2829

2930
#[stable(feature = "char_convert", since = "1.13.0")]

library/core/src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
#![feature(const_type_name)]
145145
#![feature(const_default_impls)]
146146
#![feature(const_unsafecell_get_mut)]
147+
#![feature(consteval_debug_assertions)]
147148
#![feature(core_panic)]
148149
#![feature(duration_consts_float)]
149150
#![feature(maybe_uninit_uninit_array)]
@@ -243,6 +244,15 @@ pub mod assert_matches {
243244
pub use crate::macros::{assert_matches, debug_assert_matches};
244245
}
245246

247+
// We don't export this through #[macro_export] for now, to avoid breakage.
248+
// See https://github.com/rust-lang/rust/issues/82913
249+
#[unstable(feature = "consteval_debug_assertions", issue = "none")]
250+
/// Unstable module containing the unstable `use_debug_assertions` macro.
251+
pub mod use_debug_assertions {
252+
#[unstable(feature = "consteval_debug_assertions", issue = "none")]
253+
pub use crate::macros::use_debug_assertions;
254+
}
255+
246256
#[macro_use]
247257
mod internal_macros;
248258

library/core/src/macros/mod.rs

+31-6
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,29 @@ pub macro assert_matches {
168168
},
169169
}
170170

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+
171194
/// Asserts that a boolean expression is `true` at runtime.
172195
///
173196
/// This will invoke the [`panic!`] macro if the provided expression cannot be
@@ -212,10 +235,10 @@ pub macro assert_matches {
212235
#[macro_export]
213236
#[stable(feature = "rust1", since = "1.0.0")]
214237
#[rustc_diagnostic_item = "debug_assert_macro"]
215-
#[allow_internal_unstable(edition_panic)]
238+
#[allow_internal_unstable(edition_panic, consteval_debug_assertions)]
216239
macro_rules! debug_assert {
217240
($($arg:tt)*) => {
218-
if $crate::cfg!(debug_assertions) {
241+
if $crate::use_debug_assertions::use_debug_assertions!() {
219242
$crate::assert!($($arg)*);
220243
}
221244
};
@@ -243,9 +266,10 @@ macro_rules! debug_assert {
243266
#[macro_export]
244267
#[stable(feature = "rust1", since = "1.0.0")]
245268
#[cfg_attr(not(test), rustc_diagnostic_item = "debug_assert_eq_macro")]
269+
#[allow_internal_unstable(consteval_debug_assertions)]
246270
macro_rules! debug_assert_eq {
247271
($($arg:tt)*) => {
248-
if $crate::cfg!(debug_assertions) {
272+
if $crate::use_debug_assertions::use_debug_assertions!() {
249273
$crate::assert_eq!($($arg)*);
250274
}
251275
};
@@ -273,9 +297,10 @@ macro_rules! debug_assert_eq {
273297
#[macro_export]
274298
#[stable(feature = "assert_ne", since = "1.13.0")]
275299
#[cfg_attr(not(test), rustc_diagnostic_item = "debug_assert_ne_macro")]
300+
#[allow_internal_unstable(consteval_debug_assertions)]
276301
macro_rules! debug_assert_ne {
277302
($($arg:tt)*) => {
278-
if $crate::cfg!(debug_assertions) {
303+
if $crate::use_debug_assertions::use_debug_assertions!() {
279304
$crate::assert_ne!($($arg)*);
280305
}
281306
};
@@ -314,10 +339,10 @@ macro_rules! debug_assert_ne {
314339
/// ```
315340
#[macro_export]
316341
#[unstable(feature = "assert_matches", issue = "82775")]
317-
#[allow_internal_unstable(assert_matches)]
342+
#[allow_internal_unstable(assert_matches, consteval_debug_assertions)]
318343
#[rustc_macro_transparency = "semitransparent"]
319344
pub macro debug_assert_matches($($arg:tt)*) {
320-
if $crate::cfg!(debug_assertions) {
345+
if $crate::use_debug_assertions::use_debug_assertions!() {
321346
$crate::assert_matches::assert_matches!($($arg)*);
322347
}
323348
}

0 commit comments

Comments
 (0)