Skip to content

Commit 2e2924f

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 b65227a commit 2e2924f

File tree

1 file changed

+36
-27
lines changed

1 file changed

+36
-27
lines changed

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

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

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

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

35-
struct IdentifiedAnnotation<'tcx> {
36-
tcx: Option<TyCtxt<'tcx>>,
37-
}
40+
struct AstIdentifiedAnn;
3841

39-
impl<'tcx> pprust_ast::PpAnn for IdentifiedAnnotation<'tcx> {
42+
impl pprust_ast::PpAnn for AstIdentifiedAnn {
4043
fn pre(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
4144
if let pprust_ast::AnnNode::Expr(_) = node {
4245
s.popen();
@@ -74,11 +77,17 @@ impl<'tcx> pprust_ast::PpAnn for IdentifiedAnnotation<'tcx> {
7477
}
7578
}
7679

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

8493
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
@@ -119,11 +128,11 @@ impl<'tcx> pprust_hir::PpAnn for IdentifiedAnnotation<'tcx> {
119128
}
120129
}
121130

122-
struct HygieneAnnotation<'a> {
131+
struct AstHygieneAnn<'a> {
123132
sess: &'a Session,
124133
}
125134

126-
impl<'a> pprust_ast::PpAnn for HygieneAnnotation<'a> {
135+
impl<'a> pprust_ast::PpAnn for AstHygieneAnn<'a> {
127136
fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
128137
match node {
129138
pprust_ast::AnnNode::Ident(&Ident { name, span }) => {
@@ -145,12 +154,12 @@ impl<'a> pprust_ast::PpAnn for HygieneAnnotation<'a> {
145154
}
146155
}
147156

148-
struct TypedAnnotation<'tcx> {
157+
struct HirTypedAnn<'tcx> {
149158
tcx: TyCtxt<'tcx>,
150159
maybe_typeck_results: Cell<Option<&'tcx ty::TypeckResults<'tcx>>>,
151160
}
152161

153-
impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
162+
impl<'tcx> pprust_hir::PpAnn for HirTypedAnn<'tcx> {
154163
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
155164
let old_maybe_typeck_results = self.maybe_typeck_results.get();
156165
if let pprust_hir::Nested::Body(id) = nested {
@@ -242,11 +251,11 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
242251
Source(s) => {
243252
debug!("pretty printing source code {:?}", s);
244253
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 }),
254+
Normal => Box::new(AstNoAnn),
255+
Expanded => Box::new(AstNoAnn),
256+
Identified => Box::new(AstIdentifiedAnn),
257+
ExpandedIdentified => Box::new(AstIdentifiedAnn),
258+
ExpandedHygiene => Box::new(AstHygieneAnn { sess }),
250259
};
251260
let parse = &sess.parse_sess;
252261
let is_expanded = ppm.needs_ast_map();
@@ -289,15 +298,15 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
289298
};
290299
match s {
291300
PpHirMode::Normal => {
292-
let annotation = NoAnn { tcx: Some(tcx) };
301+
let annotation = HirNoAnn { tcx };
293302
f(&annotation)
294303
}
295304
PpHirMode::Identified => {
296-
let annotation = IdentifiedAnnotation { tcx: Some(tcx) };
305+
let annotation = HirIdentifiedAnn { tcx };
297306
f(&annotation)
298307
}
299308
PpHirMode::Typed => {
300-
let annotation = TypedAnnotation { tcx, maybe_typeck_results: Cell::new(None) };
309+
let annotation = HirTypedAnn { tcx, maybe_typeck_results: Cell::new(None) };
301310
tcx.dep_graph.with_ignore(|| f(&annotation))
302311
}
303312
}

0 commit comments

Comments
 (0)