Skip to content

Commit 7d1f36a

Browse files
committed
Inline open_drop_for_variant & clean matches::test
1 parent 49ccc10 commit 7d1f36a

File tree

2 files changed

+32
-54
lines changed

2 files changed

+32
-54
lines changed

src/librustc_borrowck/borrowck/mir/elaborate_drops.rs

+22-43
Original file line numberDiff line numberDiff line change
@@ -620,48 +620,11 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
620620
self.elaborated_drop_block(&inner_c)
621621
}
622622

623-
fn open_drop_for_variant<'a>(&mut self,
624-
c: &DropCtxt<'a, 'tcx>,
625-
drop_block: &mut Option<BasicBlock>,
626-
adt: &'tcx ty::AdtDef,
627-
substs: &'tcx Substs<'tcx>,
628-
variant_index: usize)
629-
-> (BasicBlock, bool)
630-
{
631-
let subpath = super::move_path_children_matching(
632-
self.move_data(), c.path, |proj| match proj {
633-
&Projection {
634-
elem: ProjectionElem::Downcast(_, idx), ..
635-
} => idx == variant_index,
636-
_ => false
637-
});
638-
639-
if let Some(variant_path) = subpath {
640-
let base_lv = c.lvalue.clone().elem(
641-
ProjectionElem::Downcast(adt, variant_index)
642-
);
643-
let fields = self.move_paths_for_fields(
644-
&base_lv,
645-
variant_path,
646-
&adt.variants[variant_index],
647-
substs);
648-
(self.drop_ladder(c, fields), true)
649-
} else {
650-
// variant not found - drop the entire enum
651-
if let None = *drop_block {
652-
*drop_block = Some(self.complete_drop(c, true));
653-
}
654-
(drop_block.unwrap(), false)
655-
}
656-
}
657-
658623
fn open_drop_for_adt<'a>(&mut self, c: &DropCtxt<'a, 'tcx>,
659624
adt: &'tcx ty::AdtDef, substs: &'tcx Substs<'tcx>)
660625
-> BasicBlock {
661626
debug!("open_drop_for_adt({:?}, {:?}, {:?})", c, adt, substs);
662627

663-
let mut drop_block = None;
664-
665628
match adt.variants.len() {
666629
1 => {
667630
let fields = self.move_paths_for_fields(
@@ -676,17 +639,33 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
676639
let mut values = Vec::with_capacity(adt.variants.len());
677640
let mut blocks = Vec::with_capacity(adt.variants.len());
678641
let mut otherwise = None;
679-
for (idx, variant) in adt.variants.iter().enumerate() {
642+
for (variant_index, variant) in adt.variants.iter().enumerate() {
680643
let discr = ConstInt::new_inttype(variant.disr_val, adt.discr_ty,
681644
self.tcx.sess.target.uint_type,
682645
self.tcx.sess.target.int_type).unwrap();
683-
let (blk, is_ladder) = self.open_drop_for_variant(c, &mut drop_block, adt,
684-
substs, idx);
685-
if is_ladder {
646+
let subpath = super::move_path_children_matching(
647+
self.move_data(), c.path, |proj| match proj {
648+
&Projection {
649+
elem: ProjectionElem::Downcast(_, idx), ..
650+
} => idx == variant_index,
651+
_ => false
652+
});
653+
if let Some(variant_path) = subpath {
654+
let base_lv = c.lvalue.clone().elem(
655+
ProjectionElem::Downcast(adt, variant_index)
656+
);
657+
let fields = self.move_paths_for_fields(
658+
&base_lv,
659+
variant_path,
660+
&adt.variants[variant_index],
661+
substs);
686662
values.push(discr);
687-
blocks.push(blk);
663+
blocks.push(self.drop_ladder(c, fields));
688664
} else {
689-
otherwise = Some(blk)
665+
// variant not found - drop the entire enum
666+
if let None = otherwise {
667+
otherwise = Some(self.complete_drop(c, true));
668+
}
690669
}
691670
}
692671
if let Some(block) = otherwise {

src/librustc_mir/build/matches/test.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
227227
}
228228

229229
TestKind::SwitchInt { switch_ty, ref options, indices: _ } => {
230-
let (values, targets, ret) = if switch_ty.sty == ty::TyBool {
231-
static BOOL_SWITCH_FALSE: &'static [ConstInt] = &[ConstInt::Infer(0)];
230+
let (ret, terminator) = if switch_ty.sty == ty::TyBool {
232231
assert!(options.len() > 0 && options.len() <= 2);
233232
let (true_bb, false_bb) = (self.cfg.start_new_block(),
234233
self.cfg.start_new_block());
@@ -237,7 +236,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
237236
&ConstVal::Bool(false) => vec![false_bb, true_bb],
238237
v => span_bug!(test.span, "expected boolean value but got {:?}", v)
239238
};
240-
(From::from(BOOL_SWITCH_FALSE), vec![false_bb, true_bb], ret)
239+
(ret, TerminatorKind::if_(self.hir.tcx(), Operand::Consume(lvalue.clone()),
240+
true_bb, false_bb))
241241
} else {
242242
// The switch may be inexhaustive so we
243243
// add a catch all block
@@ -250,15 +250,14 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
250250
let values: Vec<_> = options.iter().map(|v|
251251
v.to_const_int().expect("switching on integral")
252252
).collect();
253-
(From::from(values), targets.clone(), targets)
253+
(targets.clone(), TerminatorKind::SwitchInt {
254+
discr: Operand::Consume(lvalue.clone()),
255+
switch_ty: switch_ty,
256+
values: From::from(values),
257+
targets: targets,
258+
})
254259
};
255-
256-
self.cfg.terminate(block, source_info, TerminatorKind::SwitchInt {
257-
discr: Operand::Consume(lvalue.clone()),
258-
switch_ty: switch_ty,
259-
values: values,
260-
targets: targets.clone(),
261-
});
260+
self.cfg.terminate(block, source_info, terminator);
262261
ret
263262
}
264263

0 commit comments

Comments
 (0)