Skip to content

Commit d6ce3c9

Browse files
committed
Auto merge of #148766 - cjgillot:mir-const-runtime-checks, r=RalfJung,saethlin
Replace Rvalue::NullaryOp by a variant in mir::Operand. Based on rust-lang/rust#148151 This PR fully removes the MIR `Rvalue::NullaryOp`. After rust-lang/rust#148151, it was only useful for runtime checks like `ub_checks`, `contract_checks` and `overflow_checks`. These are "runtime" checks, boolean constants that may only be `true` in codegen. It depends on a rustc flag passed to codegen, so we need to represent those flags cross-crate. This PR replaces those runtime checks by special variants in MIR `ConstValue`. This allows code that expects constants to manipulate those as such, even if we may not always be able to evaluate them to actual scalars.
2 parents 2fc869a + 95fca4a commit d6ce3c9

4 files changed

Lines changed: 20 additions & 33 deletions

File tree

rustc_public/src/mir/body.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -587,9 +587,6 @@ pub enum Rvalue {
587587
/// nature of this operation?
588588
ThreadLocalRef(crate::CrateItem),
589589

590-
/// Computes a value as described by the operation.
591-
NullaryOp(NullOp),
592-
593590
/// Exactly like `BinaryOp`, but less operands.
594591
///
595592
/// Also does two's-complement arithmetic. Negation requires a signed integer or a float;
@@ -641,7 +638,6 @@ impl Rvalue {
641638
.discriminant_ty()
642639
.ok_or_else(|| error!("Expected a `RigidTy` but found: {place_ty:?}"))
643640
}
644-
Rvalue::NullaryOp(NullOp::RuntimeChecks(_)) => Ok(Ty::bool_ty()),
645641
Rvalue::Aggregate(ak, ops) => match *ak {
646642
AggregateKind::Array(ty) => Ty::try_new_array(ty, ops.len() as u64),
647643
AggregateKind::Tuple => Ok(Ty::new_tuple(
@@ -677,6 +673,7 @@ pub enum Operand {
677673
Copy(Place),
678674
Move(Place),
679675
Constant(ConstOperand),
676+
RuntimeChecks(RuntimeChecks),
680677
}
681678

682679
#[derive(Clone, Eq, PartialEq, Hash, Serialize)]
@@ -699,6 +696,16 @@ pub struct ConstOperand {
699696
pub const_: MirConst,
700697
}
701698

699+
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)]
700+
pub enum RuntimeChecks {
701+
/// cfg!(ub_checks), but at codegen time
702+
UbChecks,
703+
/// cfg!(contract_checks), but at codegen time
704+
ContractChecks,
705+
/// cfg!(overflow_checks), but at codegen time
706+
OverflowChecks,
707+
}
708+
702709
/// Debug information pertaining to a user variable.
703710
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
704711
pub struct VarDebugInfo {
@@ -1018,22 +1025,6 @@ pub enum CastKind {
10181025
Subtype,
10191026
}
10201027

1021-
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)]
1022-
pub enum NullOp {
1023-
/// Codegen conditions for runtime checks.
1024-
RuntimeChecks(RuntimeChecks),
1025-
}
1026-
1027-
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)]
1028-
pub enum RuntimeChecks {
1029-
/// cfg!(ub_checks), but at codegen time
1030-
UbChecks,
1031-
/// cfg!(contract_checks), but at codegen time
1032-
ContractChecks,
1033-
/// cfg!(overflow_checks), but at codegen time
1034-
OverflowChecks,
1035-
}
1036-
10371028
impl Operand {
10381029
/// Get the type of an operand relative to the local declaration.
10391030
///
@@ -1045,6 +1036,7 @@ impl Operand {
10451036
match self {
10461037
Operand::Copy(place) | Operand::Move(place) => place.ty(locals),
10471038
Operand::Constant(c) => Ok(c.ty()),
1039+
Operand::RuntimeChecks(_) => Ok(Ty::bool_ty()),
10481040
}
10491041
}
10501042
}

rustc_public/src/mir/pretty.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ fn pretty_operand(operand: &Operand) -> String {
332332
format!("move {mv:?}")
333333
}
334334
Operand::Constant(cnst) => pretty_mir_const(&cnst.const_),
335+
Operand::RuntimeChecks(checks) => format!("{checks:?}"),
335336
}
336337
}
337338

@@ -386,9 +387,6 @@ fn pretty_rvalue<W: Write>(writer: &mut W, rval: &Rvalue) -> io::Result<()> {
386387
Rvalue::ThreadLocalRef(item) => {
387388
write!(writer, "thread_local_ref{item:?}")
388389
}
389-
Rvalue::NullaryOp(nul) => {
390-
write!(writer, "{nul:?}() \" \"")
391-
}
392390
Rvalue::UnaryOp(un, op) => {
393391
write!(writer, "{:?}({})", un, pretty_operand(op))
394392
}

rustc_public/src/mir/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ macro_rules! make_mir_visitor {
282282
self.visit_operand(op, location)
283283
}
284284
Rvalue::ThreadLocalRef(_) => {}
285-
Rvalue::NullaryOp(_) => {}
286285
Rvalue::UnaryOp(_, op) | Rvalue::Use(op) => {
287286
self.visit_operand(op, location);
288287
}
@@ -297,6 +296,7 @@ macro_rules! make_mir_visitor {
297296
Operand::Constant(constant) => {
298297
self.visit_const_operand(constant, location);
299298
}
299+
Operand::RuntimeChecks(_) => {}
300300
}
301301
}
302302

rustc_public/src/unstable/convert/stable/mir.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ impl<'tcx> Stable<'tcx> for mir::Rvalue<'tcx> {
232232
)
233233
}
234234
}
235-
NullaryOp(null_op) => crate::mir::Rvalue::NullaryOp(null_op.stable(tables, cx)),
236235
UnaryOp(un_op, op) => {
237236
crate::mir::Rvalue::UnaryOp(un_op.stable(tables, cx), op.stable(tables, cx))
238237
}
@@ -312,21 +311,18 @@ impl<'tcx> Stable<'tcx> for mir::FakeBorrowKind {
312311
}
313312
}
314313

315-
impl<'tcx> Stable<'tcx> for mir::NullOp {
316-
type T = crate::mir::NullOp;
314+
impl<'tcx> Stable<'tcx> for mir::RuntimeChecks {
315+
type T = crate::mir::RuntimeChecks;
317316
fn stable<'cx>(
318317
&self,
319318
_: &mut Tables<'cx, BridgeTys>,
320319
_: &CompilerCtxt<'cx, BridgeTys>,
321320
) -> Self::T {
322-
use rustc_middle::mir::NullOp::*;
323321
use rustc_middle::mir::RuntimeChecks::*;
324322
match self {
325-
RuntimeChecks(op) => crate::mir::NullOp::RuntimeChecks(match op {
326-
UbChecks => crate::mir::RuntimeChecks::UbChecks,
327-
ContractChecks => crate::mir::RuntimeChecks::ContractChecks,
328-
OverflowChecks => crate::mir::RuntimeChecks::OverflowChecks,
329-
}),
323+
UbChecks => crate::mir::RuntimeChecks::UbChecks,
324+
ContractChecks => crate::mir::RuntimeChecks::ContractChecks,
325+
OverflowChecks => crate::mir::RuntimeChecks::OverflowChecks,
330326
}
331327
}
332328
}
@@ -383,6 +379,7 @@ impl<'tcx> Stable<'tcx> for mir::Operand<'tcx> {
383379
Copy(place) => crate::mir::Operand::Copy(place.stable(tables, cx)),
384380
Move(place) => crate::mir::Operand::Move(place.stable(tables, cx)),
385381
Constant(c) => crate::mir::Operand::Constant(c.stable(tables, cx)),
382+
RuntimeChecks(c) => crate::mir::Operand::RuntimeChecks(c.stable(tables, cx)),
386383
}
387384
}
388385
}

0 commit comments

Comments
 (0)