]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/no_landing_pads.rs
use `diagnostic_item` and modify wording
[rust.git] / src / librustc_mir / transform / no_landing_pads.rs
1 //! This pass removes the unwind branch of all the terminators when the no-landing-pads option is
2 //! specified.
3
4 use crate::transform::{MirPass, MirSource};
5 use rustc::mir::visit::MutVisitor;
6 use rustc::mir::*;
7 use rustc::ty::TyCtxt;
8
9 pub struct NoLandingPads<'tcx> {
10     tcx: TyCtxt<'tcx>,
11 }
12
13 impl<'tcx> NoLandingPads<'tcx> {
14     pub fn new(tcx: TyCtxt<'tcx>) -> Self {
15         NoLandingPads { tcx }
16     }
17 }
18
19 impl<'tcx> MirPass<'tcx> for NoLandingPads<'tcx> {
20     fn run_pass(&self, tcx: TyCtxt<'tcx>, _: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>) {
21         no_landing_pads(tcx, body)
22     }
23 }
24
25 pub fn no_landing_pads<'tcx>(tcx: TyCtxt<'tcx>, body: &mut BodyAndCache<'tcx>) {
26     if tcx.sess.no_landing_pads() {
27         NoLandingPads::new(tcx).visit_body(body);
28     }
29 }
30
31 impl<'tcx> MutVisitor<'tcx> for NoLandingPads<'tcx> {
32     fn tcx(&self) -> TyCtxt<'tcx> {
33         self.tcx
34     }
35
36     fn visit_terminator_kind(&mut self, kind: &mut TerminatorKind<'tcx>, location: Location) {
37         if let Some(unwind) = kind.unwind_mut() {
38             unwind.take();
39         }
40         self.super_terminator_kind(kind, location);
41     }
42 }