Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8e25965

Browse files
committed
Use a new NullOp and add back checks for slices and copy
1 parent f87c833 commit 8e25965

File tree

27 files changed

+205
-122
lines changed

27 files changed

+205
-122
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19711971
ConstraintCategory::SizedBound,
19721972
);
19731973
}
1974+
&Rvalue::NullaryOp(NullOp::DebugAssertions, _) => {}
19741975

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

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 9 additions & 0 deletions
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()),

compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -438,14 +438,6 @@ fn codegen_regular_intrinsic_call<'tcx>(
438438
fx.bcx.ins().trap(TrapCode::User(0));
439439
return;
440440
}
441-
sym::debug_assertions => {
442-
let bool_layout = fx.layout_of(fx.tcx.types.bool);
443-
let val = CValue::by_val(
444-
fx.bcx.ins().iconst(types::I8, fx.tcx.sess.opts.debug_assertions as i64),
445-
bool_layout,
446-
);
447-
ret.write_cvalue(fx, val);
448-
}
449441
sym::likely | sym::unlikely => {
450442
intrinsic_args!(fx, args => (a); intrinsic);
451443

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ 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),
8887
sym::va_start => bx.va_start(args[0].immediate()),
8988
sym::va_end => bx.va_end(args[0].immediate()),
9089
sym::size_of_val => {

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 10 additions & 4 deletions
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),

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,6 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
537537
// not the optimization stage.)
538538
sym::is_val_statically_known => ecx.write_scalar(Scalar::from_bool(false), dest)?,
539539

540-
sym::debug_assertions => {
541-
ecx.write_scalar(Scalar::from_bool(ecx.tcx.sess.opts.debug_assertions), dest)?
542-
}
543-
544540
_ => {
545541
throw_unsup_format!(
546542
"intrinsic `{intrinsic_name}` is not supported at compile-time"

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,28 @@ 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+
// This should never actually lead us to any checks, because for the most
263+
// part those checks cannot be done in const-eval, and where they can be
264+
// done CTFE/Miri have better diagnostics.
265+
// But we implement the NullOp here to be correct I guess?
266+
let val = self.tcx.sess.opts.debug_assertions;
267+
Scalar::from_bool(val)
253268
}
254269
};
255-
self.write_scalar(Scalar::from_target_usize(val, self), &dest)?;
270+
self.write_scalar(val, &dest)?;
256271
}
257272

258273
ShallowInitBox(ref operand, _) => {

compiler/rustc_const_eval/src/transform/check_consts/check.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,10 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
571571

572572
Rvalue::Cast(_, _, _) => {}
573573

574-
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_), _) => {}
574+
Rvalue::NullaryOp(
575+
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::DebugAssertions,
576+
_,
577+
) => {}
575578
Rvalue::ShallowInitBox(_, _) => {}
576579

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

compiler/rustc_const_eval/src/transform/validate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11161116
Rvalue::Repeat(_, _)
11171117
| Rvalue::ThreadLocalRef(_)
11181118
| Rvalue::AddressOf(_, _)
1119-
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, _)
1119+
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::DebugAssertions, _)
11201120
| Rvalue::Discriminant(_) => {}
11211121
}
11221122
self.super_rvalue(rvalue, location);

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 1 addition & 0 deletions
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| {

0 commit comments

Comments
 (0)