Stop referring to hir::Crate in hir_pretty.

This commit is contained in:
Camille GILLOT 2021-09-19 23:16:28 +02:00
parent 54c3299b3a
commit f9e1de979d
2 changed files with 28 additions and 19 deletions

View file

@ -59,23 +59,23 @@ where
} }
fn call_with_pp_support_hir<A, F>(ppmode: &PpHirMode, tcx: TyCtxt<'_>, f: F) -> A fn call_with_pp_support_hir<A, F>(ppmode: &PpHirMode, tcx: TyCtxt<'_>, f: F) -> A
where where
F: FnOnce(&dyn HirPrinterSupport<'_>, &hir::Crate<'_>) -> A, F: FnOnce(&dyn HirPrinterSupport<'_>, hir_map::Map<'_>) -> A,
{ {
match *ppmode { match *ppmode {
PpHirMode::Normal => { PpHirMode::Normal => {
let annotation = NoAnn { sess: tcx.sess, tcx: Some(tcx) }; let annotation = NoAnn { sess: tcx.sess, tcx: Some(tcx) };
f(&annotation, tcx.hir().krate()) f(&annotation, tcx.hir())
} }
PpHirMode::Identified => { PpHirMode::Identified => {
let annotation = IdentifiedAnnotation { sess: tcx.sess, tcx: Some(tcx) }; let annotation = IdentifiedAnnotation { sess: tcx.sess, tcx: Some(tcx) };
f(&annotation, tcx.hir().krate()) f(&annotation, tcx.hir())
} }
PpHirMode::Typed => { PpHirMode::Typed => {
abort_on_err(tcx.analysis(()), tcx.sess); abort_on_err(tcx.analysis(()), tcx.sess);
let annotation = TypedAnnotation { tcx, maybe_typeck_results: Cell::new(None) }; 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()))
} }
} }
} }
@ -443,17 +443,27 @@ pub fn print_after_hir_lowering<'tcx>(
format!("{:#?}", krate) 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); debug!("pretty printing HIR {:?}", s);
let sess = annotation.sess(); let sess = annotation.sess();
let sm = sess.source_map(); 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| { HirTree => {
debug!("pretty printing HIR tree"); call_with_pp_support_hir(&PpHirMode::Normal, tcx, move |_annotation, hir_map| {
format!("{:#?}", krate) debug!("pretty printing HIR tree");
}), format!("{:#?}", hir_map.krate())
})
}
_ => unreachable!(), _ => unreachable!(),
}; };

View file

@ -15,7 +15,6 @@ use rustc_target::spec::abi::Abi;
use std::borrow::Cow; use std::borrow::Cow;
use std::cell::Cell; use std::cell::Cell;
use std::collections::BTreeMap;
use std::vec; use std::vec;
pub fn id_to_string(map: &dyn rustc_hir::intravisit::Map<'_>, hir_id: hir::HirId) -> String { pub fn id_to_string(map: &dyn rustc_hir::intravisit::Map<'_>, hir_id: hir::HirId) -> String {
@ -69,7 +68,7 @@ impl PpAnn for &dyn rustc_hir::intravisit::Map<'_> {
pub struct State<'a> { pub struct State<'a> {
pub s: pp::Printer, pub s: pp::Printer,
comments: Option<Comments<'a>>, comments: Option<Comments<'a>>,
attrs: &'a BTreeMap<hir::HirId, &'a [ast::Attribute]>, attrs: &'a dyn Fn(hir::HirId) -> &'a [ast::Attribute],
ann: &'a (dyn PpAnn + 'a), ann: &'a (dyn PpAnn + 'a),
} }
@ -146,17 +145,18 @@ pub const INDENT_UNIT: usize = 4;
/// it can scan the input text for comments to copy forward. /// it can scan the input text for comments to copy forward.
pub fn print_crate<'a>( pub fn print_crate<'a>(
sm: &'a SourceMap, sm: &'a SourceMap,
krate: &hir::Crate<'_>, krate: &hir::Mod<'_>,
filename: FileName, filename: FileName,
input: String, input: String,
attrs: &'a dyn Fn(hir::HirId) -> &'a [ast::Attribute],
ann: &'a dyn PpAnn, ann: &'a dyn PpAnn,
) -> String { ) -> String {
let mut s = State::new_from_input(sm, filename, input, &krate.attrs, ann); let mut s = State::new_from_input(sm, filename, input, attrs, ann);
// When printing the AST, we sometimes need to inject `#[no_std]` here. // When printing the AST, we sometimes need to inject `#[no_std]` here.
// Since you can't compile the HIR, it's not necessary. // Since you can't compile the HIR, it's not necessary.
s.print_mod(&krate.module(), s.attrs(hir::CRATE_HIR_ID)); s.print_mod(krate, (*attrs)(hir::CRATE_HIR_ID));
s.print_remaining_comments(); s.print_remaining_comments();
s.s.eof() s.s.eof()
} }
@ -166,7 +166,7 @@ impl<'a> State<'a> {
sm: &'a SourceMap, sm: &'a SourceMap,
filename: FileName, filename: FileName,
input: String, input: String,
attrs: &'a BTreeMap<hir::HirId, &[ast::Attribute]>, attrs: &'a dyn Fn(hir::HirId) -> &'a [ast::Attribute],
ann: &'a dyn PpAnn, ann: &'a dyn PpAnn,
) -> State<'a> { ) -> State<'a> {
State { State {
@ -178,7 +178,7 @@ impl<'a> State<'a> {
} }
fn attrs(&self, id: hir::HirId) -> &'a [ast::Attribute] { fn attrs(&self, id: hir::HirId) -> &'a [ast::Attribute] {
self.attrs.get(&id).map_or(&[], |la| *la) (self.attrs)(id)
} }
} }
@ -186,8 +186,7 @@ pub fn to_string<F>(ann: &dyn PpAnn, f: F) -> String
where where
F: FnOnce(&mut State<'_>), F: FnOnce(&mut State<'_>),
{ {
let mut printer = let mut printer = State { s: pp::mk_printer(), comments: None, attrs: &|_| &[], ann };
State { s: pp::mk_printer(), comments: None, attrs: &BTreeMap::default(), ann };
f(&mut printer); f(&mut printer);
printer.s.eof() printer.s.eof()
} }