//! The various pretty-printing routines.
+use crate::session_diagnostics::UnprettyDumpFail;
use rustc_ast as ast;
use rustc_ast_pretty::pprust;
-use rustc_errors::ErrorReported;
+use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir;
use rustc_hir_pretty as pprust_hir;
use rustc_middle::hir::map as hir_map;
+use rustc_middle::mir::{write_mir_graphviz, write_mir_pretty};
use rustc_middle::ty::{self, TyCtxt};
-use rustc_mir::util::{write_mir_graphviz, write_mir_pretty};
use rustc_session::config::{Input, PpAstTreeMode, PpHirMode, PpMode, PpSourceMode};
use rustc_session::Session;
use rustc_span::symbol::Ident;
F: FnOnce(&dyn PrinterSupport) -> A,
{
match *ppmode {
- Normal | EveryBodyLoops | Expanded => {
+ Normal | Expanded => {
let annotation = NoAnn { sess, tcx };
f(&annotation)
}
}
fn call_with_pp_support_hir<A, F>(ppmode: &PpHirMode, tcx: TyCtxt<'_>, f: F) -> A
where
- F: FnOnce(&dyn HirPrinterSupport<'_>, &hir::Crate<'_>) -> A,
+ F: FnOnce(&dyn HirPrinterSupport<'_>, hir_map::Map<'_>) -> A,
{
match *ppmode {
PpHirMode::Normal => {
let annotation = NoAnn { sess: tcx.sess, tcx: Some(tcx) };
- f(&annotation, tcx.hir().krate())
+ f(&annotation, tcx.hir())
}
PpHirMode::Identified => {
let annotation = IdentifiedAnnotation { sess: tcx.sess, tcx: Some(tcx) };
- f(&annotation, tcx.hir().krate())
+ f(&annotation, tcx.hir())
}
PpHirMode::Typed => {
abort_on_err(tcx.analysis(()), tcx.sess);
let annotation = TypedAnnotation { tcx, maybe_typeck_results: Cell::new(None) };
- tcx.dep_graph.with_ignore(|| f(&annotation, tcx.hir().krate()))
+ tcx.dep_graph.with_ignore(|| f(&annotation, tcx.hir()))
}
}
}
/// Produces the pretty-print annotation object.
///
/// (Rust does not yet support upcasting from a trait object to
- /// an object for one of its super-traits.)
+ /// an object for one of its supertraits.)
fn pp_ann(&self) -> &dyn pprust::PpAnn;
}
/// Produces the pretty-print annotation object.
///
/// (Rust does not yet support upcasting from a trait object to
- /// an object for one of its super-traits.)
+ /// an object for one of its supertraits.)
fn pp_ann(&self) -> &dyn pprust_hir::PpAnn;
}
impl<'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'tcx> {
fn sess(&self) -> &Session {
- &self.tcx.sess
+ self.tcx.sess
}
fn hir_map(&self) -> Option<hir_map::Map<'tcx>> {
let typeck_results = self.maybe_typeck_results.get().or_else(|| {
self.tcx
.hir()
- .maybe_body_owned_by(self.tcx.hir().local_def_id_to_hir_id(expr.hir_id.owner))
+ .maybe_body_owned_by(expr.hir_id.owner.def_id)
.map(|body_id| self.tcx.typeck_body(body_id))
});
fn get_source(input: &Input, sess: &Session) -> (String, FileName) {
let src_name = input.source_name();
let src = String::clone(
- &sess
- .source_map()
+ sess.source_map()
.get_source_file(&src_name)
.expect("get_source_file")
.src
(src, src_name)
}
-fn write_or_print(out: &str, ofile: Option<&Path>) {
+fn write_or_print(out: &str, ofile: Option<&Path>, sess: &Session) {
match ofile {
None => print!("{}", out),
Some(p) => {
if let Err(e) = std::fs::write(p, out) {
- panic!("print-print failed to write {} due to {}", p.display(), e);
+ sess.emit_fatal(UnprettyDumpFail {
+ path: p.display().to_string(),
+ err: e.to_string(),
+ });
}
}
}
annotation.pp_ann(),
false,
parse.edition,
+ &sess.parse_sess.attr_id_generator,
)
})
}
_ => unreachable!(),
};
- write_or_print(&out, ofile);
+ write_or_print(&out, ofile, sess);
}
pub fn print_after_hir_lowering<'tcx>(
annotation.pp_ann(),
true,
parse.edition,
+ &sess.parse_sess.attr_id_generator,
)
})
}
format!("{:#?}", krate)
}
- Hir(s) => call_with_pp_support_hir(&s, tcx, move |annotation, krate| {
+ Hir(s) => call_with_pp_support_hir(&s, tcx, move |annotation, hir_map| {
debug!("pretty printing HIR {:?}", s);
let sess = annotation.sess();
let sm = sess.source_map();
- pprust_hir::print_crate(sm, krate, src_name, src, annotation.pp_ann())
+ let attrs = |id| hir_map.attrs(id);
+ pprust_hir::print_crate(
+ sm,
+ hir_map.root_module(),
+ src_name,
+ src,
+ &attrs,
+ annotation.pp_ann(),
+ )
}),
- HirTree => call_with_pp_support_hir(&PpHirMode::Normal, tcx, move |_annotation, krate| {
- debug!("pretty printing HIR tree");
- format!("{:#?}", krate)
- }),
+ HirTree => {
+ call_with_pp_support_hir(&PpHirMode::Normal, tcx, move |_annotation, hir_map| {
+ debug!("pretty printing HIR tree");
+ format!("{:#?}", hir_map.krate())
+ })
+ }
_ => unreachable!(),
};
- write_or_print(&out, ofile);
+ write_or_print(&out, ofile, tcx.sess);
}
// In an ideal world, this would be a public function called by the driver after
tcx: TyCtxt<'_>,
ppm: PpMode,
ofile: Option<&Path>,
-) -> Result<(), ErrorReported> {
+) -> Result<(), ErrorGuaranteed> {
tcx.analysis(())?;
let out = match ppm {
Mir => {
ThirTree => {
let mut out = String::new();
- abort_on_err(rustc_typeck::check_crate(tcx), tcx.sess);
+ abort_on_err(rustc_hir_analysis::check_crate(tcx), tcx.sess);
debug!("pretty printing THIR tree");
- for did in tcx.body_owners() {
+ for did in tcx.hir().body_owners() {
let _ = writeln!(
out,
"{:?}:\n{}\n",
_ => unreachable!(),
};
- write_or_print(&out, ofile);
+ write_or_print(&out, ofile, tcx.sess);
Ok(())
}