From ef8701a4a02e21230401ace80a19169f9f9a6ff8 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 10 Oct 2023 10:01:52 +1100 Subject: [PATCH 01/12] Rename some things. - Rename `pprust` as `pprust_ast`, to align with `pprust_hir`. - Rename `PrinterSupport` as `AstPrinterSupport`, to align with `HirPrinterSupport`. --- compiler/rustc_driver_impl/src/pretty.rs | 70 ++++++++++++------------ 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/compiler/rustc_driver_impl/src/pretty.rs b/compiler/rustc_driver_impl/src/pretty.rs index 222c7b5d6a72..5d49c78dea46 100644 --- a/compiler/rustc_driver_impl/src/pretty.rs +++ b/compiler/rustc_driver_impl/src/pretty.rs @@ -1,7 +1,7 @@ //! The various pretty-printing routines. use rustc_ast as ast; -use rustc_ast_pretty::pprust; +use rustc_ast_pretty::pprust as pprust_ast; use rustc_errors::ErrorGuaranteed; use rustc_hir as hir; use rustc_hir_pretty as pprust_hir; @@ -26,26 +26,25 @@ use crate::abort_on_err; // analysis results) on to the chosen pretty-printer, along with the // `&PpAnn` object. // -// Note that since the `&PrinterSupport` is freshly constructed on each +// Note that since the `&AstPrinterSupport` is freshly constructed on each // call, it would not make sense to try to attach the lifetime of `self` // to the lifetime of the `&PrinterObject`. -/// Constructs a `PrinterSupport` object and passes it to `f`. -fn call_with_pp_support<'tcx, A, F>( +/// Constructs an `AstPrinterSupport` object and passes it to `f`. +fn call_with_pp_support_ast<'tcx, A, F>( ppmode: &PpSourceMode, sess: &'tcx Session, tcx: Option>, f: F, ) -> A where - F: FnOnce(&dyn PrinterSupport) -> A, + F: FnOnce(&dyn AstPrinterSupport) -> A, { match *ppmode { Normal | Expanded => { let annotation = NoAnn { sess, tcx }; f(&annotation) } - Identified | ExpandedIdentified => { let annotation = IdentifiedAnnotation { sess, tcx }; f(&annotation) @@ -65,7 +64,6 @@ where let annotation = NoAnn { sess: tcx.sess, tcx: Some(tcx) }; f(&annotation, tcx.hir()) } - PpHirMode::Identified => { let annotation = IdentifiedAnnotation { sess: tcx.sess, tcx: Some(tcx) }; f(&annotation, tcx.hir()) @@ -79,7 +77,7 @@ where } } -trait PrinterSupport: pprust::PpAnn { +trait AstPrinterSupport: pprust_ast::PpAnn { /// Provides a uniform interface for re-extracting a reference to a /// `Session` from a value that now owns it. fn sess(&self) -> &Session; @@ -88,7 +86,7 @@ trait PrinterSupport: pprust::PpAnn { /// /// (Rust does not yet support upcasting from a trait object to /// an object for one of its supertraits.) - fn pp_ann(&self) -> &dyn pprust::PpAnn; + fn pp_ann(&self) -> &dyn pprust_ast::PpAnn; } trait HirPrinterSupport<'hir>: pprust_hir::PpAnn { @@ -112,12 +110,12 @@ struct NoAnn<'hir> { tcx: Option>, } -impl<'hir> PrinterSupport for NoAnn<'hir> { +impl<'hir> AstPrinterSupport for NoAnn<'hir> { fn sess(&self) -> &Session { self.sess } - fn pp_ann(&self) -> &dyn pprust::PpAnn { + fn pp_ann(&self) -> &dyn pprust_ast::PpAnn { self } } @@ -136,7 +134,7 @@ impl<'hir> HirPrinterSupport<'hir> for NoAnn<'hir> { } } -impl<'hir> pprust::PpAnn for NoAnn<'hir> {} +impl<'hir> pprust_ast::PpAnn for NoAnn<'hir> {} impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> { fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) { if let Some(tcx) = self.tcx { @@ -150,44 +148,46 @@ struct IdentifiedAnnotation<'hir> { tcx: Option>, } -impl<'hir> PrinterSupport for IdentifiedAnnotation<'hir> { +impl<'hir> AstPrinterSupport for IdentifiedAnnotation<'hir> { fn sess(&self) -> &Session { self.sess } - fn pp_ann(&self) -> &dyn pprust::PpAnn { + fn pp_ann(&self) -> &dyn pprust_ast::PpAnn { self } } -impl<'hir> pprust::PpAnn for IdentifiedAnnotation<'hir> { - fn pre(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) { - if let pprust::AnnNode::Expr(_) = node { +impl<'hir> pprust_ast::PpAnn for IdentifiedAnnotation<'hir> { + fn pre(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) { + if let pprust_ast::AnnNode::Expr(_) = node { s.popen(); } } - fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) { + fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) { match node { - pprust::AnnNode::Crate(_) | pprust::AnnNode::Ident(_) | pprust::AnnNode::Name(_) => {} + pprust_ast::AnnNode::Crate(_) + | pprust_ast::AnnNode::Ident(_) + | pprust_ast::AnnNode::Name(_) => {} - pprust::AnnNode::Item(item) => { + pprust_ast::AnnNode::Item(item) => { s.s.space(); s.synth_comment(item.id.to_string()) } - pprust::AnnNode::SubItem(id) => { + pprust_ast::AnnNode::SubItem(id) => { s.s.space(); s.synth_comment(id.to_string()) } - pprust::AnnNode::Block(blk) => { + pprust_ast::AnnNode::Block(blk) => { s.s.space(); s.synth_comment(format!("block {}", blk.id)) } - pprust::AnnNode::Expr(expr) => { + pprust_ast::AnnNode::Expr(expr) => { s.s.space(); s.synth_comment(expr.id.to_string()); s.pclose() } - pprust::AnnNode::Pat(pat) => { + pprust_ast::AnnNode::Pat(pat) => { s.s.space(); s.synth_comment(format!("pat {}", pat.id)); } @@ -256,28 +256,28 @@ struct HygieneAnnotation<'a> { sess: &'a Session, } -impl<'a> PrinterSupport for HygieneAnnotation<'a> { +impl<'a> AstPrinterSupport for HygieneAnnotation<'a> { fn sess(&self) -> &Session { self.sess } - fn pp_ann(&self) -> &dyn pprust::PpAnn { + fn pp_ann(&self) -> &dyn pprust_ast::PpAnn { self } } -impl<'a> pprust::PpAnn for HygieneAnnotation<'a> { - fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) { +impl<'a> pprust_ast::PpAnn for HygieneAnnotation<'a> { + fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) { match node { - pprust::AnnNode::Ident(&Ident { name, span }) => { + pprust_ast::AnnNode::Ident(&Ident { name, span }) => { s.s.space(); s.synth_comment(format!("{}{:?}", name.as_u32(), span.ctxt())) } - pprust::AnnNode::Name(&name) => { + pprust_ast::AnnNode::Name(&name) => { s.s.space(); s.synth_comment(name.as_u32().to_string()) } - pprust::AnnNode::Crate(_) => { + pprust_ast::AnnNode::Crate(_) => { s.s.hardbreak(); let verbose = self.sess.verbose(); s.synth_comment(rustc_span::hygiene::debug_hygiene_data(verbose)); @@ -366,11 +366,11 @@ pub fn print_after_parsing(sess: &Session, krate: &ast::Crate, ppm: PpMode) { let out = match ppm { Source(s) => { // Silently ignores an identified node. - call_with_pp_support(&s, sess, None, move |annotation| { + call_with_pp_support_ast(&s, sess, None, move |annotation| { debug!("pretty printing source code {:?}", s); let sess = annotation.sess(); let parse = &sess.parse_sess; - pprust::print_crate( + pprust_ast::print_crate( sess.source_map(), krate, src_name, @@ -403,11 +403,11 @@ pub fn print_after_hir_lowering<'tcx>(tcx: TyCtxt<'tcx>, ppm: PpMode) { let out = match ppm { Source(s) => { // Silently ignores an identified node. - call_with_pp_support(&s, tcx.sess, Some(tcx), move |annotation| { + call_with_pp_support_ast(&s, tcx.sess, Some(tcx), move |annotation| { debug!("pretty printing source code {:?}", s); let sess = annotation.sess(); let parse = &sess.parse_sess; - pprust::print_crate( + pprust_ast::print_crate( sess.source_map(), &tcx.resolver_for_lowering(()).borrow().1, src_name, From c5cfcdc4ac0880b171dc0004c660e3a6fd315ee0 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 10 Oct 2023 10:53:12 +1100 Subject: [PATCH 02/12] Remove unnecessary call to `call_with_pp_support_hir`. The callback is trivial and no pp support is actually needed. This makes the `HirTree` case more like the `AstTree` case above. --- compiler/rustc_driver_impl/src/pretty.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_driver_impl/src/pretty.rs b/compiler/rustc_driver_impl/src/pretty.rs index 5d49c78dea46..bae0d3dd6a61 100644 --- a/compiler/rustc_driver_impl/src/pretty.rs +++ b/compiler/rustc_driver_impl/src/pretty.rs @@ -441,10 +441,8 @@ pub fn print_after_hir_lowering<'tcx>(tcx: TyCtxt<'tcx>, ppm: PpMode) { }), HirTree => { - call_with_pp_support_hir(&PpHirMode::Normal, tcx, move |_annotation, hir_map| { - debug!("pretty printing HIR tree"); - format!("{:#?}", hir_map.krate()) - }) + debug!("pretty printing HIR tree"); + format!("{:#?}", tcx.hir().krate()) } _ => unreachable!(), From 87090a97e3de431bf15832d9389bf07a969e2791 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 10 Oct 2023 10:54:36 +1100 Subject: [PATCH 03/12] Remove an outdated comment. `phase_3_run_analysis_passes` no longer exists, and AFAICT this code has been refactored so much since this comment was written that it no longer has any useful meaning. --- compiler/rustc_driver_impl/src/pretty.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/compiler/rustc_driver_impl/src/pretty.rs b/compiler/rustc_driver_impl/src/pretty.rs index bae0d3dd6a61..4a5198080a20 100644 --- a/compiler/rustc_driver_impl/src/pretty.rs +++ b/compiler/rustc_driver_impl/src/pretty.rs @@ -451,10 +451,6 @@ pub fn print_after_hir_lowering<'tcx>(tcx: TyCtxt<'tcx>, ppm: PpMode) { write_or_print(&out, tcx.sess); } -// In an ideal world, this would be a public function called by the driver after -// analysis is performed. However, we want to call `phase_3_run_analysis_passes` -// with a different callback than the standard driver, so that isn't easy. -// Instead, we call that function ourselves. fn print_with_analysis(tcx: TyCtxt<'_>, ppm: PpMode) -> Result<(), ErrorGuaranteed> { tcx.analysis(())?; let out = match ppm { From 1467ba06b6b50633541dca9ddebcdacd16a499b1 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 10 Oct 2023 12:17:06 +1100 Subject: [PATCH 04/12] Remove PpAstTreeMode. It's simpler to distinguish the two AST modes directly in `PpMode`. --- compiler/rustc_driver_impl/src/pretty.rs | 6 +++--- compiler/rustc_session/src/config.rs | 23 +++++++++-------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_driver_impl/src/pretty.rs b/compiler/rustc_driver_impl/src/pretty.rs index 4a5198080a20..b258643e97c1 100644 --- a/compiler/rustc_driver_impl/src/pretty.rs +++ b/compiler/rustc_driver_impl/src/pretty.rs @@ -8,7 +8,7 @@ use rustc_hir_pretty as pprust_hir; use rustc_middle::hir::map as hir_map; use rustc_middle::mir::{write_mir_graphviz, write_mir_pretty}; use rustc_middle::ty::{self, TyCtxt}; -use rustc_session::config::{OutFileName, PpAstTreeMode, PpHirMode, PpMode, PpSourceMode}; +use rustc_session::config::{OutFileName, PpHirMode, PpMode, PpSourceMode}; use rustc_session::Session; use rustc_span::symbol::Ident; use rustc_span::FileName; @@ -382,7 +382,7 @@ pub fn print_after_parsing(sess: &Session, krate: &ast::Crate, ppm: PpMode) { ) }) } - AstTree(PpAstTreeMode::Normal) => { + AstTree => { debug!("pretty printing AST tree"); format!("{krate:#?}") } @@ -420,7 +420,7 @@ pub fn print_after_hir_lowering<'tcx>(tcx: TyCtxt<'tcx>, ppm: PpMode) { }) } - AstTree(PpAstTreeMode::Expanded) => { + AstTreeExpanded => { debug!("pretty-printing expanded AST"); format!("{:#?}", tcx.resolver_for_lowering(()).borrow().1) } diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 58461856eb1e..056986a81441 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -2925,8 +2925,8 @@ fn parse_pretty(handler: &EarlyErrorHandler, unstable_opts: &UnstableOptions) -> "expanded" => Source(PpSourceMode::Expanded), "expanded,identified" => Source(PpSourceMode::ExpandedIdentified), "expanded,hygiene" => Source(PpSourceMode::ExpandedHygiene), - "ast-tree" => AstTree(PpAstTreeMode::Normal), - "ast-tree,expanded" => AstTree(PpAstTreeMode::Expanded), + "ast-tree" => AstTree, + "ast-tree,expanded" => AstTreeExpanded, "hir" => Hir(PpHirMode::Normal), "hir,identified" => Hir(PpHirMode::Identified), "hir,typed" => Hir(PpHirMode::Typed), @@ -3083,14 +3083,6 @@ pub enum PpSourceMode { ExpandedHygiene, } -#[derive(Copy, Clone, PartialEq, Debug)] -pub enum PpAstTreeMode { - /// `-Zunpretty=ast` - Normal, - /// `-Zunpretty=ast,expanded` - Expanded, -} - #[derive(Copy, Clone, PartialEq, Debug)] pub enum PpHirMode { /// `-Zunpretty=hir` @@ -3106,7 +3098,10 @@ pub enum PpMode { /// Options that print the source code, i.e. /// `-Zunpretty=normal` and `-Zunpretty=expanded` Source(PpSourceMode), - AstTree(PpAstTreeMode), + /// `-Zunpretty=ast-tree` + AstTree, + /// `-Zunpretty=ast-tree,expanded` + AstTreeExpanded, /// Options that print the HIR, i.e. `-Zunpretty=hir` Hir(PpHirMode), /// `-Zunpretty=hir-tree` @@ -3126,10 +3121,10 @@ impl PpMode { use PpMode::*; use PpSourceMode::*; match *self { - Source(Normal | Identified) | AstTree(PpAstTreeMode::Normal) => false, + Source(Normal | Identified) | AstTree => false, Source(Expanded | ExpandedIdentified | ExpandedHygiene) - | AstTree(PpAstTreeMode::Expanded) + | AstTreeExpanded | Hir(_) | HirTree | ThirTree @@ -3141,7 +3136,7 @@ impl PpMode { pub fn needs_hir(&self) -> bool { use PpMode::*; match *self { - Source(_) | AstTree(_) => false, + Source(_) | AstTree | AstTreeExpanded => false, Hir(_) | HirTree | ThirTree | ThirFlat | Mir | MirCFG => true, } From d5e7c5f3cc9919bf1ad0b61568d74c65639d4e4e Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 10 Oct 2023 12:22:22 +1100 Subject: [PATCH 05/12] Remove unused `PrinterSupport::hir_map` method. --- compiler/rustc_driver_impl/src/pretty.rs | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/compiler/rustc_driver_impl/src/pretty.rs b/compiler/rustc_driver_impl/src/pretty.rs index b258643e97c1..b647bf1c3139 100644 --- a/compiler/rustc_driver_impl/src/pretty.rs +++ b/compiler/rustc_driver_impl/src/pretty.rs @@ -94,10 +94,6 @@ trait HirPrinterSupport<'hir>: pprust_hir::PpAnn { /// `Session` from a value that now owns it. fn sess(&self) -> &Session; - /// Provides a uniform interface for re-extracting a reference to an - /// `hir_map::Map` from a value that now owns it. - fn hir_map(&self) -> Option>; - /// Produces the pretty-print annotation object. /// /// (Rust does not yet support upcasting from a trait object to @@ -125,10 +121,6 @@ impl<'hir> HirPrinterSupport<'hir> for NoAnn<'hir> { self.sess } - fn hir_map(&self) -> Option> { - self.tcx.map(|tcx| tcx.hir()) - } - fn pp_ann(&self) -> &dyn pprust_hir::PpAnn { self } @@ -200,10 +192,6 @@ impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> { self.sess } - fn hir_map(&self) -> Option> { - self.tcx.map(|tcx| tcx.hir()) - } - fn pp_ann(&self) -> &dyn pprust_hir::PpAnn { self } @@ -298,10 +286,6 @@ impl<'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'tcx> { self.tcx.sess } - fn hir_map(&self) -> Option> { - Some(self.tcx.hir()) - } - fn pp_ann(&self) -> &dyn pprust_hir::PpAnn { self } From e3d8bbbfe29ab73afe1d258d6f69c2d6840a30ff Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 10 Oct 2023 12:58:12 +1100 Subject: [PATCH 06/12] Simplify support traits. First, both `AstPrinterSupport` and `HirPrinterSupport` have a `sess` method. This commit introduces a `Sess` trait and makes the support traits be subtraits of `Sess`, to avoid some duplication. Second, both support traits have a `pp_ann` method that isn't needed if we enable `trait_upcasting`. This commit removes those methods. (Both of these traits will be removed in a subsequent commit, as will the `trait_upcasting` use.) --- compiler/rustc_driver_impl/src/lib.rs | 5 +- compiler/rustc_driver_impl/src/pretty.rs | 95 +++++++----------------- 2 files changed, 30 insertions(+), 70 deletions(-) diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index 17556c45296e..bbecf1a1093f 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -8,10 +8,11 @@ #![cfg_attr(not(bootstrap), doc(rust_logo))] #![cfg_attr(not(bootstrap), feature(rustdoc_internals))] #![cfg_attr(not(bootstrap), allow(internal_features))] -#![feature(lazy_cell)] #![feature(decl_macro)] -#![feature(panic_update_hook)] +#![feature(lazy_cell)] #![feature(let_chains)] +#![feature(panic_update_hook)] +#![feature(trait_upcasting)] #![recursion_limit = "256"] #![allow(rustc::potential_query_instability)] #![deny(rustc::untranslatable_diagnostic)] diff --git a/compiler/rustc_driver_impl/src/pretty.rs b/compiler/rustc_driver_impl/src/pretty.rs index b647bf1c3139..ec118bd33eb9 100644 --- a/compiler/rustc_driver_impl/src/pretty.rs +++ b/compiler/rustc_driver_impl/src/pretty.rs @@ -55,9 +55,10 @@ where } } } + fn call_with_pp_support_hir(ppmode: &PpHirMode, tcx: TyCtxt<'_>, f: F) -> A where - F: FnOnce(&dyn HirPrinterSupport<'_>, hir_map::Map<'_>) -> A, + F: FnOnce(&dyn HirPrinterSupport, hir_map::Map<'_>) -> A, { match *ppmode { PpHirMode::Normal => { @@ -77,54 +78,28 @@ where } } -trait AstPrinterSupport: pprust_ast::PpAnn { +trait Sess { /// Provides a uniform interface for re-extracting a reference to a - /// `Session` from a value that now owns it. + /// `Session`. fn sess(&self) -> &Session; - - /// Produces the pretty-print annotation object. - /// - /// (Rust does not yet support upcasting from a trait object to - /// an object for one of its supertraits.) - fn pp_ann(&self) -> &dyn pprust_ast::PpAnn; } -trait HirPrinterSupport<'hir>: pprust_hir::PpAnn { - /// Provides a uniform interface for re-extracting a reference to a - /// `Session` from a value that now owns it. - fn sess(&self) -> &Session; - - /// Produces the pretty-print annotation object. - /// - /// (Rust does not yet support upcasting from a trait object to - /// an object for one of its supertraits.) - fn pp_ann(&self) -> &dyn pprust_hir::PpAnn; -} +trait AstPrinterSupport: pprust_ast::PpAnn + Sess {} +trait HirPrinterSupport: pprust_hir::PpAnn + Sess {} struct NoAnn<'hir> { sess: &'hir Session, tcx: Option>, } -impl<'hir> AstPrinterSupport for NoAnn<'hir> { +impl<'hir> Sess for NoAnn<'hir> { fn sess(&self) -> &Session { self.sess } - - fn pp_ann(&self) -> &dyn pprust_ast::PpAnn { - self - } } -impl<'hir> HirPrinterSupport<'hir> for NoAnn<'hir> { - fn sess(&self) -> &Session { - self.sess - } - - fn pp_ann(&self) -> &dyn pprust_hir::PpAnn { - self - } -} +impl<'tcx> AstPrinterSupport for NoAnn<'tcx> {} +impl<'hir> HirPrinterSupport for NoAnn<'hir> {} impl<'hir> pprust_ast::PpAnn for NoAnn<'hir> {} impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> { @@ -140,22 +115,21 @@ struct IdentifiedAnnotation<'hir> { tcx: Option>, } -impl<'hir> AstPrinterSupport for IdentifiedAnnotation<'hir> { +impl<'hir> Sess for IdentifiedAnnotation<'hir> { fn sess(&self) -> &Session { self.sess } - - fn pp_ann(&self) -> &dyn pprust_ast::PpAnn { - self - } } +impl<'hir> AstPrinterSupport for IdentifiedAnnotation<'hir> {} + impl<'hir> pprust_ast::PpAnn for IdentifiedAnnotation<'hir> { fn pre(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) { if let pprust_ast::AnnNode::Expr(_) = node { s.popen(); } } + fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) { match node { pprust_ast::AnnNode::Crate(_) @@ -187,15 +161,7 @@ impl<'hir> pprust_ast::PpAnn for IdentifiedAnnotation<'hir> { } } -impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> { - fn sess(&self) -> &Session { - self.sess - } - - fn pp_ann(&self) -> &dyn pprust_hir::PpAnn { - self - } -} +impl<'hir> HirPrinterSupport for IdentifiedAnnotation<'hir> {} impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> { fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) { @@ -203,11 +169,13 @@ impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> { pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested) } } + fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) { if let pprust_hir::AnnNode::Expr(_) = node { s.popen(); } } + fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) { match node { pprust_hir::AnnNode::Name(_) => {} @@ -244,16 +212,14 @@ struct HygieneAnnotation<'a> { sess: &'a Session, } -impl<'a> AstPrinterSupport for HygieneAnnotation<'a> { +impl<'a> Sess for HygieneAnnotation<'a> { fn sess(&self) -> &Session { self.sess } - - fn pp_ann(&self) -> &dyn pprust_ast::PpAnn { - self - } } +impl<'a> AstPrinterSupport for HygieneAnnotation<'a> {} + impl<'a> pprust_ast::PpAnn for HygieneAnnotation<'a> { fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) { match node { @@ -281,16 +247,14 @@ struct TypedAnnotation<'tcx> { maybe_typeck_results: Cell>>, } -impl<'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'tcx> { +impl<'tcx> Sess for TypedAnnotation<'tcx> { fn sess(&self) -> &Session { self.tcx.sess } - - fn pp_ann(&self) -> &dyn pprust_hir::PpAnn { - self - } } +impl<'tcx> HirPrinterSupport for TypedAnnotation<'tcx> {} + impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> { fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) { let old_maybe_typeck_results = self.maybe_typeck_results.get(); @@ -301,11 +265,13 @@ impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> { pprust_hir::PpAnn::nested(pp_ann, state, nested); self.maybe_typeck_results.set(old_maybe_typeck_results); } + fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) { if let pprust_hir::AnnNode::Expr(_) = node { s.popen(); } } + fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) { if let pprust_hir::AnnNode::Expr(expr) = node { let typeck_results = self.maybe_typeck_results.get().or_else(|| { @@ -359,7 +325,7 @@ pub fn print_after_parsing(sess: &Session, krate: &ast::Crate, ppm: PpMode) { krate, src_name, src, - annotation.pp_ann(), + annotation, false, parse.edition, &sess.parse_sess.attr_id_generator, @@ -396,7 +362,7 @@ pub fn print_after_hir_lowering<'tcx>(tcx: TyCtxt<'tcx>, ppm: PpMode) { &tcx.resolver_for_lowering(()).borrow().1, src_name, src, - annotation.pp_ann(), + annotation, true, parse.edition, &sess.parse_sess.attr_id_generator, @@ -414,14 +380,7 @@ pub fn print_after_hir_lowering<'tcx>(tcx: TyCtxt<'tcx>, ppm: PpMode) { let sess = annotation.sess(); let sm = sess.source_map(); let attrs = |id| hir_map.attrs(id); - pprust_hir::print_crate( - sm, - hir_map.root_module(), - src_name, - src, - &attrs, - annotation.pp_ann(), - ) + pprust_hir::print_crate(sm, hir_map.root_module(), src_name, src, &attrs, annotation) }), HirTree => { From 7d145a0fde3771662648df2de63e8f64033bf598 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 11 Oct 2023 08:49:24 +1100 Subject: [PATCH 07/12] Merge `print_*` functions. The handling of the `PpMode` variants is currently spread across three functions: `print_after_parsing`, `print_after_hir_lowering`, and `print_with_analysis`. Each one handles some of the variants. This split is primarily because `print_after_parsing` has slightly different arguments to the other two. This commit changes the structure. It merges the three functions into a single `print` function, and encapsulates the different arguments in a new enum `PrintExtra`. Benefits: - The code is a little shorter. - All the `PpMode` variants are handled in a single `match`, with no need for `unreachable!` arms. - It enables the trait removal in the subsequent commit by reducing the number of `call_with_pp_support_ast` call sites from two to one. --- compiler/rustc_driver_impl/src/lib.rs | 4 +- compiler/rustc_driver_impl/src/pretty.rs | 118 ++++++++++------------- 2 files changed, 52 insertions(+), 70 deletions(-) diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index bbecf1a1093f..1cddd561ded0 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -396,7 +396,7 @@ fn run_compiler( if ppm.needs_ast_map() { queries.global_ctxt()?.enter(|tcx| { tcx.ensure().early_lint_checks(()); - pretty::print_after_hir_lowering(tcx, *ppm); + pretty::print(sess, *ppm, pretty::PrintExtra::NeedsAstMap { tcx }); Ok(()) })?; @@ -405,7 +405,7 @@ fn run_compiler( queries.global_ctxt()?.enter(|tcx| tcx.output_filenames(())); } else { let krate = queries.parse()?.steal(); - pretty::print_after_parsing(sess, &krate, *ppm); + pretty::print(sess, *ppm, pretty::PrintExtra::AfterParsing { krate }); } trace!("finished pretty-printing"); return early_exit(); diff --git a/compiler/rustc_driver_impl/src/pretty.rs b/compiler/rustc_driver_impl/src/pretty.rs index ec118bd33eb9..837f725abe92 100644 --- a/compiler/rustc_driver_impl/src/pretty.rs +++ b/compiler/rustc_driver_impl/src/pretty.rs @@ -2,9 +2,9 @@ use rustc_ast as ast; use rustc_ast_pretty::pprust as pprust_ast; -use rustc_errors::ErrorGuaranteed; use rustc_hir as hir; use rustc_hir_pretty as pprust_hir; +use rustc_middle::bug; use rustc_middle::hir::map as hir_map; use rustc_middle::mir::{write_mir_graphviz, write_mir_pretty}; use rustc_middle::ty::{self, TyCtxt}; @@ -310,106 +310,92 @@ fn write_or_print(out: &str, sess: &Session) { sess.io.output_file.as_ref().unwrap_or(&OutFileName::Stdout).overwrite(out, sess); } -pub fn print_after_parsing(sess: &Session, krate: &ast::Crate, ppm: PpMode) { - let (src, src_name) = get_source(sess); +// Extra data for pretty-printing, the form of which depends on what kind of +// pretty-printing we are doing. +pub enum PrintExtra<'tcx> { + AfterParsing { krate: ast::Crate }, + NeedsAstMap { tcx: TyCtxt<'tcx> }, +} - let out = match ppm { - Source(s) => { - // Silently ignores an identified node. - call_with_pp_support_ast(&s, sess, None, move |annotation| { - debug!("pretty printing source code {:?}", s); - let sess = annotation.sess(); - let parse = &sess.parse_sess; - pprust_ast::print_crate( - sess.source_map(), - krate, - src_name, - src, - annotation, - false, - parse.edition, - &sess.parse_sess.attr_id_generator, - ) - }) +impl<'tcx> PrintExtra<'tcx> { + fn with_krate(&self, f: F) -> R + where + F: FnOnce(&ast::Crate) -> R + { + match self { + PrintExtra::AfterParsing { krate, .. } => f(krate), + PrintExtra::NeedsAstMap { tcx } => f(&tcx.resolver_for_lowering(()).borrow().1), } - AstTree => { - debug!("pretty printing AST tree"); - format!("{krate:#?}") - } - _ => unreachable!(), - }; + } - write_or_print(&out, sess); + fn tcx(&self) -> TyCtxt<'tcx> { + match self { + PrintExtra::AfterParsing { .. } => bug!("PrintExtra::tcx"), + PrintExtra::NeedsAstMap { tcx } => *tcx, + } + } } -pub fn print_after_hir_lowering<'tcx>(tcx: TyCtxt<'tcx>, ppm: PpMode) { +pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) { if ppm.needs_analysis() { - abort_on_err(print_with_analysis(tcx, ppm), tcx.sess); - return; + abort_on_err(ex.tcx().analysis(()), sess); } - let (src, src_name) = get_source(tcx.sess); + let (src, src_name) = get_source(sess); let out = match ppm { Source(s) => { // Silently ignores an identified node. - call_with_pp_support_ast(&s, tcx.sess, Some(tcx), move |annotation| { + call_with_pp_support_ast(&s, sess, None, move |annotation| { debug!("pretty printing source code {:?}", s); let sess = annotation.sess(); let parse = &sess.parse_sess; - pprust_ast::print_crate( - sess.source_map(), - &tcx.resolver_for_lowering(()).borrow().1, - src_name, - src, - annotation, - true, - parse.edition, - &sess.parse_sess.attr_id_generator, + let is_expanded = ppm.needs_ast_map(); + ex.with_krate(|krate| + pprust_ast::print_crate( + sess.source_map(), + krate, + src_name, + src, + annotation, + is_expanded, + parse.edition, + &sess.parse_sess.attr_id_generator, + ) ) }) } - + AstTree => { + debug!("pretty printing AST tree"); + ex.with_krate(|krate| format!("{krate:#?}")) + } AstTreeExpanded => { debug!("pretty-printing expanded AST"); - format!("{:#?}", tcx.resolver_for_lowering(()).borrow().1) + format!("{:#?}", ex.tcx().resolver_for_lowering(()).borrow().1) } - - Hir(s) => call_with_pp_support_hir(&s, tcx, move |annotation, hir_map| { + Hir(s) => call_with_pp_support_hir(&s, ex.tcx(), move |annotation, hir_map| { debug!("pretty printing HIR {:?}", s); let sess = annotation.sess(); let sm = sess.source_map(); let attrs = |id| hir_map.attrs(id); pprust_hir::print_crate(sm, hir_map.root_module(), src_name, src, &attrs, annotation) }), - HirTree => { debug!("pretty printing HIR tree"); - format!("{:#?}", tcx.hir().krate()) + format!("{:#?}", ex.tcx().hir().krate()) } - - _ => unreachable!(), - }; - - write_or_print(&out, tcx.sess); -} - -fn print_with_analysis(tcx: TyCtxt<'_>, ppm: PpMode) -> Result<(), ErrorGuaranteed> { - tcx.analysis(())?; - let out = match ppm { Mir => { let mut out = Vec::new(); - write_mir_pretty(tcx, None, &mut out).unwrap(); + write_mir_pretty(ex.tcx(), None, &mut out).unwrap(); String::from_utf8(out).unwrap() } - MirCFG => { let mut out = Vec::new(); - write_mir_graphviz(tcx, None, &mut out).unwrap(); + write_mir_graphviz(ex.tcx(), None, &mut out).unwrap(); String::from_utf8(out).unwrap() } - ThirTree => { + let tcx = ex.tcx(); let mut out = String::new(); abort_on_err(rustc_hir_analysis::check_crate(tcx), tcx.sess); debug!("pretty printing THIR tree"); @@ -418,8 +404,8 @@ fn print_with_analysis(tcx: TyCtxt<'_>, ppm: PpMode) -> Result<(), ErrorGuarante } out } - ThirFlat => { + let tcx = ex.tcx(); let mut out = String::new(); abort_on_err(rustc_hir_analysis::check_crate(tcx), tcx.sess); debug!("pretty printing THIR flat"); @@ -428,11 +414,7 @@ fn print_with_analysis(tcx: TyCtxt<'_>, ppm: PpMode) -> Result<(), ErrorGuarante } out } - - _ => unreachable!(), }; - write_or_print(&out, tcx.sess); - - Ok(()) + write_or_print(&out, sess); } From 060851b7646dff523c25958930b2a7cbe6ce91a1 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 11 Oct 2023 08:55:41 +1100 Subject: [PATCH 08/12] 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. --- compiler/rustc_driver_impl/src/lib.rs | 1 - compiler/rustc_driver_impl/src/pretty.rs | 181 +++++++---------------- 2 files changed, 52 insertions(+), 130 deletions(-) diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index 1cddd561ded0..4074d57433ef 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -12,7 +12,6 @@ #![feature(lazy_cell)] #![feature(let_chains)] #![feature(panic_update_hook)] -#![feature(trait_upcasting)] #![recursion_limit = "256"] #![allow(rustc::potential_query_instability)] #![deny(rustc::untranslatable_diagnostic)] diff --git a/compiler/rustc_driver_impl/src/pretty.rs b/compiler/rustc_driver_impl/src/pretty.rs index 837f725abe92..4145b5baeb01 100644 --- a/compiler/rustc_driver_impl/src/pretty.rs +++ b/compiler/rustc_driver_impl/src/pretty.rs @@ -5,7 +5,6 @@ use rustc_ast_pretty::pprust as pprust_ast; use rustc_hir as hir; use rustc_hir_pretty as pprust_hir; use rustc_middle::bug; -use rustc_middle::hir::map as hir_map; use rustc_middle::mir::{write_mir_graphviz, write_mir_pretty}; use rustc_middle::ty::{self, TyCtxt}; use rustc_session::config::{OutFileName, PpHirMode, PpMode, PpSourceMode}; @@ -20,87 +19,10 @@ pub use self::PpMode::*; pub use self::PpSourceMode::*; use crate::abort_on_err; -// This slightly awkward construction is to allow for each PpMode to -// choose whether it needs to do analyses (which can consume the -// Session) and then pass through the session (now attached to the -// analysis results) on to the chosen pretty-printer, along with the -// `&PpAnn` object. -// -// Note that since the `&AstPrinterSupport` is freshly constructed on each -// call, it would not make sense to try to attach the lifetime of `self` -// to the lifetime of the `&PrinterObject`. - -/// Constructs an `AstPrinterSupport` object and passes it to `f`. -fn call_with_pp_support_ast<'tcx, A, F>( - ppmode: &PpSourceMode, - sess: &'tcx Session, - tcx: Option>, - f: F, -) -> A -where - F: FnOnce(&dyn AstPrinterSupport) -> A, -{ - match *ppmode { - Normal | Expanded => { - let annotation = NoAnn { sess, tcx }; - f(&annotation) - } - Identified | ExpandedIdentified => { - let annotation = IdentifiedAnnotation { sess, tcx }; - f(&annotation) - } - ExpandedHygiene => { - let annotation = HygieneAnnotation { sess }; - f(&annotation) - } - } -} - -fn call_with_pp_support_hir(ppmode: &PpHirMode, tcx: TyCtxt<'_>, f: F) -> A -where - F: FnOnce(&dyn HirPrinterSupport, hir_map::Map<'_>) -> A, -{ - match *ppmode { - PpHirMode::Normal => { - let annotation = NoAnn { sess: tcx.sess, tcx: Some(tcx) }; - f(&annotation, tcx.hir()) - } - PpHirMode::Identified => { - let annotation = IdentifiedAnnotation { sess: tcx.sess, tcx: Some(tcx) }; - f(&annotation, tcx.hir()) - } - PpHirMode::Typed => { - abort_on_err(tcx.analysis(()), tcx.sess); - - let annotation = TypedAnnotation { tcx, maybe_typeck_results: Cell::new(None) }; - tcx.dep_graph.with_ignore(|| f(&annotation, tcx.hir())) - } - } -} - -trait Sess { - /// Provides a uniform interface for re-extracting a reference to a - /// `Session`. - fn sess(&self) -> &Session; -} - -trait AstPrinterSupport: pprust_ast::PpAnn + Sess {} -trait HirPrinterSupport: pprust_hir::PpAnn + Sess {} - struct NoAnn<'hir> { - sess: &'hir Session, tcx: Option>, } -impl<'hir> Sess for NoAnn<'hir> { - fn sess(&self) -> &Session { - self.sess - } -} - -impl<'tcx> AstPrinterSupport for NoAnn<'tcx> {} -impl<'hir> HirPrinterSupport for NoAnn<'hir> {} - impl<'hir> pprust_ast::PpAnn for NoAnn<'hir> {} impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> { fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) { @@ -111,18 +33,9 @@ impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> { } struct IdentifiedAnnotation<'hir> { - sess: &'hir Session, tcx: Option>, } -impl<'hir> Sess for IdentifiedAnnotation<'hir> { - fn sess(&self) -> &Session { - self.sess - } -} - -impl<'hir> AstPrinterSupport for IdentifiedAnnotation<'hir> {} - impl<'hir> pprust_ast::PpAnn for IdentifiedAnnotation<'hir> { fn pre(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) { if let pprust_ast::AnnNode::Expr(_) = node { @@ -161,8 +74,6 @@ impl<'hir> pprust_ast::PpAnn for IdentifiedAnnotation<'hir> { } } -impl<'hir> HirPrinterSupport for IdentifiedAnnotation<'hir> {} - impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> { fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) { if let Some(ref tcx) = self.tcx { @@ -212,14 +123,6 @@ struct HygieneAnnotation<'a> { sess: &'a Session, } -impl<'a> Sess for HygieneAnnotation<'a> { - fn sess(&self) -> &Session { - self.sess - } -} - -impl<'a> AstPrinterSupport for HygieneAnnotation<'a> {} - impl<'a> pprust_ast::PpAnn for HygieneAnnotation<'a> { fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) { match node { @@ -247,14 +150,6 @@ struct TypedAnnotation<'tcx> { maybe_typeck_results: Cell>>, } -impl<'tcx> Sess for TypedAnnotation<'tcx> { - fn sess(&self) -> &Session { - self.tcx.sess - } -} - -impl<'tcx> HirPrinterSupport for TypedAnnotation<'tcx> {} - impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> { fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) { let old_maybe_typeck_results = self.maybe_typeck_results.get(); @@ -320,7 +215,7 @@ pub enum PrintExtra<'tcx> { impl<'tcx> PrintExtra<'tcx> { fn with_krate(&self, f: F) -> R where - F: FnOnce(&ast::Crate) -> R + F: FnOnce(&ast::Crate) -> R, { match self { PrintExtra::AfterParsing { krate, .. } => f(krate), @@ -345,23 +240,26 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) { let out = match ppm { Source(s) => { - // Silently ignores an identified node. - call_with_pp_support_ast(&s, sess, None, move |annotation| { - debug!("pretty printing source code {:?}", s); - let sess = annotation.sess(); - let parse = &sess.parse_sess; - let is_expanded = ppm.needs_ast_map(); - ex.with_krate(|krate| - pprust_ast::print_crate( - sess.source_map(), - krate, - src_name, - src, - annotation, - is_expanded, - parse.edition, - &sess.parse_sess.attr_id_generator, - ) + debug!("pretty printing source code {:?}", s); + let annotation: Box = 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 }), + }; + let parse = &sess.parse_sess; + let is_expanded = ppm.needs_ast_map(); + ex.with_krate(|krate| { + pprust_ast::print_crate( + sess.source_map(), + krate, + src_name, + src, + &*annotation, + is_expanded, + parse.edition, + &sess.parse_sess.attr_id_generator, ) }) } @@ -373,13 +271,38 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) { debug!("pretty-printing expanded AST"); format!("{:#?}", ex.tcx().resolver_for_lowering(()).borrow().1) } - Hir(s) => call_with_pp_support_hir(&s, ex.tcx(), move |annotation, hir_map| { + Hir(s) => { debug!("pretty printing HIR {:?}", s); - let sess = annotation.sess(); - let sm = sess.source_map(); - let attrs = |id| hir_map.attrs(id); - pprust_hir::print_crate(sm, hir_map.root_module(), src_name, src, &attrs, annotation) - }), + let tcx = ex.tcx(); + let f = |annotation: &dyn pprust_hir::PpAnn| { + let sm = sess.source_map(); + let hir_map = tcx.hir(); + let attrs = |id| hir_map.attrs(id); + pprust_hir::print_crate( + sm, + hir_map.root_module(), + src_name, + src, + &attrs, + annotation, + ) + }; + match s { + PpHirMode::Normal => { + let annotation = NoAnn { tcx: Some(tcx) }; + f(&annotation) + } + PpHirMode::Identified => { + let annotation = IdentifiedAnnotation { tcx: Some(tcx) }; + f(&annotation) + } + PpHirMode::Typed => { + abort_on_err(tcx.analysis(()), tcx.sess); + let annotation = TypedAnnotation { tcx, maybe_typeck_results: Cell::new(None) }; + tcx.dep_graph.with_ignore(|| f(&annotation)) + } + } + } HirTree => { debug!("pretty printing HIR tree"); format!("{:#?}", ex.tcx().hir().krate()) From ba58e3213db9626381f5fd014e569f7d2335fa03 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 11 Oct 2023 08:58:27 +1100 Subject: [PATCH 09/12] Rename some `'hir` lifetimes as `'tcx`. Because they all end up within a `TyCtxt`. --- compiler/rustc_driver_impl/src/pretty.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_driver_impl/src/pretty.rs b/compiler/rustc_driver_impl/src/pretty.rs index 4145b5baeb01..daba78612f5c 100644 --- a/compiler/rustc_driver_impl/src/pretty.rs +++ b/compiler/rustc_driver_impl/src/pretty.rs @@ -19,12 +19,12 @@ pub use self::PpMode::*; pub use self::PpSourceMode::*; use crate::abort_on_err; -struct NoAnn<'hir> { - tcx: Option>, +struct NoAnn<'tcx> { + tcx: Option>, } -impl<'hir> pprust_ast::PpAnn for NoAnn<'hir> {} -impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> { +impl<'tcx> pprust_ast::PpAnn for NoAnn<'tcx> {} +impl<'tcx> pprust_hir::PpAnn for NoAnn<'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) @@ -32,11 +32,11 @@ impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> { } } -struct IdentifiedAnnotation<'hir> { - tcx: Option>, +struct IdentifiedAnnotation<'tcx> { + tcx: Option>, } -impl<'hir> pprust_ast::PpAnn for IdentifiedAnnotation<'hir> { +impl<'tcx> pprust_ast::PpAnn for IdentifiedAnnotation<'tcx> { fn pre(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) { if let pprust_ast::AnnNode::Expr(_) = node { s.popen(); @@ -74,7 +74,7 @@ impl<'hir> pprust_ast::PpAnn for IdentifiedAnnotation<'hir> { } } -impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> { +impl<'tcx> pprust_hir::PpAnn for IdentifiedAnnotation<'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) From b65227a9ee38cd21e0b4ffbec7542d2638b661a2 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 11 Oct 2023 09:31:43 +1100 Subject: [PATCH 10/12] Make `needs_analysis` true for `PpHirMode::Typed`. This avoids the need for a bespoke `tcx.analysis()` call. --- compiler/rustc_driver_impl/src/pretty.rs | 1 - compiler/rustc_session/src/config.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/rustc_driver_impl/src/pretty.rs b/compiler/rustc_driver_impl/src/pretty.rs index daba78612f5c..87e3af2f9d9c 100644 --- a/compiler/rustc_driver_impl/src/pretty.rs +++ b/compiler/rustc_driver_impl/src/pretty.rs @@ -297,7 +297,6 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) { f(&annotation) } PpHirMode::Typed => { - abort_on_err(tcx.analysis(()), tcx.sess); let annotation = TypedAnnotation { tcx, maybe_typeck_results: Cell::new(None) }; tcx.dep_graph.with_ignore(|| f(&annotation)) } diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 056986a81441..2a6f5994c490 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -3144,7 +3144,7 @@ impl PpMode { pub fn needs_analysis(&self) -> bool { use PpMode::*; - matches!(*self, Mir | MirCFG | ThirTree | ThirFlat) + matches!(*self, Hir(PpHirMode::Typed) | Mir | MirCFG | ThirTree | ThirFlat) } } From 2e2924f2631bebbf2e0f5f40e26bf5d8d032c6d4 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 11 Oct 2023 09:47:15 +1100 Subject: [PATCH 11/12] 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`. --- compiler/rustc_driver_impl/src/pretty.rs | 63 ++++++++++++++---------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_driver_impl/src/pretty.rs b/compiler/rustc_driver_impl/src/pretty.rs index 87e3af2f9d9c..8c6fee830130 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>, +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>, -} +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>>, } -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 = 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)) } } From 2b4c33817a5aaecabf4c6598d41e190080ec119e Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 6 Oct 2023 18:15:46 +1100 Subject: [PATCH 12/12] Remove unneeded `pub`s. --- compiler/rustc_driver_impl/src/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index 4074d57433ef..7a45ac10f0ba 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -545,7 +545,7 @@ pub enum Compilation { } impl Compilation { - pub fn and_then Compilation>(self, next: F) -> Compilation { + fn and_then Compilation>(self, next: F) -> Compilation { match self { Compilation::Stop => Compilation::Stop, Compilation::Continue => next(), @@ -657,7 +657,7 @@ fn show_md_content_with_pager(content: &str, color: ColorConfig) { } } -pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Compilation { +fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Compilation { if sess.opts.unstable_opts.link_only { if let Input::File(file) = &sess.io.input { let outputs = compiler.build_output_filenames(sess, &[]); @@ -698,7 +698,7 @@ pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Comp } } -pub fn list_metadata( +fn list_metadata( handler: &EarlyErrorHandler, sess: &Session, metadata_loader: &dyn MetadataLoader, @@ -1184,7 +1184,7 @@ fn print_flag_list( /// /// So with all that in mind, the comments below have some more detail about the /// contortions done here to get things to work out correctly. -pub fn handle_options(handler: &EarlyErrorHandler, args: &[String]) -> Option { +fn handle_options(handler: &EarlyErrorHandler, args: &[String]) -> Option { if args.is_empty() { // user did not write `-v` nor `-Z unstable-options`, so do not // include that extra information. @@ -1283,9 +1283,9 @@ pub fn catch_with_exit_code(f: impl FnOnce() -> interface::Result<()>) -> i32 { } } -pub static ICE_PATH: OnceLock> = OnceLock::new(); +static ICE_PATH: OnceLock> = OnceLock::new(); -pub fn ice_path() -> &'static Option { +fn ice_path() -> &'static Option { ICE_PATH.get_or_init(|| { if !rustc_feature::UnstableFeatures::from_environment(None).is_nightly_build() { return None; @@ -1394,7 +1394,7 @@ pub fn install_ice_hook(bug_report_url: &'static str, extra_info: fn(&Handler)) /// /// When `install_ice_hook` is called, this function will be called as the panic /// hook. -pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str, extra_info: fn(&Handler)) { +fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str, extra_info: fn(&Handler)) { let fallback_bundle = rustc_errors::fallback_fluent_bundle(crate::DEFAULT_LOCALE_RESOURCES.to_vec(), false); let emitter = Box::new(rustc_errors::emitter::EmitterWriter::stderr(