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

Experiment: force debug_assertions in consteval context #97467

Closed
Closed
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion library/core/src/char/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::convert::TryFrom;
use crate::fmt;
use crate::mem::transmute;
use crate::str::FromStr;
use crate::use_debug_assertions::use_debug_assertions;

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

#[stable(feature = "char_convert", since = "1.13.0")]
Expand Down
10 changes: 10 additions & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
#![feature(const_type_name)]
#![feature(const_default_impls)]
#![feature(const_unsafecell_get_mut)]
#![feature(consteval_debug_assertions)]
#![feature(core_panic)]
#![feature(duration_consts_float)]
#![feature(maybe_uninit_uninit_array)]
Expand Down Expand Up @@ -243,6 +244,15 @@ pub mod assert_matches {
pub use crate::macros::{assert_matches, debug_assert_matches};
}

// We don't export this through #[macro_export] for now, to avoid breakage.
// See https://github.com/rust-lang/rust/issues/82913
#[unstable(feature = "consteval_debug_assertions", issue = "none")]
/// Unstable module containing the unstable `use_debug_assertions` macro.
pub mod use_debug_assertions {
#[unstable(feature = "consteval_debug_assertions", issue = "none")]
pub use crate::macros::use_debug_assertions;
}

#[macro_use]
mod internal_macros;

Expand Down
37 changes: 31 additions & 6 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,29 @@ pub macro assert_matches {
},
}

/// Part of an experiment to enable debug assertions in consteval regardless of actual setting.
///
/// This returns `true` in consteval context and `cfg!(debug_assertions)` at runtime.
#[macro_export]
#[unstable(feature = "consteval_debug_assertions", issue = "none")]
#[allow_internal_unstable(const_eval_select)]
#[allow(unused_unsafe)]
#[rustc_macro_transparency = "semitransparent"]
pub macro use_debug_assertions() {{
// FIXME: Currently no unsafe hygiene inside macros; see https://github.com/rust-lang/rust/issues/74838
const fn use_debug_assertions() -> bool {
const fn always_true() -> bool {
true
}

// SAFETY: Code isn't going to rely on these values being the same, so, we're okay here.
unsafe {
$crate::intrinsics::const_eval_select((), always_true, || $crate::cfg!(debug_assertions))
}
}
use_debug_assertions()
}}

/// Asserts that a boolean expression is `true` at runtime.
///
/// This will invoke the [`panic!`] macro if the provided expression cannot be
Expand Down Expand Up @@ -212,10 +235,10 @@ pub macro assert_matches {
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "debug_assert_macro"]
#[allow_internal_unstable(edition_panic)]
#[allow_internal_unstable(edition_panic, consteval_debug_assertions)]
macro_rules! debug_assert {
($($arg:tt)*) => {
if $crate::cfg!(debug_assertions) {
if $crate::use_debug_assertions::use_debug_assertions!() {
$crate::assert!($($arg)*);
}
};
Expand Down Expand Up @@ -243,9 +266,10 @@ macro_rules! debug_assert {
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "debug_assert_eq_macro")]
#[allow_internal_unstable(consteval_debug_assertions)]
macro_rules! debug_assert_eq {
($($arg:tt)*) => {
if $crate::cfg!(debug_assertions) {
if $crate::use_debug_assertions::use_debug_assertions!() {
$crate::assert_eq!($($arg)*);
}
};
Expand Down Expand Up @@ -273,9 +297,10 @@ macro_rules! debug_assert_eq {
#[macro_export]
#[stable(feature = "assert_ne", since = "1.13.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "debug_assert_ne_macro")]
#[allow_internal_unstable(consteval_debug_assertions)]
macro_rules! debug_assert_ne {
($($arg:tt)*) => {
if $crate::cfg!(debug_assertions) {
if $crate::use_debug_assertions::use_debug_assertions!() {
$crate::assert_ne!($($arg)*);
}
};
Expand Down Expand Up @@ -314,10 +339,10 @@ macro_rules! debug_assert_ne {
/// ```
#[macro_export]
#[unstable(feature = "assert_matches", issue = "82775")]
#[allow_internal_unstable(assert_matches)]
#[allow_internal_unstable(assert_matches, consteval_debug_assertions)]
#[rustc_macro_transparency = "semitransparent"]
pub macro debug_assert_matches($($arg:tt)*) {
if $crate::cfg!(debug_assertions) {
if $crate::use_debug_assertions::use_debug_assertions!() {
$crate::assert_matches::assert_matches!($($arg)*);
}
}
Expand Down