Skip to content

Commit a7912cb

Browse files
committed
Put checks that detect UB under their own flag below debug_assertions
1 parent 83d0a94 commit a7912cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+206
-64
lines changed

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ fn codegen_stmt<'tcx>(
789789
layout.offset_of_subfield(fx, fields.iter()).bytes()
790790
}
791791
NullOp::UbChecks => {
792-
let val = fx.tcx.sess.opts.debug_assertions;
792+
let val = fx.tcx.sess.ub_checks();
793793
let val = CValue::by_val(
794794
fx.bcx.ins().iconst(types::I8, i64::try_from(val).unwrap()),
795795
fx.layout_of(fx.tcx.types.bool),

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
682682
bx.cx().const_usize(val)
683683
}
684684
mir::NullOp::UbChecks => {
685-
let val = bx.tcx().sess.opts.debug_assertions;
685+
let val = bx.tcx().sess.ub_checks();
686686
bx.cx().const_bool(val)
687687
}
688688
};

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
258258
let val = layout.offset_of_subfield(self, fields.iter()).bytes();
259259
Scalar::from_target_usize(val, self)
260260
}
261-
mir::NullOp::UbChecks => Scalar::from_bool(self.tcx.sess.opts.debug_assertions),
261+
mir::NullOp::UbChecks => Scalar::from_bool(self.tcx.sess.ub_checks()),
262262
};
263263
self.write_scalar(val, &dest)?;
264264
}

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub type GatedCfg = (Symbol, Symbol, GateFn);
2525
const GATED_CFGS: &[GatedCfg] = &[
2626
// (name in cfg, feature, function to check if the feature is enabled)
2727
(sym::overflow_checks, sym::cfg_overflow_checks, cfg_fn!(cfg_overflow_checks)),
28+
(sym::ub_checks, sym::cfg_ub_checks, cfg_fn!(cfg_ub_checks)),
2829
(sym::target_thread_local, sym::cfg_target_thread_local, cfg_fn!(cfg_target_thread_local)),
2930
(
3031
sym::target_has_atomic_equal_alignment,

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ declare_features! (
381381
(unstable, cfg_target_has_atomic_equal_alignment, "1.60.0", Some(93822)),
382382
/// Allows `cfg(target_thread_local)`.
383383
(unstable, cfg_target_thread_local, "1.7.0", Some(29594)),
384+
/// Allows the use of `#[cfg(ub_checks)` to check if UB checks are enabled.
385+
(unstable, cfg_ub_checks, "CURRENT_RUSTC_VERSION", Some(123499)),
384386
/// Allow conditional compilation depending on rust version
385387
(unstable, cfg_version, "1.45.0", Some(64796)),
386388
/// Allows to use the `#[cfi_encoding = ""]` attribute.

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ fn test_unstable_options_tracking_hash() {
846846
tracked!(trap_unreachable, Some(false));
847847
tracked!(treat_err_as_bug, NonZero::new(1));
848848
tracked!(tune_cpu, Some(String::from("abc")));
849+
tracked!(ub_checks, Some(false));
849850
tracked!(uninit_const_chunk_threshold, 123);
850851
tracked!(unleash_the_miri_inside_of_you, true);
851852
tracked!(use_ctors_section, Some(true));

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,8 @@ impl<'tcx> Body<'tcx> {
777777
// _1 = const _
778778
// SwitchInt(_1)
779779
//
780-
// And MIR for if intrinsics::debug_assertions() looks like this:
781-
// _1 = cfg!(debug_assertions)
780+
// And MIR for if intrinsics::ub_checks() looks like this:
781+
// _1 = UbChecks()
782782
// SwitchInt(_1)
783783
//
784784
// So we're going to try to recognize this pattern.
@@ -799,9 +799,7 @@ impl<'tcx> Body<'tcx> {
799799
}
800800

801801
match rvalue {
802-
Rvalue::NullaryOp(NullOp::UbChecks, _) => {
803-
Some((tcx.sess.opts.debug_assertions as u128, targets))
804-
}
802+
Rvalue::NullaryOp(NullOp::UbChecks, _) => Some((tcx.sess.ub_checks() as u128, targets)),
805803
Rvalue::Use(Operand::Constant(constant)) => {
806804
let bits = eval_mono_const(constant);
807805
Some((bits, targets))

compiler/rustc_mir_transform/src/check_alignment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl<'tcx> MirPass<'tcx> for CheckAlignment {
1616
if sess.target.llvm_target == "i686-pc-windows-msvc" {
1717
return false;
1818
}
19-
sess.opts.debug_assertions
19+
sess.ub_checks()
2020
}
2121

2222
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/instsimplify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'tcx> InstSimplifyContext<'tcx, '_> {
149149

150150
fn simplify_ub_check(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
151151
if let Rvalue::NullaryOp(NullOp::UbChecks, _) = *rvalue {
152-
let const_ = Const::from_bool(self.tcx, self.tcx.sess.opts.debug_assertions);
152+
let const_ = Const::from_bool(self.tcx, self.tcx.sess.ub_checks());
153153
let constant = ConstOperand { span: source_info.span, const_, user_ty: None };
154154
*rvalue = Rvalue::Use(Operand::Constant(Box::new(constant)));
155155
}

compiler/rustc_session/src/config/cfg.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ pub(crate) fn default_configuration(sess: &Session) -> Cfg {
212212
ins_none!(sym::test);
213213
}
214214

215+
if sess.ub_checks() {
216+
ins_none!(sym::ub_checks);
217+
}
218+
215219
ret
216220
}
217221

@@ -367,6 +371,8 @@ impl CheckCfg {
367371

368372
ins!(sym::test, no_values);
369373

374+
ins!(sym::ub_checks, no_values);
375+
370376
ins!(sym::unix, no_values);
371377
ins!(sym::windows, no_values);
372378
}

0 commit comments

Comments
 (0)