Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 381e3a6

Browse files
committedMar 12, 2025
Return blocks from DropTree::build_mir
Rather than requiring the user to pass in a correctly sized blocks map.
1 parent 59a9b9e commit 381e3a6

File tree

1 file changed

+17
-20
lines changed
  • compiler/rustc_mir_build/src/builder

1 file changed

+17
-20
lines changed
 

‎compiler/rustc_mir_build/src/builder/scope.rs

+17-20
Original file line numberDiff line numberDiff line change
@@ -305,27 +305,25 @@ impl DropTree {
305305
}
306306

307307
/// Builds the MIR for a given drop tree.
308-
///
309-
/// `blocks` should have the same length as `self.drops`, and may have its
310-
/// first value set to some already existing block.
311308
fn build_mir<'tcx, T: DropTreeBuilder<'tcx>>(
312309
&mut self,
313310
cfg: &mut CFG<'tcx>,
314-
blocks: &mut IndexVec<DropIdx, Option<BasicBlock>>,
315-
) {
311+
root_node: Option<BasicBlock>,
312+
) -> IndexVec<DropIdx, Option<BasicBlock>> {
316313
debug!("DropTree::build_mir(drops = {:#?})", self);
317-
assert_eq!(blocks.len(), self.drops.len());
318314

319-
self.assign_blocks::<T>(cfg, blocks);
320-
self.link_blocks(cfg, blocks)
315+
let mut blocks = self.assign_blocks::<T>(cfg, root_node);
316+
self.link_blocks(cfg, &mut blocks);
317+
318+
blocks
321319
}
322320

323321
/// Assign blocks for all of the drops in the drop tree that need them.
324322
fn assign_blocks<'tcx, T: DropTreeBuilder<'tcx>>(
325323
&mut self,
326324
cfg: &mut CFG<'tcx>,
327-
blocks: &mut IndexVec<DropIdx, Option<BasicBlock>>,
328-
) {
325+
root_node: Option<BasicBlock>,
326+
) -> IndexVec<DropIdx, Option<BasicBlock>> {
329327
// StorageDead statements can share blocks with each other and also with
330328
// a Drop terminator. We iterate through the drops to find which drops
331329
// need their own block.
@@ -342,8 +340,11 @@ impl DropTree {
342340
Own,
343341
}
344342

343+
let mut blocks = IndexVec::from_elem(None, &self.drops);
344+
blocks[ROOT_NODE] = root_node;
345+
345346
let mut needs_block = IndexVec::from_elem(Block::None, &self.drops);
346-
if blocks[ROOT_NODE].is_some() {
347+
if root_node.is_some() {
347348
// In some cases (such as drops for `continue`) the root node
348349
// already has a block. In this case, make sure that we don't
349350
// override it.
@@ -385,6 +386,8 @@ impl DropTree {
385386

386387
debug!("assign_blocks: blocks = {:#?}", blocks);
387388
assert!(entry_points.is_empty());
389+
390+
blocks
388391
}
389392

390393
fn link_blocks<'tcx>(
@@ -1575,10 +1578,7 @@ impl<'a, 'tcx: 'a> Builder<'a, 'tcx> {
15751578
span: Span,
15761579
continue_block: Option<BasicBlock>,
15771580
) -> Option<BlockAnd<()>> {
1578-
let mut blocks = IndexVec::from_elem(None, &drops.drops);
1579-
blocks[ROOT_NODE] = continue_block;
1580-
1581-
drops.build_mir::<ExitScopes>(&mut self.cfg, &mut blocks);
1581+
let blocks = drops.build_mir::<ExitScopes>(&mut self.cfg, continue_block);
15821582
let is_coroutine = self.coroutine.is_some();
15831583

15841584
// Link the exit drop tree to unwind drop tree.
@@ -1634,8 +1634,7 @@ impl<'a, 'tcx: 'a> Builder<'a, 'tcx> {
16341634
let drops = &mut self.scopes.coroutine_drops;
16351635
let cfg = &mut self.cfg;
16361636
let fn_span = self.fn_span;
1637-
let mut blocks = IndexVec::from_elem(None, &drops.drops);
1638-
drops.build_mir::<CoroutineDrop>(cfg, &mut blocks);
1637+
let blocks = drops.build_mir::<CoroutineDrop>(cfg, None);
16391638
if let Some(root_block) = blocks[ROOT_NODE] {
16401639
cfg.terminate(
16411640
root_block,
@@ -1671,9 +1670,7 @@ impl<'a, 'tcx: 'a> Builder<'a, 'tcx> {
16711670
fn_span: Span,
16721671
resume_block: &mut Option<BasicBlock>,
16731672
) {
1674-
let mut blocks = IndexVec::from_elem(None, &drops.drops);
1675-
blocks[ROOT_NODE] = *resume_block;
1676-
drops.build_mir::<Unwind>(cfg, &mut blocks);
1673+
let blocks = drops.build_mir::<Unwind>(cfg, *resume_block);
16771674
if let (None, Some(resume)) = (*resume_block, blocks[ROOT_NODE]) {
16781675
cfg.terminate(resume, SourceInfo::outermost(fn_span), TerminatorKind::UnwindResume);
16791676

0 commit comments

Comments
 (0)
Failed to load comments.