diff --git a/src/librustc_mir/build/matches/test.rs b/src/librustc_mir/build/matches/test.rs index 439f028ca8a..c0b93456d85 100644 --- a/src/librustc_mir/build/matches/test.rs +++ b/src/librustc_mir/build/matches/test.rs @@ -205,7 +205,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { if let Some(otherwise_block) = otherwise_block { targets.push(otherwise_block); } else { - let unreachable_block = self.cfg.start_new_block(); + let unreachable_block = self.unreachable_block(); targets.push(unreachable_block); target_blocks.push(unreachable_block); } diff --git a/src/librustc_mir/build/mod.rs b/src/librustc_mir/build/mod.rs index 2073d495300..79be3701041 100644 --- a/src/librustc_mir/build/mod.rs +++ b/src/librustc_mir/build/mod.rs @@ -306,6 +306,8 @@ struct Builder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> { cached_resume_block: Option, /// cached block with the RETURN terminator cached_return_block: Option, + /// cached block with the UNREACHABLE terminator + cached_unreachable_block: Option, } struct CFG<'tcx> { @@ -399,6 +401,11 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>, TerminatorKind::Goto { target: return_block }); builder.cfg.terminate(return_block, source_info, TerminatorKind::Return); + // Attribute any unreachable codepaths to the function's closing brace + if let Some(unreachable_block) = builder.cached_unreachable_block { + builder.cfg.terminate(unreachable_block, source_info, + TerminatorKind::Unreachable); + } return_block.unit() })); assert_eq!(block, builder.return_block()); @@ -501,7 +508,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { var_indices: NodeMap(), unit_temp: None, cached_resume_block: None, - cached_return_block: None + cached_return_block: None, + cached_unreachable_block: None, }; assert_eq!(builder.cfg.start_new_block(), START_BLOCK); @@ -630,6 +638,17 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { } } } + + fn unreachable_block(&mut self) -> BasicBlock { + match self.cached_unreachable_block { + Some(ub) => ub, + None => { + let ub = self.cfg.start_new_block(); + self.cached_unreachable_block = Some(ub); + ub + } + } + } } ///////////////////////////////////////////////////////////////////////////