Skip to content

Commit aed0c9c

Browse files
committed
use lazy cached unreachable block - assign it to the function's closing brace
1 parent 829b703 commit aed0c9c

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/librustc_mir/build/matches/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
205205
if let Some(otherwise_block) = otherwise_block {
206206
targets.push(otherwise_block);
207207
} else {
208-
let unreachable_block = self.cfg.start_new_block();
208+
let unreachable_block = self.unreachable_block();
209209
targets.push(unreachable_block);
210210
target_blocks.push(unreachable_block);
211211
}

src/librustc_mir/build/mod.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ struct Builder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
306306
cached_resume_block: Option<BasicBlock>,
307307
/// cached block with the RETURN terminator
308308
cached_return_block: Option<BasicBlock>,
309+
/// cached block with the UNREACHABLE terminator
310+
cached_unreachable_block: Option<BasicBlock>,
309311
}
310312

311313
struct CFG<'tcx> {
@@ -399,6 +401,11 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
399401
TerminatorKind::Goto { target: return_block });
400402
builder.cfg.terminate(return_block, source_info,
401403
TerminatorKind::Return);
404+
// Attribute any unreachable codepaths to the function's closing brace
405+
if let Some(unreachable_block) = builder.cached_unreachable_block {
406+
builder.cfg.terminate(unreachable_block, source_info,
407+
TerminatorKind::Unreachable);
408+
}
402409
return_block.unit()
403410
}));
404411
assert_eq!(block, builder.return_block());
@@ -501,7 +508,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
501508
var_indices: NodeMap(),
502509
unit_temp: None,
503510
cached_resume_block: None,
504-
cached_return_block: None
511+
cached_return_block: None,
512+
cached_unreachable_block: None,
505513
};
506514

507515
assert_eq!(builder.cfg.start_new_block(), START_BLOCK);
@@ -630,6 +638,17 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
630638
}
631639
}
632640
}
641+
642+
fn unreachable_block(&mut self) -> BasicBlock {
643+
match self.cached_unreachable_block {
644+
Some(ub) => ub,
645+
None => {
646+
let ub = self.cfg.start_new_block();
647+
self.cached_unreachable_block = Some(ub);
648+
ub
649+
}
650+
}
651+
}
633652
}
634653

635654
///////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)