Skip to content

Commit 33d124f

Browse files
Move Assert to StatementKind
1 parent 0d40f89 commit 33d124f

File tree

8 files changed

+119
-124
lines changed

8 files changed

+119
-124
lines changed

src/librustc/mir/mod.rs

Lines changed: 56 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -517,16 +517,6 @@ pub enum TerminatorKind<'tcx> {
517517
/// Cleanups to be done if the call unwinds.
518518
cleanup: Option<Block>
519519
},
520-
521-
/// Jump to the target if the condition has the expected value,
522-
/// otherwise panic with a message and a cleanup target.
523-
Assert {
524-
cond: Operand<'tcx>,
525-
expected: bool,
526-
msg: AssertMessage<'tcx>,
527-
target: Block,
528-
cleanup: Option<Block>
529-
}
530520
}
531521

532522
impl<'tcx> Terminator<'tcx> {
@@ -572,8 +562,6 @@ impl<'tcx> TerminatorKind<'tcx> {
572562
Drop { ref target, unwind: None, .. } => {
573563
slice::ref_slice(target).into_cow()
574564
}
575-
Assert { target, cleanup: Some(unwind), .. } => vec![target, unwind].into_cow(),
576-
Assert { ref target, .. } => slice::ref_slice(target).into_cow(),
577565
}
578566
}
579567

@@ -597,8 +585,6 @@ impl<'tcx> TerminatorKind<'tcx> {
597585
Drop { ref mut target, unwind: None, .. } => {
598586
vec![target]
599587
}
600-
Assert { ref mut target, cleanup: Some(ref mut unwind), .. } => vec![target, unwind],
601-
Assert { ref mut target, .. } => vec![target]
602588
}
603589
}
604590
}
@@ -680,26 +666,6 @@ impl<'tcx> TerminatorKind<'tcx> {
680666
}
681667
write!(fmt, ")")
682668
}
683-
Assert { ref cond, expected, ref msg, .. } => {
684-
write!(fmt, "assert(")?;
685-
if !expected {
686-
write!(fmt, "!")?;
687-
}
688-
write!(fmt, "{:?}, ", cond)?;
689-
690-
match *msg {
691-
AssertMessage::BoundsCheck { ref len, ref index } => {
692-
write!(fmt, "{:?}, {:?}, {:?}",
693-
"index out of bounds: the len is {} but the index is {}",
694-
len, index)?;
695-
}
696-
AssertMessage::Math(ref err) => {
697-
write!(fmt, "{:?}", err.description())?;
698-
}
699-
}
700-
701-
write!(fmt, ")")
702-
}
703669
}
704670
}
705671

@@ -730,9 +696,6 @@ impl<'tcx> TerminatorKind<'tcx> {
730696
Drop { unwind: Some(_), .. } => {
731697
vec!["return".into_cow(), "unwind".into_cow()]
732698
}
733-
Assert { cleanup: None, .. } => vec!["".into()],
734-
Assert { .. } =>
735-
vec!["success".into_cow(), "unwind".into_cow()]
736699
}
737700
}
738701
}
@@ -783,6 +746,15 @@ pub enum StatementKind<'tcx> {
783746
inputs: Vec<Operand<'tcx>>
784747
},
785748

749+
/// Jump to the target if the condition has the expected value,
750+
/// otherwise panic with a message and a cleanup target.
751+
Assert {
752+
cond: Operand<'tcx>,
753+
expected: bool,
754+
msg: AssertMessage<'tcx>,
755+
cleanup: Option<Block>
756+
},
757+
786758
/// No-op. Useful for deleting instructions without affecting statement indices.
787759
Nop,
788760
}
@@ -800,6 +772,26 @@ impl<'tcx> Debug for Statement<'tcx> {
800772
InlineAsm { ref asm, ref outputs, ref inputs } => {
801773
write!(fmt, "asm!({:?} : {:?} : {:?})", asm, outputs, inputs)
802774
},
775+
Assert { ref cond, expected, ref msg, .. } => {
776+
write!(fmt, "assert(")?;
777+
if !expected {
778+
write!(fmt, "!")?;
779+
}
780+
write!(fmt, "{:?}, ", cond)?;
781+
782+
match *msg {
783+
AssertMessage::BoundsCheck { ref len, ref index } => {
784+
write!(fmt, "{:?}, {:?}, {:?}",
785+
"index out of bounds: the len is {} but the index is {}",
786+
len, index)?;
787+
}
788+
AssertMessage::Math(ref err) => {
789+
write!(fmt, "{:?}", err.description())?;
790+
}
791+
}
792+
793+
write!(fmt, ")")
794+
},
803795
Nop => write!(fmt, "nop"),
804796
}
805797
}
@@ -1423,6 +1415,22 @@ impl<'tcx> TypeFoldable<'tcx> for Statement<'tcx> {
14231415
outputs: outputs.fold_with(folder),
14241416
inputs: inputs.fold_with(folder)
14251417
},
1418+
Assert { ref cond, expected, ref msg, cleanup } => {
1419+
let msg = if let AssertMessage::BoundsCheck { ref len, ref index } = *msg {
1420+
AssertMessage::BoundsCheck {
1421+
len: len.fold_with(folder),
1422+
index: index.fold_with(folder),
1423+
}
1424+
} else {
1425+
msg.clone()
1426+
};
1427+
Assert {
1428+
cond: cond.fold_with(folder),
1429+
expected: expected,
1430+
msg: msg,
1431+
cleanup: cleanup
1432+
}
1433+
},
14261434
Nop => Nop,
14271435
};
14281436
Statement {
@@ -1441,6 +1449,17 @@ impl<'tcx> TypeFoldable<'tcx> for Statement<'tcx> {
14411449
StorageDead(ref lvalue) => lvalue.visit_with(visitor),
14421450
InlineAsm { ref outputs, ref inputs, .. } =>
14431451
outputs.visit_with(visitor) || inputs.visit_with(visitor),
1452+
Assert { ref cond, ref msg, .. } => {
1453+
if cond.visit_with(visitor) {
1454+
if let AssertMessage::BoundsCheck { ref len, ref index } = *msg {
1455+
len.visit_with(visitor) || index.visit_with(visitor)
1456+
} else {
1457+
false
1458+
}
1459+
} else {
1460+
false
1461+
}
1462+
},
14441463
Nop => false,
14451464
}
14461465
}
@@ -1481,23 +1500,6 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
14811500
cleanup: cleanup
14821501
}
14831502
},
1484-
Assert { ref cond, expected, ref msg, target, cleanup } => {
1485-
let msg = if let AssertMessage::BoundsCheck { ref len, ref index } = *msg {
1486-
AssertMessage::BoundsCheck {
1487-
len: len.fold_with(folder),
1488-
index: index.fold_with(folder),
1489-
}
1490-
} else {
1491-
msg.clone()
1492-
};
1493-
Assert {
1494-
cond: cond.fold_with(folder),
1495-
expected: expected,
1496-
msg: msg,
1497-
target: target,
1498-
cleanup: cleanup
1499-
}
1500-
},
15011503
Resume => Resume,
15021504
Return => Return,
15031505
Unreachable => Unreachable,
@@ -1523,17 +1525,6 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
15231525
} else { false };
15241526
dest || func.visit_with(visitor) || args.visit_with(visitor)
15251527
},
1526-
Assert { ref cond, ref msg, .. } => {
1527-
if cond.visit_with(visitor) {
1528-
if let AssertMessage::BoundsCheck { ref len, ref index } = *msg {
1529-
len.visit_with(visitor) || index.visit_with(visitor)
1530-
} else {
1531-
false
1532-
}
1533-
} else {
1534-
false
1535-
}
1536-
},
15371528
Goto { .. } |
15381529
Resume |
15391530
Return |

src/librustc/mir/visit.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,14 @@ macro_rules! make_mir_visitor {
344344
self.visit_operand(input, location);
345345
}
346346
}
347+
StatementKind::Assert { ref $($mutability)* cond,
348+
expected: _,
349+
ref $($mutability)* msg,
350+
cleanup } => {
351+
self.visit_operand(cond, location);
352+
self.visit_assert_message(msg, location);
353+
cleanup.map(|t| self.visit_branch(block, t));
354+
}
347355
StatementKind::Nop => {}
348356
}
349357
}
@@ -430,17 +438,6 @@ macro_rules! make_mir_visitor {
430438
}
431439
cleanup.map(|t| self.visit_branch(block, t));
432440
}
433-
434-
TerminatorKind::Assert { ref $($mutability)* cond,
435-
expected: _,
436-
ref $($mutability)* msg,
437-
target,
438-
cleanup } => {
439-
self.visit_operand(cond, source_location);
440-
self.visit_assert_message(msg, source_location);
441-
self.visit_branch(block, target);
442-
cleanup.map(|t| self.visit_branch(block, t));
443-
}
444441
}
445442
}
446443

src/librustc_mir/build/expr/as_lvalue.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,16 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
7575
len: Operand::Consume(len),
7676
index: idx.clone()
7777
};
78-
let success = this.assert(block, Operand::Consume(lt), true,
79-
msg, expr_span);
80-
success.and(slice.index(idx))
78+
this.cfg.push(block, Statement {
79+
source_info: this.source_info(expr_span),
80+
kind: StatementKind::Assert {
81+
cond: Operand::Consume(lt),
82+
expected: true,
83+
msg: msg,
84+
cleanup: this.diverge_cleanup(),
85+
}
86+
});
87+
block.and(slice.index(idx))
8188
}
8289
ExprKind::SelfRef => {
8390
block.and(Lvalue::Local(Local::new(1)))

src/librustc_mir/build/expr/as_rvalue.rs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,15 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
8989
Rvalue::BinaryOp(BinOp::Eq, arg.clone(), minval));
9090

9191
let err = ConstMathErr::Overflow(Op::Neg);
92-
block = this.assert(block, Operand::Consume(is_min), false,
93-
AssertMessage::Math(err), expr_span);
92+
this.cfg.push(block, Statement {
93+
source_info: this.source_info(expr_span),
94+
kind: StatementKind::Assert {
95+
cond: Operand::Consume(is_min),
96+
expected: false,
97+
msg: AssertMessage::Math(err),
98+
cleanup: this.diverge_cleanup(),
99+
},
100+
});
94101
}
95102
block.and(Rvalue::UnaryOp(op, arg))
96103
}
@@ -284,9 +291,15 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
284291
}
285292
});
286293

287-
block = self.assert(block, Operand::Consume(of), false,
288-
AssertMessage::Math(err), span);
289-
294+
self.cfg.push(block, Statement {
295+
source_info: self.source_info(span),
296+
kind: StatementKind::Assert {
297+
cond: Operand::Consume(of),
298+
expected: false,
299+
msg: AssertMessage::Math(err),
300+
cleanup: self.diverge_cleanup(),
301+
},
302+
});
290303
block.and(Rvalue::Use(Operand::Consume(val)))
291304
} else {
292305
if ty.is_integral() && (op == BinOp::Div || op == BinOp::Rem) {
@@ -307,8 +320,15 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
307320
self.cfg.push_assign(block, source_info, &is_zero,
308321
Rvalue::BinaryOp(BinOp::Eq, rhs.clone(), zero));
309322

310-
block = self.assert(block, Operand::Consume(is_zero), false,
311-
AssertMessage::Math(zero_err), span);
323+
self.cfg.push(block, Statement {
324+
source_info: self.source_info(span),
325+
kind: StatementKind::Assert {
326+
cond: Operand::Consume(is_zero),
327+
expected: false,
328+
msg: AssertMessage::Math(zero_err),
329+
cleanup: self.diverge_cleanup(),
330+
},
331+
});
312332

313333
// We only need to check for the overflow in one case:
314334
// MIN / -1, and only for signed values.
@@ -332,8 +352,15 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
332352
self.cfg.push_assign(block, source_info, &of,
333353
Rvalue::BinaryOp(BinOp::BitAnd, is_neg_1, is_min));
334354

335-
block = self.assert(block, Operand::Consume(of), false,
336-
AssertMessage::Math(overflow_err), span);
355+
self.cfg.push(block, Statement {
356+
source_info: self.source_info(span),
357+
kind: StatementKind::Assert {
358+
cond: Operand::Consume(of),
359+
expected: false,
360+
msg: AssertMessage::Math(overflow_err),
361+
cleanup: self.diverge_cleanup(),
362+
},
363+
});
337364
}
338365
}
339366

src/librustc_mir/build/scope.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -628,32 +628,6 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
628628
});
629629
next_target.unit()
630630
}
631-
632-
/// Create an Assert terminator and return the success block.
633-
/// If the boolean condition operand is not the expected value,
634-
/// a runtime panic will be caused with the given message.
635-
pub fn assert(&mut self, block: Block,
636-
cond: Operand<'tcx>,
637-
expected: bool,
638-
msg: AssertMessage<'tcx>,
639-
span: Span)
640-
-> Block {
641-
let source_info = self.source_info(span);
642-
643-
let success_block = self.cfg.start_new_block();
644-
let cleanup = self.diverge_cleanup();
645-
646-
self.cfg.terminate(block, source_info,
647-
TerminatorKind::Assert {
648-
cond: cond,
649-
expected: expected,
650-
msg: msg,
651-
target: success_block,
652-
cleanup: cleanup
653-
});
654-
655-
success_block
656-
}
657631
}
658632

659633
/// Builds drops for pop_scope and exit_scope.

src/librustc_passes/mir_stats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ impl<'a, 'tcx> mir_visit::Visitor<'tcx> for StatCollector<'a, 'tcx> {
129129
StatementKind::StorageLive(..) => "StatementKind::StorageLive",
130130
StatementKind::StorageDead(..) => "StatementKind::StorageDead",
131131
StatementKind::InlineAsm { .. } => "StatementKind::InlineAsm",
132+
StatementKind::Assert { .. } => "StatementKind::Assert",
132133
StatementKind::Nop => "StatementKind::Nop",
133134
}, &statement.kind);
134135
self.super_statement(block, statement, location);
@@ -156,7 +157,6 @@ impl<'a, 'tcx> mir_visit::Visitor<'tcx> for StatCollector<'a, 'tcx> {
156157
TerminatorKind::Drop { .. } => "TerminatorKind::Drop",
157158
TerminatorKind::DropAndReplace { .. } => "TerminatorKind::DropAndReplace",
158159
TerminatorKind::Call { .. } => "TerminatorKind::Call",
159-
TerminatorKind::Assert { .. } => "TerminatorKind::Assert",
160160
}, kind);
161161
self.super_terminator_kind(block, kind, location);
162162
}

src/librustc_trans/mir/block.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
9191
};
9292

9393
for statement in &data.statements {
94-
bcx = self.trans_statement(bcx, statement);
94+
bcx = self.trans_statement(bcx, statement, cleanup_bundle);
9595
}
9696

9797
let terminator = data.terminator();
@@ -198,13 +198,6 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
198198
funclet_br(self, bcx, target);
199199
}
200200

201-
mir::TerminatorKind::Assert { ref cond, expected, ref msg, target, cleanup } => {
202-
bcx = self.trans_assert(
203-
bcx, cond, expected, msg, cleanup, cleanup_bundle, terminator.source_info
204-
);
205-
funclet_br(self, bcx, target);
206-
}
207-
208201
mir::TerminatorKind::DropAndReplace { .. } => {
209202
bug!("undesugared DropAndReplace in trans: {:?}", data);
210203
}

0 commit comments

Comments
 (0)