Skip to content

Commit 8836ac5

Browse files
committed
Add a new debug_assertions instrinsic (compiler)
And in clippy
1 parent 55fabf3 commit 8836ac5

File tree

18 files changed

+72
-13
lines changed

18 files changed

+72
-13
lines changed

Diff for: compiler/rustc_borrowck/src/type_check/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1984,6 +1984,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19841984
ConstraintCategory::SizedBound,
19851985
);
19861986
}
1987+
&Rvalue::NullaryOp(NullOp::DebugAssertions, _) => {}
19871988

19881989
Rvalue::ShallowInitBox(operand, ty) => {
19891990
self.check_operand(operand, location);

Diff for: compiler/rustc_codegen_cranelift/src/base.rs

+9
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,15 @@ fn codegen_stmt<'tcx>(
767767
NullOp::OffsetOf(fields) => {
768768
layout.offset_of_subfield(fx, fields.iter()).bytes()
769769
}
770+
NullOp::DebugAssertions => {
771+
let val = fx.tcx.sess.opts.debug_assertions;
772+
let val = CValue::by_val(
773+
fx.bcx.ins().iconst(types::I8, i64::try_from(val).unwrap()),
774+
fx.layout_of(fx.tcx.types.bool),
775+
);
776+
lval.write_cvalue(fx, val);
777+
return;
778+
}
770779
};
771780
let val = CValue::by_val(
772781
fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(val).unwrap()),

Diff for: compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -672,17 +672,23 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
672672
let val = match null_op {
673673
mir::NullOp::SizeOf => {
674674
assert!(bx.cx().type_is_sized(ty));
675-
layout.size.bytes()
675+
let val = layout.size.bytes();
676+
bx.cx().const_usize(val)
676677
}
677678
mir::NullOp::AlignOf => {
678679
assert!(bx.cx().type_is_sized(ty));
679-
layout.align.abi.bytes()
680+
let val = layout.align.abi.bytes();
681+
bx.cx().const_usize(val)
680682
}
681683
mir::NullOp::OffsetOf(fields) => {
682-
layout.offset_of_subfield(bx.cx(), fields.iter()).bytes()
684+
let val = layout.offset_of_subfield(bx.cx(), fields.iter()).bytes();
685+
bx.cx().const_usize(val)
686+
}
687+
mir::NullOp::DebugAssertions => {
688+
let val = bx.tcx().sess.opts.debug_assertions;
689+
bx.cx().const_bool(val)
683690
}
684691
};
685-
let val = bx.cx().const_usize(val);
686692
let tcx = self.cx.tcx();
687693
OperandRef {
688694
val: OperandValue::Immediate(val),

Diff for: compiler/rustc_const_eval/src/interpret/step.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,25 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
246246
);
247247
}
248248
let val = match null_op {
249-
mir::NullOp::SizeOf => layout.size.bytes(),
250-
mir::NullOp::AlignOf => layout.align.abi.bytes(),
249+
mir::NullOp::SizeOf => {
250+
let val = layout.size.bytes();
251+
Scalar::from_target_usize(val, self)
252+
}
253+
mir::NullOp::AlignOf => {
254+
let val = layout.align.abi.bytes();
255+
Scalar::from_target_usize(val, self)
256+
}
251257
mir::NullOp::OffsetOf(fields) => {
252-
layout.offset_of_subfield(self, fields.iter()).bytes()
258+
let val = layout.offset_of_subfield(self, fields.iter()).bytes();
259+
Scalar::from_target_usize(val, self)
260+
}
261+
mir::NullOp::DebugAssertions => {
262+
// The checks hidden behind this are always better done by the interpreter
263+
// itself, because it knows the runtime state better.
264+
Scalar::from_bool(false)
253265
}
254266
};
255-
self.write_scalar(Scalar::from_target_usize(val, self), &dest)?;
267+
self.write_scalar(val, &dest)?;
256268
}
257269

258270
ShallowInitBox(ref operand, _) => {

Diff for: compiler/rustc_const_eval/src/transform/check_consts/check.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,10 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
544544

545545
Rvalue::Cast(_, _, _) => {}
546546

547-
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_), _) => {}
547+
Rvalue::NullaryOp(
548+
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::DebugAssertions,
549+
_,
550+
) => {}
548551
Rvalue::ShallowInitBox(_, _) => {}
549552

550553
Rvalue::UnaryOp(_, operand) => {

Diff for: compiler/rustc_const_eval/src/transform/validate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11391139
Rvalue::Repeat(_, _)
11401140
| Rvalue::ThreadLocalRef(_)
11411141
| Rvalue::AddressOf(_, _)
1142-
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, _)
1142+
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::DebugAssertions, _)
11431143
| Rvalue::Discriminant(_) => {}
11441144
}
11451145
self.super_rvalue(rvalue, location);

Diff for: 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;

Diff for: compiler/rustc_middle/src/mir/pretty.rs

+1
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
907907
NullOp::SizeOf => write!(fmt, "SizeOf({t})"),
908908
NullOp::AlignOf => write!(fmt, "AlignOf({t})"),
909909
NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({t}, {fields:?})"),
910+
NullOp::DebugAssertions => write!(fmt, "cfg!(debug_assertions)"),
910911
}
911912
}
912913
ThreadLocalRef(did) => ty::tls::with(|tcx| {

Diff for: compiler/rustc_middle/src/mir/syntax.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,8 @@ pub enum NullOp<'tcx> {
13611361
AlignOf,
13621362
/// Returns the offset of a field
13631363
OffsetOf(&'tcx List<(VariantIdx, FieldIdx)>),
1364+
/// cfg!(debug_assertions), but expanded in codegen
1365+
DebugAssertions,
13641366
}
13651367

13661368
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]

Diff for: compiler/rustc_middle/src/mir/tcx.rs

+1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ impl<'tcx> Rvalue<'tcx> {
194194
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
195195
tcx.types.usize
196196
}
197+
Rvalue::NullaryOp(NullOp::DebugAssertions, _) => tcx.types.bool,
197198
Rvalue::Aggregate(ref ak, ref ops) => match **ak {
198199
AggregateKind::Array(ty) => Ty::new_array(tcx, ty, ops.len() as u64),
199200
AggregateKind::Tuple => {

Diff for: compiler/rustc_mir_dataflow/src/move_paths/builder.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,10 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
429429
| Rvalue::AddressOf(..)
430430
| Rvalue::Discriminant(..)
431431
| Rvalue::Len(..)
432-
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {}
432+
| Rvalue::NullaryOp(
433+
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..) | NullOp::DebugAssertions,
434+
_,
435+
) => {}
433436
}
434437
}
435438

Diff for: compiler/rustc_mir_transform/src/const_prop_lint.rs

+1
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
639639
NullOp::OffsetOf(fields) => {
640640
op_layout.offset_of_subfield(self, fields.iter()).bytes()
641641
}
642+
NullOp::DebugAssertions => return None,
642643
};
643644
ImmTy::from_scalar(Scalar::from_target_usize(val, self), layout).into()
644645
}

Diff for: compiler/rustc_mir_transform/src/gvn.rs

+1
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
489489
NullOp::OffsetOf(fields) => {
490490
layout.offset_of_subfield(&self.ecx, fields.iter()).bytes()
491491
}
492+
NullOp::DebugAssertions => return None,
492493
};
493494
let usize_layout = self.ecx.layout_of(self.tcx.types.usize).unwrap();
494495
let imm = ImmTy::try_from_uint(val, usize_layout)?;

Diff for: compiler/rustc_mir_transform/src/lower_intrinsics.rs

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ impl<'tcx> MirPass<'tcx> for LowerIntrinsics {
2121
sym::unreachable => {
2222
terminator.kind = TerminatorKind::Unreachable;
2323
}
24+
sym::debug_assertions => {
25+
let target = target.unwrap();
26+
block.statements.push(Statement {
27+
source_info: terminator.source_info,
28+
kind: StatementKind::Assign(Box::new((
29+
*destination,
30+
Rvalue::NullaryOp(NullOp::DebugAssertions, tcx.types.bool),
31+
))),
32+
});
33+
terminator.kind = TerminatorKind::Goto { target };
34+
}
2435
sym::forget => {
2536
if let Some(target) = *target {
2637
block.statements.push(Statement {

Diff for: compiler/rustc_mir_transform/src/promote_consts.rs

+1
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ impl<'tcx> Validator<'_, 'tcx> {
446446
NullOp::SizeOf => {}
447447
NullOp::AlignOf => {}
448448
NullOp::OffsetOf(_) => {}
449+
NullOp::DebugAssertions => {}
449450
},
450451

451452
Rvalue::ShallowInitBox(_, _) => return Err(Unpromotable),

Diff for: compiler/rustc_smir/src/rustc_smir/convert/mir.rs

+1
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ impl<'tcx> Stable<'tcx> for mir::NullOp<'tcx> {
257257
OffsetOf(indices) => stable_mir::mir::NullOp::OffsetOf(
258258
indices.iter().map(|idx| idx.stable(tables)).collect(),
259259
),
260+
DebugAssertions => stable_mir::mir::NullOp::DebugAssertions,
260261
}
261262
}
262263
}

Diff for: compiler/stable_mir/src/mir/body.rs

+3
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ impl Rvalue {
639639
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
640640
Ok(Ty::usize_ty())
641641
}
642+
Rvalue::NullaryOp(NullOp::DebugAssertions, _) => Ok(Ty::bool_ty()),
642643
Rvalue::Aggregate(ak, ops) => match *ak {
643644
AggregateKind::Array(ty) => Ty::try_new_array(ty, ops.len() as u64),
644645
AggregateKind::Tuple => Ok(Ty::new_tuple(
@@ -1005,6 +1006,8 @@ pub enum NullOp {
10051006
AlignOf,
10061007
/// Returns the offset of a field.
10071008
OffsetOf(Vec<(VariantIdx, FieldIdx)>),
1009+
/// cfg!(debug_assertions), but at codegen time
1010+
DebugAssertions,
10081011
}
10091012

10101013
impl Operand {

Diff for: src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ fn check_rvalue<'tcx>(
174174
))
175175
}
176176
},
177-
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_), _) | Rvalue::ShallowInitBox(_, _) => {
177+
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::DebugAssertions, _) | Rvalue::ShallowInitBox(_, _) => {
178178
Ok(())
179179
},
180180
Rvalue::UnaryOp(_, operand) => {

0 commit comments

Comments
 (0)