]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_mir/util/pretty.rs
Rollup merge of #69607 - GuillaumeGomez:cleanup-e0376, r=Dylan-DPC
[rust.git] / src / librustc_mir / util / pretty.rs
index 36194335a555a6098367417e112386dd9aff24ea..6fd8f06fe8f25c4ba35ec39ea4cf8b892dc3ec49 100644 (file)
@@ -1,16 +1,16 @@
-use rustc::hir::def_id::{DefId, LOCAL_CRATE};
-use rustc::mir::*;
+use super::graphviz::write_mir_fn_graphviz;
+use crate::transform::MirSource;
 use rustc::mir::visit::Visitor;
+use rustc::mir::*;
 use rustc::ty::{self, TyCtxt};
 use rustc_data_structures::fx::FxHashMap;
+use rustc_hir::def_id::{DefId, LOCAL_CRATE};
 use rustc_index::vec::Idx;
 use std::fmt::Display;
 use std::fmt::Write as _;
 use std::fs;
 use std::io::{self, Write};
 use std::path::{Path, PathBuf};
-use super::graphviz::write_mir_fn_graphviz;
-use crate::transform::MirSource;
 
 const INDENT: &str = "    ";
 /// Alignment for lining up comments following MIR statements
@@ -178,9 +178,7 @@ fn dump_path(
     let mut file_path = PathBuf::new();
     file_path.push(Path::new(&tcx.sess.opts.debugging_opts.dump_mir_dir));
 
-    let item_name = tcx
-        .def_path(source.def_id())
-        .to_filename_friendly_no_crate();
+    let item_name = tcx.def_path(source.def_id()).to_filename_friendly_no_crate();
     // All drop shims have the same DefId, so we have to add the type
     // to get unique file names.
     let shim_disambiguator = match source.instance {
@@ -188,13 +186,11 @@ fn dump_path(
             // Unfortunately, pretty-printed typed are not very filename-friendly.
             // We dome some filtering.
             let mut s = ".".to_owned();
-            s.extend(ty.to_string()
-                .chars()
-                .filter_map(|c| match c {
-                    ' ' => None,
-                    ':' | '<' | '>' => Some('_'),
-                    c => Some(c)
-                }));
+            s.extend(ty.to_string().chars().filter_map(|c| match c {
+                ' ' => None,
+                ':' | '<' | '>' => Some('_'),
+                c => Some(c),
+            }));
             s
         }
         _ => String::new(),
@@ -202,13 +198,7 @@ fn dump_path(
 
     let file_name = format!(
         "rustc.{}{}{}{}.{}.{}.{}",
-        item_name,
-        shim_disambiguator,
-        promotion_id,
-        pass_num,
-        pass_name,
-        disambiguator,
-        extension,
+        item_name, shim_disambiguator, promotion_id, pass_num, pass_name, disambiguator, extension,
     );
 
     file_path.push(&file_name);
@@ -241,14 +231,8 @@ pub fn write_mir_pretty<'tcx>(
     single: Option<DefId>,
     w: &mut dyn Write,
 ) -> io::Result<()> {
-    writeln!(
-        w,
-        "// WARNING: This output format is intended for human consumers only"
-    )?;
-    writeln!(
-        w,
-        "// and is subject to change without notice. Knock yourself out."
-    )?;
+    writeln!(w, "// WARNING: This output format is intended for human consumers only")?;
+    writeln!(w, "// and is subject to change without notice. Knock yourself out.")?;
 
     let mut first = true;
     for def_id in dump_mir_def_ids(tcx, single) {
@@ -265,10 +249,7 @@ pub fn write_mir_pretty<'tcx>(
 
         for (i, body) in tcx.promoted_mir(def_id).iter_enumerated() {
             writeln!(w, "")?;
-            let src = MirSource {
-                instance: ty::InstanceDef::Item(def_id),
-                promoted: Some(i),
-            };
+            let src = MirSource { instance: ty::InstanceDef::Item(def_id), promoted: Some(i) };
             write_mir_fn(tcx, src, body, &mut |_, _| Ok(()), w)?;
         }
     }
@@ -316,10 +297,7 @@ pub fn write_basic_block<'tcx, F>(
     writeln!(w, "{}{:?}{}: {{", INDENT, block, cleanup_text)?;
 
     // List of statements in the middle.
-    let mut current_location = Location {
-        block: block,
-        statement_index: 0,
-    };
+    let mut current_location = Location { block: block, statement_index: 0 };
     for statement in &data.statements {
         extra_data(PassWhere::BeforeLocation(current_location), w)?;
         let indented_body = format!("{0}{0}{1:?};", INDENT, statement);
@@ -370,10 +348,7 @@ fn write_extra<'tcx, F>(tcx: TyCtxt<'tcx>, write: &mut dyn Write, mut visit_op:
 where
     F: FnMut(&mut ExtraComments<'tcx>),
 {
-    let mut extra_comments = ExtraComments {
-        _tcx: tcx,
-        comments: vec![],
-    };
+    let mut extra_comments = ExtraComments { _tcx: tcx, comments: vec![] };
     visit_op(&mut extra_comments);
     for comment in extra_comments.comments {
         writeln!(write, "{:A$} // {}", "", comment, A = ALIGN)?;
@@ -445,11 +420,7 @@ fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
 }
 
 fn comment(tcx: TyCtxt<'_>, SourceInfo { span, scope }: SourceInfo) -> String {
-    format!(
-        "scope {} at {}",
-        scope.index(),
-        tcx.sess.source_map().span_to_string(span)
-    )
+    format!("scope {} at {}", scope.index(), tcx.sess.source_map().span_to_string(span))
 }
 
 /// Prints local variables in a scope tree.
@@ -472,10 +443,7 @@ fn write_scope_tree(
 
         let indented_debug_info = format!(
             "{0:1$}debug {2} => {3:?};",
-            INDENT,
-            indent,
-            var_debug_info.name,
-            var_debug_info.place,
+            INDENT, indent, var_debug_info.name, var_debug_info.place,
         );
 
         writeln!(
@@ -489,7 +457,7 @@ fn write_scope_tree(
 
     // Local variable types (including the user's name in a comment).
     for (local, local_decl) in body.local_decls.iter_enumerated() {
-        if (1..body.arg_count+1).contains(&local.index()) {
+        if (1..body.arg_count + 1).contains(&local.index()) {
             // Skip over argument locals, they're printed in the signature.
             continue;
         }
@@ -499,30 +467,17 @@ fn write_scope_tree(
             continue;
         }
 
-        let mut_str = if local_decl.mutability == Mutability::Mut {
-            "mut "
-        } else {
-            ""
-        };
+        let mut_str = if local_decl.mutability == Mutability::Mut { "mut " } else { "" };
 
-        let mut indented_decl = format!(
-            "{0:1$}let {2}{3:?}: {4:?}",
-            INDENT,
-            indent,
-            mut_str,
-            local,
-            local_decl.ty
-        );
+        let mut indented_decl =
+            format!("{0:1$}let {2}{3:?}: {4:?}", INDENT, indent, mut_str, local, local_decl.ty);
         for user_ty in local_decl.user_ty.projections() {
             write!(indented_decl, " as {:?}", user_ty).unwrap();
         }
         indented_decl.push_str(";");
 
-        let local_name = if local == RETURN_PLACE {
-            format!(" return place")
-        } else {
-            String::new()
-        };
+        let local_name =
+            if local == RETURN_PLACE { " return place".to_string() } else { String::new() };
 
         writeln!(
             w,
@@ -564,10 +519,7 @@ pub fn write_mir_intro<'tcx>(
     let mut scope_tree: FxHashMap<SourceScope, Vec<SourceScope>> = Default::default();
     for (index, scope_data) in body.source_scopes.iter().enumerate() {
         if let Some(parent) = scope_data.parent_scope {
-            scope_tree
-                .entry(parent)
-                .or_default()
-                .push(SourceScope::new(index));
+            scope_tree.entry(parent).or_default().push(SourceScope::new(index));
         } else {
             // Only the argument scope has no parent, because it's the root.
             assert_eq!(index, OUTERMOST_SOURCE_SCOPE.index());
@@ -588,24 +540,22 @@ fn write_mir_sig(
     body: &Body<'_>,
     w: &mut dyn Write,
 ) -> io::Result<()> {
-    use rustc::hir::def::DefKind;
+    use rustc_hir::def::DefKind;
 
     trace!("write_mir_sig: {:?}", src.instance);
     let kind = tcx.def_kind(src.def_id());
     let is_function = match kind {
-        Some(DefKind::Fn)
-        | Some(DefKind::Method)
-        | Some(DefKind::Ctor(..)) => true,
+        Some(DefKind::Fn) | Some(DefKind::Method) | Some(DefKind::Ctor(..)) => true,
         _ => tcx.is_closure(src.def_id()),
     };
     match (kind, src.promoted) {
         (_, Some(i)) => write!(w, "{:?} in ", i)?,
-        (Some(DefKind::Const), _)
-        | (Some(DefKind::AssocConst), _) => write!(w, "const ")?,
-        (Some(DefKind::Static), _) =>
-            write!(w, "static {}", if tcx.is_mutable_static(src.def_id()) { "mut " } else { "" })?,
+        (Some(DefKind::Const), _) | (Some(DefKind::AssocConst), _) => write!(w, "const ")?,
+        (Some(DefKind::Static), _) => {
+            write!(w, "static {}", if tcx.is_mutable_static(src.def_id()) { "mut " } else { "" })?
+        }
         (_, _) if is_function => write!(w, "fn ")?,
-        (None, _) => {}, // things like anon const, not an item
+        (None, _) => {} // things like anon const, not an item
         _ => bug!("Unexpected def kind {:?}", kind),
     }
 
@@ -656,9 +606,5 @@ fn write_user_type_annotations(body: &Body<'_>, w: &mut dyn Write) -> io::Result
 }
 
 pub fn dump_mir_def_ids(tcx: TyCtxt<'_>, single: Option<DefId>) -> Vec<DefId> {
-    if let Some(i) = single {
-        vec![i]
-    } else {
-        tcx.mir_keys(LOCAL_CRATE).iter().cloned().collect()
-    }
+    if let Some(i) = single { vec![i] } else { tcx.mir_keys(LOCAL_CRATE).iter().cloned().collect() }
 }