Skip to content

Commit c1d6ee7

Browse files
committed
Toggle assert_unsafe_precondition in codegen instead of expansion
1 parent 11f32b7 commit c1d6ee7

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
8484
return;
8585
}
8686

87+
sym::debug_assertions => bx.const_bool(bx.tcx().sess.opts.debug_assertions),
8788
sym::va_start => bx.va_start(args[0].immediate()),
8889
sym::va_end => bx.va_end(args[0].immediate()),
8990
sym::size_of_val => {

compiler/rustc_const_eval/src/const_eval/machine.rs

+5
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,11 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
536536
// (We know the value here in the machine of course, but this is the runtime of that code,
537537
// not the optimization stage.)
538538
sym::is_val_statically_known => ecx.write_scalar(Scalar::from_bool(false), dest)?,
539+
540+
sym::debug_assertions => {
541+
ecx.write_scalar(Scalar::from_bool(ecx.tcx.sess.opts.debug_assertions), dest)?
542+
}
543+
539544
_ => {
540545
throw_unsup_format!(
541546
"intrinsic `{intrinsic_name}` is not supported at compile-time"

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: DefId) -> hir
112112
| sym::forget
113113
| sym::black_box
114114
| sym::variant_count
115-
| sym::ptr_mask => hir::Unsafety::Normal,
115+
| sym::ptr_mask
116+
| sym::debug_assertions => hir::Unsafety::Normal,
116117
_ => hir::Unsafety::Unsafe,
117118
};
118119

@@ -461,6 +462,8 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
461462
(0, vec![Ty::new_imm_ptr(tcx, Ty::new_unit(tcx))], tcx.types.usize)
462463
}
463464

465+
sym::debug_assertions => (0, Vec::new(), tcx.types.bool),
466+
464467
other => {
465468
tcx.dcx().emit_err(UnrecognizedIntrinsicFunction { span: it.span, name: other });
466469
return;

library/core/src/intrinsics.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -2569,6 +2569,12 @@ extern "rust-intrinsic" {
25692569
#[rustc_nounwind]
25702570
#[cfg(not(bootstrap))]
25712571
pub fn is_val_statically_known<T: Copy>(arg: T) -> bool;
2572+
2573+
#[rustc_const_unstable(feature = "delayed_debug_assertions", issue = "none")]
2574+
#[rustc_safe_intrinsic]
2575+
#[rustc_nounwind]
2576+
#[cfg(not(bootstrap))]
2577+
pub(crate) fn debug_assertions() -> bool;
25722578
}
25732579

25742580
// FIXME: Seems using `unstable` here completely ignores `rustc_allow_const_fn_unstable`
@@ -2604,10 +2610,17 @@ pub const unsafe fn is_val_statically_known<T: Copy>(_arg: T) -> bool {
26042610
///
26052611
/// So in a sense it is UB if this macro is useful, but we expect callers of `unsafe fn` to make
26062612
/// the occasional mistake, and this check should help them figure things out.
2607-
#[allow_internal_unstable(const_eval_select)] // permit this to be called in stably-const fn
2613+
#[allow_internal_unstable(const_eval_select, delayed_debug_assertions)] // permit this to be called in stably-const fn
26082614
macro_rules! assert_unsafe_precondition {
26092615
($name:expr, $([$($tt:tt)*])?($($i:ident:$ty:ty),*$(,)?) => $e:expr $(,)?) => {
2610-
if cfg!(debug_assertions) {
2616+
{
2617+
#[cfg(bootstrap)]
2618+
let should_check = cfg!(debug_assertions);
2619+
2620+
#[cfg(not(bootstrap))]
2621+
let should_check = ::core::intrinsics::debug_assertions();
2622+
2623+
if should_check {
26112624
// allow non_snake_case to allow capturing const generics
26122625
#[allow(non_snake_case)]
26132626
#[inline(always)]
@@ -2625,6 +2638,7 @@ macro_rules! assert_unsafe_precondition {
26252638

26262639
::core::intrinsics::const_eval_select(($($i,)*), comptime, runtime);
26272640
}
2641+
}
26282642
};
26292643
}
26302644
pub(crate) use assert_unsafe_precondition;

0 commit comments

Comments
 (0)