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`.
This commit is contained in:
Nicholas Nethercote 2023-10-11 09:47:15 +11:00
parent b65227a9ee
commit 2e2924f263

View File

@ -19,24 +19,27 @@ pub use self::PpMode::*;
pub use self::PpSourceMode::*; pub use self::PpSourceMode::*;
use crate::abort_on_err; use crate::abort_on_err;
struct NoAnn<'tcx> { struct AstNoAnn;
tcx: Option<TyCtxt<'tcx>>,
impl pprust_ast::PpAnn for AstNoAnn {}
struct HirNoAnn<'tcx> {
tcx: TyCtxt<'tcx>,
} }
impl<'tcx> pprust_ast::PpAnn for NoAnn<'tcx> {} impl<'tcx> pprust_hir::PpAnn for HirNoAnn<'tcx> {
impl<'tcx> pprust_hir::PpAnn for NoAnn<'tcx> {
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) { fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
if let Some(tcx) = self.tcx { pprust_hir::PpAnn::nested(
pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested) &(&self.tcx.hir() as &dyn hir::intravisit::Map<'_>),
} state,
nested,
)
} }
} }
struct IdentifiedAnnotation<'tcx> { struct AstIdentifiedAnn;
tcx: Option<TyCtxt<'tcx>>,
}
impl<'tcx> pprust_ast::PpAnn for IdentifiedAnnotation<'tcx> { impl pprust_ast::PpAnn for AstIdentifiedAnn {
fn pre(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) { fn pre(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
if let pprust_ast::AnnNode::Expr(_) = node { if let pprust_ast::AnnNode::Expr(_) = node {
s.popen(); s.popen();
@ -74,11 +77,17 @@ impl<'tcx> pprust_ast::PpAnn for IdentifiedAnnotation<'tcx> {
} }
} }
impl<'tcx> pprust_hir::PpAnn for IdentifiedAnnotation<'tcx> { struct HirIdentifiedAnn<'tcx> {
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) { tcx: TyCtxt<'tcx>,
if let Some(ref tcx) = self.tcx {
pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
} }
impl<'tcx> pprust_hir::PpAnn for HirIdentifiedAnn<'tcx> {
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
pprust_hir::PpAnn::nested(
&(&self.tcx.hir() as &dyn hir::intravisit::Map<'_>),
state,
nested,
)
} }
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) { fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
@ -119,11 +128,11 @@ impl<'tcx> pprust_hir::PpAnn for IdentifiedAnnotation<'tcx> {
} }
} }
struct HygieneAnnotation<'a> { struct AstHygieneAnn<'a> {
sess: &'a Session, sess: &'a Session,
} }
impl<'a> pprust_ast::PpAnn for HygieneAnnotation<'a> { impl<'a> pprust_ast::PpAnn for AstHygieneAnn<'a> {
fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) { fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
match node { match node {
pprust_ast::AnnNode::Ident(&Ident { name, span }) => { pprust_ast::AnnNode::Ident(&Ident { name, span }) => {
@ -145,12 +154,12 @@ impl<'a> pprust_ast::PpAnn for HygieneAnnotation<'a> {
} }
} }
struct TypedAnnotation<'tcx> { struct HirTypedAnn<'tcx> {
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
maybe_typeck_results: Cell<Option<&'tcx ty::TypeckResults<'tcx>>>, maybe_typeck_results: Cell<Option<&'tcx ty::TypeckResults<'tcx>>>,
} }
impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> { impl<'tcx> pprust_hir::PpAnn for HirTypedAnn<'tcx> {
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) { fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
let old_maybe_typeck_results = self.maybe_typeck_results.get(); let old_maybe_typeck_results = self.maybe_typeck_results.get();
if let pprust_hir::Nested::Body(id) = nested { if let pprust_hir::Nested::Body(id) = nested {
@ -242,11 +251,11 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
Source(s) => { Source(s) => {
debug!("pretty printing source code {:?}", s); debug!("pretty printing source code {:?}", s);
let annotation: Box<dyn pprust_ast::PpAnn> = match s { let annotation: Box<dyn pprust_ast::PpAnn> = match s {
Normal => Box::new(NoAnn { tcx: None }), Normal => Box::new(AstNoAnn),
Expanded => Box::new(NoAnn { tcx: Some(ex.tcx()) }), Expanded => Box::new(AstNoAnn),
Identified => Box::new(IdentifiedAnnotation { tcx: None }), Identified => Box::new(AstIdentifiedAnn),
ExpandedIdentified => Box::new(IdentifiedAnnotation { tcx: Some(ex.tcx()) }), ExpandedIdentified => Box::new(AstIdentifiedAnn),
ExpandedHygiene => Box::new(HygieneAnnotation { sess }), ExpandedHygiene => Box::new(AstHygieneAnn { sess }),
}; };
let parse = &sess.parse_sess; let parse = &sess.parse_sess;
let is_expanded = ppm.needs_ast_map(); let is_expanded = ppm.needs_ast_map();
@ -289,15 +298,15 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
}; };
match s { match s {
PpHirMode::Normal => { PpHirMode::Normal => {
let annotation = NoAnn { tcx: Some(tcx) }; let annotation = HirNoAnn { tcx };
f(&annotation) f(&annotation)
} }
PpHirMode::Identified => { PpHirMode::Identified => {
let annotation = IdentifiedAnnotation { tcx: Some(tcx) }; let annotation = HirIdentifiedAnn { tcx };
f(&annotation) f(&annotation)
} }
PpHirMode::Typed => { PpHirMode::Typed => {
let annotation = TypedAnnotation { tcx, maybe_typeck_results: Cell::new(None) }; let annotation = HirTypedAnn { tcx, maybe_typeck_results: Cell::new(None) };
tcx.dep_graph.with_ignore(|| f(&annotation)) tcx.dep_graph.with_ignore(|| f(&annotation))
} }
} }