about summary refs log tree commit diff
path: root/compiler/rustc_driver_impl/src
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2023-10-11 09:47:15 +1100
committerNicholas Nethercote <n.nethercote@gmail.com>2023-10-13 06:35:19 +1100
commit2e2924f2631bebbf2e0f5f40e26bf5d8d032c6d4 (patch)
treeca9f9ea876e3deb406934d442606700c62817d0f /compiler/rustc_driver_impl/src
parentb65227a9ee38cd21e0b4ffbec7542d2638b661a2 (diff)
downloadrust-2e2924f2631bebbf2e0f5f40e26bf5d8d032c6d4.tar.gz
rust-2e2924f2631bebbf2e0f5f40e26bf5d8d032c6d4.zip
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`.
Diffstat (limited to 'compiler/rustc_driver_impl/src')
-rw-r--r--compiler/rustc_driver_impl/src/pretty.rs63
1 files changed, 36 insertions, 27 deletions
diff --git a/compiler/rustc_driver_impl/src/pretty.rs b/compiler/rustc_driver_impl/src/pretty.rs
index 87e3af2f9d9..8c6fee83013 100644
--- a/compiler/rustc_driver_impl/src/pretty.rs
+++ b/compiler/rustc_driver_impl/src/pretty.rs
@@ -19,24 +19,27 @@ pub use self::PpMode::*;
 pub use self::PpSourceMode::*;
 use crate::abort_on_err;
 
-struct NoAnn<'tcx> {
-    tcx: Option<TyCtxt<'tcx>>,
+struct AstNoAnn;
+
+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 NoAnn<'tcx> {
+impl<'tcx> pprust_hir::PpAnn for HirNoAnn<'tcx> {
     fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
-        if let Some(tcx) = self.tcx {
-            pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
-        }
+        pprust_hir::PpAnn::nested(
+            &(&self.tcx.hir() as &dyn hir::intravisit::Map<'_>),
+            state,
+            nested,
+        )
     }
 }
 
-struct IdentifiedAnnotation<'tcx> {
-    tcx: Option<TyCtxt<'tcx>>,
-}
+struct AstIdentifiedAnn;
 
-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<'_>) {
         if let pprust_ast::AnnNode::Expr(_) = node {
             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> {
+    tcx: TyCtxt<'tcx>,
+}
+
+impl<'tcx> pprust_hir::PpAnn for HirIdentifiedAnn<'tcx> {
     fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
-        if let Some(ref tcx) = self.tcx {
-            pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, 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<'_>) {
@@ -119,11 +128,11 @@ impl<'tcx> pprust_hir::PpAnn for IdentifiedAnnotation<'tcx> {
     }
 }
 
-struct HygieneAnnotation<'a> {
+struct AstHygieneAnn<'a> {
     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<'_>) {
         match node {
             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>,
     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) {
         let old_maybe_typeck_results = self.maybe_typeck_results.get();
         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) => {
             debug!("pretty printing source code {:?}", s);
             let annotation: Box<dyn pprust_ast::PpAnn> = match s {
-                Normal => Box::new(NoAnn { tcx: None }),
-                Expanded => Box::new(NoAnn { tcx: Some(ex.tcx()) }),
-                Identified => Box::new(IdentifiedAnnotation { tcx: None }),
-                ExpandedIdentified => Box::new(IdentifiedAnnotation { tcx: Some(ex.tcx()) }),
-                ExpandedHygiene => Box::new(HygieneAnnotation { sess }),
+                Normal => Box::new(AstNoAnn),
+                Expanded => Box::new(AstNoAnn),
+                Identified => Box::new(AstIdentifiedAnn),
+                ExpandedIdentified => Box::new(AstIdentifiedAnn),
+                ExpandedHygiene => Box::new(AstHygieneAnn { sess }),
             };
             let parse = &sess.parse_sess;
             let is_expanded = ppm.needs_ast_map();
@@ -289,15 +298,15 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
             };
             match s {
                 PpHirMode::Normal => {
-                    let annotation = NoAnn { tcx: Some(tcx) };
+                    let annotation = HirNoAnn { tcx };
                     f(&annotation)
                 }
                 PpHirMode::Identified => {
-                    let annotation = IdentifiedAnnotation { tcx: Some(tcx) };
+                    let annotation = HirIdentifiedAnn { tcx };
                     f(&annotation)
                 }
                 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))
                 }
             }