Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f9e1de9

Browse files
committed
Stop referring to hir::Crate in hir_pretty.
1 parent 54c3299 commit f9e1de9

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

compiler/rustc_driver/src/pretty.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,23 @@ where
5959
}
6060
fn call_with_pp_support_hir<A, F>(ppmode: &PpHirMode, tcx: TyCtxt<'_>, f: F) -> A
6161
where
62-
F: FnOnce(&dyn HirPrinterSupport<'_>, &hir::Crate<'_>) -> A,
62+
F: FnOnce(&dyn HirPrinterSupport<'_>, hir_map::Map<'_>) -> A,
6363
{
6464
match *ppmode {
6565
PpHirMode::Normal => {
6666
let annotation = NoAnn { sess: tcx.sess, tcx: Some(tcx) };
67-
f(&annotation, tcx.hir().krate())
67+
f(&annotation, tcx.hir())
6868
}
6969

7070
PpHirMode::Identified => {
7171
let annotation = IdentifiedAnnotation { sess: tcx.sess, tcx: Some(tcx) };
72-
f(&annotation, tcx.hir().krate())
72+
f(&annotation, tcx.hir())
7373
}
7474
PpHirMode::Typed => {
7575
abort_on_err(tcx.analysis(()), tcx.sess);
7676

7777
let annotation = TypedAnnotation { tcx, maybe_typeck_results: Cell::new(None) };
78-
tcx.dep_graph.with_ignore(|| f(&annotation, tcx.hir().krate()))
78+
tcx.dep_graph.with_ignore(|| f(&annotation, tcx.hir()))
7979
}
8080
}
8181
}
@@ -443,17 +443,27 @@ pub fn print_after_hir_lowering<'tcx>(
443443
format!("{:#?}", krate)
444444
}
445445

446-
Hir(s) => call_with_pp_support_hir(&s, tcx, move |annotation, krate| {
446+
Hir(s) => call_with_pp_support_hir(&s, tcx, move |annotation, hir_map| {
447447
debug!("pretty printing HIR {:?}", s);
448448
let sess = annotation.sess();
449449
let sm = sess.source_map();
450-
pprust_hir::print_crate(sm, krate, src_name, src, annotation.pp_ann())
450+
let attrs = |id| hir_map.attrs(id);
451+
pprust_hir::print_crate(
452+
sm,
453+
hir_map.root_module(),
454+
src_name,
455+
src,
456+
&attrs,
457+
annotation.pp_ann(),
458+
)
451459
}),
452460

453-
HirTree => call_with_pp_support_hir(&PpHirMode::Normal, tcx, move |_annotation, krate| {
454-
debug!("pretty printing HIR tree");
455-
format!("{:#?}", krate)
456-
}),
461+
HirTree => {
462+
call_with_pp_support_hir(&PpHirMode::Normal, tcx, move |_annotation, hir_map| {
463+
debug!("pretty printing HIR tree");
464+
format!("{:#?}", hir_map.krate())
465+
})
466+
}
457467

458468
_ => unreachable!(),
459469
};

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use rustc_target::spec::abi::Abi;
1515

1616
use std::borrow::Cow;
1717
use std::cell::Cell;
18-
use std::collections::BTreeMap;
1918
use std::vec;
2019

2120
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<'_> {
6968
pub struct State<'a> {
7069
pub s: pp::Printer,
7170
comments: Option<Comments<'a>>,
72-
attrs: &'a BTreeMap<hir::HirId, &'a [ast::Attribute]>,
71+
attrs: &'a dyn Fn(hir::HirId) -> &'a [ast::Attribute],
7372
ann: &'a (dyn PpAnn + 'a),
7473
}
7574

@@ -146,17 +145,18 @@ pub const INDENT_UNIT: usize = 4;
146145
/// it can scan the input text for comments to copy forward.
147146
pub fn print_crate<'a>(
148147
sm: &'a SourceMap,
149-
krate: &hir::Crate<'_>,
148+
krate: &hir::Mod<'_>,
150149
filename: FileName,
151150
input: String,
151+
attrs: &'a dyn Fn(hir::HirId) -> &'a [ast::Attribute],
152152
ann: &'a dyn PpAnn,
153153
) -> String {
154-
let mut s = State::new_from_input(sm, filename, input, &krate.attrs, ann);
154+
let mut s = State::new_from_input(sm, filename, input, attrs, ann);
155155

156156
// When printing the AST, we sometimes need to inject `#[no_std]` here.
157157
// Since you can't compile the HIR, it's not necessary.
158158

159-
s.print_mod(&krate.module(), s.attrs(hir::CRATE_HIR_ID));
159+
s.print_mod(krate, (*attrs)(hir::CRATE_HIR_ID));
160160
s.print_remaining_comments();
161161
s.s.eof()
162162
}
@@ -166,7 +166,7 @@ impl<'a> State<'a> {
166166
sm: &'a SourceMap,
167167
filename: FileName,
168168
input: String,
169-
attrs: &'a BTreeMap<hir::HirId, &[ast::Attribute]>,
169+
attrs: &'a dyn Fn(hir::HirId) -> &'a [ast::Attribute],
170170
ann: &'a dyn PpAnn,
171171
) -> State<'a> {
172172
State {
@@ -178,16 +178,15 @@ impl<'a> State<'a> {
178178
}
179179

180180
fn attrs(&self, id: hir::HirId) -> &'a [ast::Attribute] {
181-
self.attrs.get(&id).map_or(&[], |la| *la)
181+
(self.attrs)(id)
182182
}
183183
}
184184

185185
pub fn to_string<F>(ann: &dyn PpAnn, f: F) -> String
186186
where
187187
F: FnOnce(&mut State<'_>),
188188
{
189-
let mut printer =
190-
State { s: pp::mk_printer(), comments: None, attrs: &BTreeMap::default(), ann };
189+
let mut printer = State { s: pp::mk_printer(), comments: None, attrs: &|_| &[], ann };
191190
f(&mut printer);
192191
printer.s.eof()
193192
}

0 commit comments

Comments
 (0)