Skip to content

Commit 88800fd

Browse files
committed
Split and rename the annotation structs.
`NoAnn` and `IdentifiedAnnotation` impl both `pprust_ast::PpAnn` and `pprust_hir::PpAnn`, which is a bit confusing, because the optional `tcx` is only needed for the HIR cases. (Currently the `tcx` is unnecessarily provided in the `expanded` AST cases.) This commit splits each one into `Ast` and `Hir` versions, which makes things clear about where the `tcx` is needed. The commit also renames all the traits so they consistently end with `Ann`.
1 parent 649f92d commit 88800fd

File tree

1 file changed

+36
-27
lines changed

1 file changed

+36
-27
lines changed

compiler/rustc_driver_impl/src/pretty.rs

+36-27
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,27 @@ pub use self::PpMode::*;
1818
pub use self::PpSourceMode::*;
1919
use crate::abort_on_err;
2020

21-
struct NoAnn<'tcx> {
22-
tcx: Option<TyCtxt<'tcx>>,
21+
struct AstNoAnn;
22+
23+
impl pprust_ast::PpAnn for AstNoAnn {}
24+
25+
struct HirNoAnn<'tcx> {
26+
tcx: TyCtxt<'tcx>,
2327
}
2428

25-
impl<'tcx> pprust_ast::PpAnn for NoAnn<'tcx> {}
26-
impl<'tcx> pprust_hir::PpAnn for NoAnn<'tcx> {
29+
impl<'tcx> pprust_hir::PpAnn for HirNoAnn<'tcx> {
2730
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
28-
if let Some(tcx) = self.tcx {
29-
pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
30-
}
31+
pprust_hir::PpAnn::nested(
32+
&(&self.tcx.hir() as &dyn hir::intravisit::Map<'_>),
33+
state,
34+
nested,
35+
)
3136
}
3237
}
3338

34-
struct IdentifiedAnnotation<'tcx> {
35-
tcx: Option<TyCtxt<'tcx>>,
36-
}
39+
struct AstIdentifiedAnn;
3740

38-
impl<'tcx> pprust_ast::PpAnn for IdentifiedAnnotation<'tcx> {
41+
impl pprust_ast::PpAnn for AstIdentifiedAnn {
3942
fn pre(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
4043
if let pprust_ast::AnnNode::Expr(_) = node {
4144
s.popen();
@@ -73,11 +76,17 @@ impl<'tcx> pprust_ast::PpAnn for IdentifiedAnnotation<'tcx> {
7376
}
7477
}
7578

76-
impl<'tcx> pprust_hir::PpAnn for IdentifiedAnnotation<'tcx> {
79+
struct HirIdentifiedAnn<'tcx> {
80+
tcx: TyCtxt<'tcx>,
81+
}
82+
83+
impl<'tcx> pprust_hir::PpAnn for HirIdentifiedAnn<'tcx> {
7784
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
78-
if let Some(ref tcx) = self.tcx {
79-
pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
80-
}
85+
pprust_hir::PpAnn::nested(
86+
&(&self.tcx.hir() as &dyn hir::intravisit::Map<'_>),
87+
state,
88+
nested,
89+
)
8190
}
8291

8392
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
@@ -118,11 +127,11 @@ impl<'tcx> pprust_hir::PpAnn for IdentifiedAnnotation<'tcx> {
118127
}
119128
}
120129

121-
struct HygieneAnnotation<'a> {
130+
struct AstHygieneAnn<'a> {
122131
sess: &'a Session,
123132
}
124133

125-
impl<'a> pprust_ast::PpAnn for HygieneAnnotation<'a> {
134+
impl<'a> pprust_ast::PpAnn for AstHygieneAnn<'a> {
126135
fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
127136
match node {
128137
pprust_ast::AnnNode::Ident(&Ident { name, span }) => {
@@ -144,12 +153,12 @@ impl<'a> pprust_ast::PpAnn for HygieneAnnotation<'a> {
144153
}
145154
}
146155

147-
struct TypedAnnotation<'tcx> {
156+
struct HirTypedAnn<'tcx> {
148157
tcx: TyCtxt<'tcx>,
149158
maybe_typeck_results: Cell<Option<&'tcx ty::TypeckResults<'tcx>>>,
150159
}
151160

152-
impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
161+
impl<'tcx> pprust_hir::PpAnn for HirTypedAnn<'tcx> {
153162
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
154163
let old_maybe_typeck_results = self.maybe_typeck_results.get();
155164
if let pprust_hir::Nested::Body(id) = nested {
@@ -241,11 +250,11 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
241250
Source(s) => {
242251
debug!("pretty printing source code {:?}", s);
243252
let annotation: Box<dyn pprust_ast::PpAnn> = match s {
244-
Normal => Box::new(NoAnn { tcx: None }),
245-
Expanded => Box::new(NoAnn { tcx: Some(ex.tcx()) }),
246-
Identified => Box::new(IdentifiedAnnotation { tcx: None }),
247-
ExpandedIdentified => Box::new(IdentifiedAnnotation { tcx: Some(ex.tcx()) }),
248-
ExpandedHygiene => Box::new(HygieneAnnotation { sess }),
253+
Normal => Box::new(AstNoAnn),
254+
Expanded => Box::new(AstNoAnn),
255+
Identified => Box::new(AstIdentifiedAnn),
256+
ExpandedIdentified => Box::new(AstIdentifiedAnn),
257+
ExpandedHygiene => Box::new(AstHygieneAnn { sess }),
249258
};
250259
let parse = &sess.parse_sess;
251260
let is_expanded = ppm.needs_ast_map();
@@ -288,15 +297,15 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
288297
};
289298
match s {
290299
PpHirMode::Normal => {
291-
let annotation = NoAnn { tcx: Some(tcx) };
300+
let annotation = HirNoAnn { tcx };
292301
f(&annotation)
293302
}
294303
PpHirMode::Identified => {
295-
let annotation = IdentifiedAnnotation { tcx: Some(tcx) };
304+
let annotation = HirIdentifiedAnn { tcx };
296305
f(&annotation)
297306
}
298307
PpHirMode::Typed => {
299-
let annotation = TypedAnnotation { tcx, maybe_typeck_results: Cell::new(None) };
308+
let annotation = HirTypedAnn { tcx, maybe_typeck_results: Cell::new(None) };
300309
tcx.dep_graph.with_ignore(|| f(&annotation))
301310
}
302311
}

0 commit comments

Comments
 (0)