]> git.proxmox.com Git - rustc.git/blobdiff - src/librustc_mir/transform/no_landing_pads.rs
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_mir / transform / no_landing_pads.rs
index 762bb5d44839f096c06ee216889d3310bec56ed0..1d83733e4cd306bc88868a32f6195286df53327d 100644 (file)
@@ -1,32 +1,43 @@
 //! This pass removes the unwind branch of all the terminators when the no-landing-pads option is
 //! specified.
 
-use rustc::ty::TyCtxt;
-use rustc::mir::*;
-use rustc::mir::visit::MutVisitor;
 use crate::transform::{MirPass, MirSource};
+use rustc_middle::mir::visit::MutVisitor;
+use rustc_middle::mir::*;
+use rustc_middle::ty::TyCtxt;
+use rustc_target::spec::PanicStrategy;
 
-pub struct NoLandingPads;
+pub struct NoLandingPads<'tcx> {
+    tcx: TyCtxt<'tcx>,
+}
+
+impl<'tcx> NoLandingPads<'tcx> {
+    pub fn new(tcx: TyCtxt<'tcx>) -> Self {
+        NoLandingPads { tcx }
+    }
+}
 
-impl<'tcx> MirPass<'tcx> for NoLandingPads {
+impl<'tcx> MirPass<'tcx> for NoLandingPads<'tcx> {
     fn run_pass(&self, tcx: TyCtxt<'tcx>, _: MirSource<'tcx>, body: &mut Body<'tcx>) {
         no_landing_pads(tcx, body)
     }
 }
 
 pub fn no_landing_pads<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
-    if tcx.sess.no_landing_pads() {
-        NoLandingPads.visit_body(body);
+    if tcx.sess.panic_strategy() == PanicStrategy::Abort {
+        NoLandingPads::new(tcx).visit_body(body);
     }
 }
 
-impl<'tcx> MutVisitor<'tcx> for NoLandingPads {
-    fn visit_terminator_kind(&mut self,
-                        kind: &mut TerminatorKind<'tcx>,
-                        location: Location) {
-        if let Some(unwind) = kind.unwind_mut() {
+impl<'tcx> MutVisitor<'tcx> for NoLandingPads<'tcx> {
+    fn tcx(&self) -> TyCtxt<'tcx> {
+        self.tcx
+    }
+
+    fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, location: Location) {
+        if let Some(unwind) = terminator.kind.unwind_mut() {
             unwind.take();
         }
-        self.super_terminator_kind(kind, location);
+        self.super_terminator(terminator, location);
     }
 }