Skip to content

Commit 060851b

Browse files
committed
Remove pretty-printing traits.
`call_with_pp_support_ast` and `call_with_pp_support_hir` how each have a single call site. This commit inlines and removes them, which also removes the need for all the supporting traits: `Sess`, `AstPrinterSupport`, and `HirPrinterSupport`. The `sess` member is also removed from several structs.
1 parent 7d145a0 commit 060851b

File tree

2 files changed

+52
-130
lines changed

2 files changed

+52
-130
lines changed

Diff for: compiler/rustc_driver_impl/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#![feature(lazy_cell)]
1313
#![feature(let_chains)]
1414
#![feature(panic_update_hook)]
15-
#![feature(trait_upcasting)]
1615
#![recursion_limit = "256"]
1716
#![allow(rustc::potential_query_instability)]
1817
#![deny(rustc::untranslatable_diagnostic)]

Diff for: compiler/rustc_driver_impl/src/pretty.rs

+52-129
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rustc_ast_pretty::pprust as pprust_ast;
55
use rustc_hir as hir;
66
use rustc_hir_pretty as pprust_hir;
77
use rustc_middle::bug;
8-
use rustc_middle::hir::map as hir_map;
98
use rustc_middle::mir::{write_mir_graphviz, write_mir_pretty};
109
use rustc_middle::ty::{self, TyCtxt};
1110
use rustc_session::config::{OutFileName, PpHirMode, PpMode, PpSourceMode};
@@ -20,87 +19,10 @@ pub use self::PpMode::*;
2019
pub use self::PpSourceMode::*;
2120
use crate::abort_on_err;
2221

23-
// This slightly awkward construction is to allow for each PpMode to
24-
// choose whether it needs to do analyses (which can consume the
25-
// Session) and then pass through the session (now attached to the
26-
// analysis results) on to the chosen pretty-printer, along with the
27-
// `&PpAnn` object.
28-
//
29-
// Note that since the `&AstPrinterSupport` is freshly constructed on each
30-
// call, it would not make sense to try to attach the lifetime of `self`
31-
// to the lifetime of the `&PrinterObject`.
32-
33-
/// Constructs an `AstPrinterSupport` object and passes it to `f`.
34-
fn call_with_pp_support_ast<'tcx, A, F>(
35-
ppmode: &PpSourceMode,
36-
sess: &'tcx Session,
37-
tcx: Option<TyCtxt<'tcx>>,
38-
f: F,
39-
) -> A
40-
where
41-
F: FnOnce(&dyn AstPrinterSupport) -> A,
42-
{
43-
match *ppmode {
44-
Normal | Expanded => {
45-
let annotation = NoAnn { sess, tcx };
46-
f(&annotation)
47-
}
48-
Identified | ExpandedIdentified => {
49-
let annotation = IdentifiedAnnotation { sess, tcx };
50-
f(&annotation)
51-
}
52-
ExpandedHygiene => {
53-
let annotation = HygieneAnnotation { sess };
54-
f(&annotation)
55-
}
56-
}
57-
}
58-
59-
fn call_with_pp_support_hir<A, F>(ppmode: &PpHirMode, tcx: TyCtxt<'_>, f: F) -> A
60-
where
61-
F: FnOnce(&dyn HirPrinterSupport, hir_map::Map<'_>) -> A,
62-
{
63-
match *ppmode {
64-
PpHirMode::Normal => {
65-
let annotation = NoAnn { sess: tcx.sess, tcx: Some(tcx) };
66-
f(&annotation, tcx.hir())
67-
}
68-
PpHirMode::Identified => {
69-
let annotation = IdentifiedAnnotation { sess: tcx.sess, tcx: Some(tcx) };
70-
f(&annotation, tcx.hir())
71-
}
72-
PpHirMode::Typed => {
73-
abort_on_err(tcx.analysis(()), tcx.sess);
74-
75-
let annotation = TypedAnnotation { tcx, maybe_typeck_results: Cell::new(None) };
76-
tcx.dep_graph.with_ignore(|| f(&annotation, tcx.hir()))
77-
}
78-
}
79-
}
80-
81-
trait Sess {
82-
/// Provides a uniform interface for re-extracting a reference to a
83-
/// `Session`.
84-
fn sess(&self) -> &Session;
85-
}
86-
87-
trait AstPrinterSupport: pprust_ast::PpAnn + Sess {}
88-
trait HirPrinterSupport: pprust_hir::PpAnn + Sess {}
89-
9022
struct NoAnn<'hir> {
91-
sess: &'hir Session,
9223
tcx: Option<TyCtxt<'hir>>,
9324
}
9425

95-
impl<'hir> Sess for NoAnn<'hir> {
96-
fn sess(&self) -> &Session {
97-
self.sess
98-
}
99-
}
100-
101-
impl<'tcx> AstPrinterSupport for NoAnn<'tcx> {}
102-
impl<'hir> HirPrinterSupport for NoAnn<'hir> {}
103-
10426
impl<'hir> pprust_ast::PpAnn for NoAnn<'hir> {}
10527
impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
10628
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
@@ -111,18 +33,9 @@ impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
11133
}
11234

11335
struct IdentifiedAnnotation<'hir> {
114-
sess: &'hir Session,
11536
tcx: Option<TyCtxt<'hir>>,
11637
}
11738

118-
impl<'hir> Sess for IdentifiedAnnotation<'hir> {
119-
fn sess(&self) -> &Session {
120-
self.sess
121-
}
122-
}
123-
124-
impl<'hir> AstPrinterSupport for IdentifiedAnnotation<'hir> {}
125-
12639
impl<'hir> pprust_ast::PpAnn for IdentifiedAnnotation<'hir> {
12740
fn pre(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
12841
if let pprust_ast::AnnNode::Expr(_) = node {
@@ -161,8 +74,6 @@ impl<'hir> pprust_ast::PpAnn for IdentifiedAnnotation<'hir> {
16174
}
16275
}
16376

164-
impl<'hir> HirPrinterSupport for IdentifiedAnnotation<'hir> {}
165-
16677
impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
16778
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
16879
if let Some(ref tcx) = self.tcx {
@@ -212,14 +123,6 @@ struct HygieneAnnotation<'a> {
212123
sess: &'a Session,
213124
}
214125

215-
impl<'a> Sess for HygieneAnnotation<'a> {
216-
fn sess(&self) -> &Session {
217-
self.sess
218-
}
219-
}
220-
221-
impl<'a> AstPrinterSupport for HygieneAnnotation<'a> {}
222-
223126
impl<'a> pprust_ast::PpAnn for HygieneAnnotation<'a> {
224127
fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
225128
match node {
@@ -247,14 +150,6 @@ struct TypedAnnotation<'tcx> {
247150
maybe_typeck_results: Cell<Option<&'tcx ty::TypeckResults<'tcx>>>,
248151
}
249152

250-
impl<'tcx> Sess for TypedAnnotation<'tcx> {
251-
fn sess(&self) -> &Session {
252-
self.tcx.sess
253-
}
254-
}
255-
256-
impl<'tcx> HirPrinterSupport for TypedAnnotation<'tcx> {}
257-
258153
impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
259154
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
260155
let old_maybe_typeck_results = self.maybe_typeck_results.get();
@@ -320,7 +215,7 @@ pub enum PrintExtra<'tcx> {
320215
impl<'tcx> PrintExtra<'tcx> {
321216
fn with_krate<F, R>(&self, f: F) -> R
322217
where
323-
F: FnOnce(&ast::Crate) -> R
218+
F: FnOnce(&ast::Crate) -> R,
324219
{
325220
match self {
326221
PrintExtra::AfterParsing { krate, .. } => f(krate),
@@ -345,23 +240,26 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
345240

346241
let out = match ppm {
347242
Source(s) => {
348-
// Silently ignores an identified node.
349-
call_with_pp_support_ast(&s, sess, None, move |annotation| {
350-
debug!("pretty printing source code {:?}", s);
351-
let sess = annotation.sess();
352-
let parse = &sess.parse_sess;
353-
let is_expanded = ppm.needs_ast_map();
354-
ex.with_krate(|krate|
355-
pprust_ast::print_crate(
356-
sess.source_map(),
357-
krate,
358-
src_name,
359-
src,
360-
annotation,
361-
is_expanded,
362-
parse.edition,
363-
&sess.parse_sess.attr_id_generator,
364-
)
243+
debug!("pretty printing source code {:?}", s);
244+
let annotation: Box<dyn pprust_ast::PpAnn> = match s {
245+
Normal => Box::new(NoAnn { tcx: None }),
246+
Expanded => Box::new(NoAnn { tcx: Some(ex.tcx()) }),
247+
Identified => Box::new(IdentifiedAnnotation { tcx: None }),
248+
ExpandedIdentified => Box::new(IdentifiedAnnotation { tcx: Some(ex.tcx()) }),
249+
ExpandedHygiene => Box::new(HygieneAnnotation { sess }),
250+
};
251+
let parse = &sess.parse_sess;
252+
let is_expanded = ppm.needs_ast_map();
253+
ex.with_krate(|krate| {
254+
pprust_ast::print_crate(
255+
sess.source_map(),
256+
krate,
257+
src_name,
258+
src,
259+
&*annotation,
260+
is_expanded,
261+
parse.edition,
262+
&sess.parse_sess.attr_id_generator,
365263
)
366264
})
367265
}
@@ -373,13 +271,38 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
373271
debug!("pretty-printing expanded AST");
374272
format!("{:#?}", ex.tcx().resolver_for_lowering(()).borrow().1)
375273
}
376-
Hir(s) => call_with_pp_support_hir(&s, ex.tcx(), move |annotation, hir_map| {
274+
Hir(s) => {
377275
debug!("pretty printing HIR {:?}", s);
378-
let sess = annotation.sess();
379-
let sm = sess.source_map();
380-
let attrs = |id| hir_map.attrs(id);
381-
pprust_hir::print_crate(sm, hir_map.root_module(), src_name, src, &attrs, annotation)
382-
}),
276+
let tcx = ex.tcx();
277+
let f = |annotation: &dyn pprust_hir::PpAnn| {
278+
let sm = sess.source_map();
279+
let hir_map = tcx.hir();
280+
let attrs = |id| hir_map.attrs(id);
281+
pprust_hir::print_crate(
282+
sm,
283+
hir_map.root_module(),
284+
src_name,
285+
src,
286+
&attrs,
287+
annotation,
288+
)
289+
};
290+
match s {
291+
PpHirMode::Normal => {
292+
let annotation = NoAnn { tcx: Some(tcx) };
293+
f(&annotation)
294+
}
295+
PpHirMode::Identified => {
296+
let annotation = IdentifiedAnnotation { tcx: Some(tcx) };
297+
f(&annotation)
298+
}
299+
PpHirMode::Typed => {
300+
abort_on_err(tcx.analysis(()), tcx.sess);
301+
let annotation = TypedAnnotation { tcx, maybe_typeck_results: Cell::new(None) };
302+
tcx.dep_graph.with_ignore(|| f(&annotation))
303+
}
304+
}
305+
}
383306
HirTree => {
384307
debug!("pretty printing HIR tree");
385308
format!("{:#?}", ex.tcx().hir().krate())

0 commit comments

Comments
 (0)