]> git.lizzy.rs Git - rust.git/blobdiff - compiler/rustc_middle/src/mir/pretty.rs
Revert "Auto merge of #93800 - b-naber:static-initializers-mir-val, r=oli-obk"
[rust.git] / compiler / rustc_middle / src / mir / pretty.rs
index 784babffeff42e0bb598c84bc90acb97ef1cd285..6320b055ab7dcd90555a79e83a24a0c2ea9e61ec 100644 (file)
@@ -17,8 +17,9 @@
 use rustc_middle::mir::visit::Visitor;
 use rustc_middle::mir::MirSource;
 use rustc_middle::mir::*;
-use rustc_middle::ty::{self, TyCtxt};
+use rustc_middle::ty::{self, TyCtxt, TypeFoldable, TypeVisitor};
 use rustc_target::abi::Size;
+use std::ops::ControlFlow;
 
 const INDENT: &str = "    ";
 /// Alignment for lining up comments following MIR statements
@@ -89,14 +90,11 @@ pub fn dump_mir<'tcx, F>(
 }
 
 pub fn dump_enabled<'tcx>(tcx: TyCtxt<'tcx>, pass_name: &str, def_id: DefId) -> bool {
-    let filters = match tcx.sess.opts.debugging_opts.dump_mir {
-        None => return false,
-        Some(ref filters) => filters,
+    let Some(ref filters) = tcx.sess.opts.debugging_opts.dump_mir else {
+        return false;
     };
-    let node_path = ty::print::with_forced_impl_filename_line(|| {
-        // see notes on #41697 below
-        tcx.def_path_str(def_id)
-    });
+    // see notes on #41697 below
+    let node_path = ty::print::with_forced_impl_filename_line!(tcx.def_path_str(def_id));
     filters.split('|').any(|or_filter| {
         or_filter.split('&').all(|and_filter| {
             let and_filter_trimmed = and_filter.trim();
@@ -124,10 +122,9 @@ fn dump_matched_mir_node<'tcx, F>(
     let _: io::Result<()> = try {
         let mut file =
             create_dump_file(tcx, "mir", pass_num, pass_name, disambiguator, body.source)?;
-        let def_path = ty::print::with_forced_impl_filename_line(|| {
-            // see notes on #41697 above
-            tcx.def_path_str(body.source.def_id())
-        });
+        // see notes on #41697 above
+        let def_path =
+            ty::print::with_forced_impl_filename_line!(tcx.def_path_str(body.source.def_id()));
         write!(file, "// MIR for `{}", def_path)?;
         match body.source.promoted {
             None => write!(file, "`")?,
@@ -586,9 +583,8 @@ fn write_scope_tree(
         )?;
     }
 
-    let children = match scope_tree.get(&parent) {
-        Some(children) => children,
-        None => return Ok(()),
+    let Some(children) = scope_tree.get(&parent) else {
+        return Ok(());
     };
 
     for &child in children {
@@ -668,7 +664,6 @@ pub fn write_allocations<'tcx>(
     fn alloc_ids_from_alloc(alloc: &Allocation) -> impl DoubleEndedIterator<Item = AllocId> + '_ {
         alloc.relocations().values().map(|id| *id)
     }
-
     fn alloc_ids_from_const(val: ConstValue<'_>) -> impl Iterator<Item = AllocId> + '_ {
         match val {
             ConstValue::Scalar(interpret::Scalar::Ptr(ptr, _size)) => {
@@ -682,29 +677,17 @@ fn alloc_ids_from_const(val: ConstValue<'_>) -> impl Iterator<Item = AllocId> +
             }
         }
     }
-
     struct CollectAllocIds(BTreeSet<AllocId>);
-
-    impl<'tcx> Visitor<'tcx> for CollectAllocIds {
-        fn visit_const(&mut self, c: ty::Const<'tcx>, _loc: Location) {
+    impl<'tcx> TypeVisitor<'tcx> for CollectAllocIds {
+        fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
             if let ty::ConstKind::Value(val) = c.val() {
                 self.0.extend(alloc_ids_from_const(val));
             }
-        }
-
-        fn visit_constant(&mut self, c: &Constant<'tcx>, loc: Location) {
-            match c.literal {
-                ConstantKind::Ty(c) => self.visit_const(c, loc),
-                ConstantKind::Val(val, _) => {
-                    self.0.extend(alloc_ids_from_const(val));
-                }
-            }
+            c.super_visit_with(self)
         }
     }
-
     let mut visitor = CollectAllocIds(Default::default());
-    visitor.visit_body(body);
-
+    body.visit_with(&mut visitor);
     // `seen` contains all seen allocations, including the ones we have *not* printed yet.
     // The protocol is to first `insert` into `seen`, and only if that returns `true`
     // then push to `todo`.
@@ -971,10 +954,10 @@ fn write_mir_sig(tcx: TyCtxt<'_>, body: &Body<'_>, w: &mut dyn Write) -> io::Res
         _ => bug!("Unexpected def kind {:?}", kind),
     }
 
-    ty::print::with_forced_impl_filename_line(|| {
+    ty::print::with_forced_impl_filename_line! {
         // see notes on #41697 elsewhere
-        write!(w, "{}", tcx.def_path_str(def_id))
-    })?;
+        write!(w, "{}", tcx.def_path_str(def_id))?
+    }
 
     if body.source.promoted.is_none() && is_function {
         write!(w, "(")?;