//! 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;
use rustc_span::FileName;
use std::cell::Cell;
+use std::fmt::Write;
use std::path::Path;
pub use self::PpMode::*;
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;
}
maybe_typeck_results: Cell<Option<&'tcx ty::TypeckResults<'tcx>>>,
}
-impl<'tcx> TypedAnnotation<'tcx> {
- /// Gets the type-checking results for the current body.
- /// As this will ICE if called outside bodies, only call when working with
- /// `Expr` or `Pat` nodes (they are guaranteed to be found only in bodies).
- #[track_caller]
- fn typeck_results(&self) -> &'tcx ty::TypeckResults<'tcx> {
- self.maybe_typeck_results
- .get()
- .expect("`TypedAnnotation::typeck_results` called outside of body")
- }
-}
-
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>> {
}
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
if let pprust_hir::AnnNode::Expr(expr) = node {
- s.s.space();
- s.s.word("as");
- s.s.space();
- s.s.word(self.typeck_results().expr_ty(expr).to_string());
+ let typeck_results = self.maybe_typeck_results.get().or_else(|| {
+ self.tcx
+ .hir()
+ .maybe_body_owned_by(expr.hir_id.owner.def_id)
+ .map(|body_id| self.tcx.typeck_body(body_id))
+ });
+
+ if let Some(typeck_results) = typeck_results {
+ s.s.space();
+ s.s.word("as");
+ s.s.space();
+ s.s.word(typeck_results.expr_ty(expr).to_string());
+ }
+
s.pclose();
}
}
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 => {
let mut out = Vec::new();
}
ThirTree => {
- // FIXME(rust-lang/project-thir-unsafeck#8)
- todo!()
+ let mut out = String::new();
+ abort_on_err(rustc_hir_analysis::check_crate(tcx), tcx.sess);
+ debug!("pretty printing THIR tree");
+ for did in tcx.hir().body_owners() {
+ let _ = writeln!(
+ out,
+ "{:?}:\n{}\n",
+ did,
+ tcx.thir_tree(ty::WithOptConstParam::unknown(did))
+ );
+ }
+ out
}
_ => unreachable!(),
};
- write_or_print(&out, ofile);
+ write_or_print(&out, ofile, tcx.sess);
Ok(())
}