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.)
This commit is contained in:
Nicholas Nethercote 2023-10-10 12:58:12 +11:00
parent d5e7c5f3cc
commit e3d8bbbfe2
2 changed files with 30 additions and 70 deletions

View File

@ -8,10 +8,11 @@
#![cfg_attr(not(bootstrap), doc(rust_logo))] #![cfg_attr(not(bootstrap), doc(rust_logo))]
#![cfg_attr(not(bootstrap), feature(rustdoc_internals))] #![cfg_attr(not(bootstrap), feature(rustdoc_internals))]
#![cfg_attr(not(bootstrap), allow(internal_features))] #![cfg_attr(not(bootstrap), allow(internal_features))]
#![feature(lazy_cell)]
#![feature(decl_macro)] #![feature(decl_macro)]
#![feature(panic_update_hook)] #![feature(lazy_cell)]
#![feature(let_chains)] #![feature(let_chains)]
#![feature(panic_update_hook)]
#![feature(trait_upcasting)]
#![recursion_limit = "256"] #![recursion_limit = "256"]
#![allow(rustc::potential_query_instability)] #![allow(rustc::potential_query_instability)]
#![deny(rustc::untranslatable_diagnostic)] #![deny(rustc::untranslatable_diagnostic)]

View File

@ -55,9 +55,10 @@ where
} }
} }
} }
fn call_with_pp_support_hir<A, F>(ppmode: &PpHirMode, tcx: TyCtxt<'_>, f: F) -> A fn call_with_pp_support_hir<A, F>(ppmode: &PpHirMode, tcx: TyCtxt<'_>, f: F) -> A
where where
F: FnOnce(&dyn HirPrinterSupport<'_>, hir_map::Map<'_>) -> A, F: FnOnce(&dyn HirPrinterSupport, hir_map::Map<'_>) -> A,
{ {
match *ppmode { match *ppmode {
PpHirMode::Normal => { 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 /// Provides a uniform interface for re-extracting a reference to a
/// `Session` from a value that now owns it. /// `Session`.
fn sess(&self) -> &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 { trait AstPrinterSupport: pprust_ast::PpAnn + Sess {}
/// Provides a uniform interface for re-extracting a reference to a trait HirPrinterSupport: pprust_hir::PpAnn + Sess {}
/// `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;
}
struct NoAnn<'hir> { struct NoAnn<'hir> {
sess: &'hir Session, sess: &'hir Session,
tcx: Option<TyCtxt<'hir>>, tcx: Option<TyCtxt<'hir>>,
} }
impl<'hir> AstPrinterSupport for NoAnn<'hir> { impl<'hir> Sess for NoAnn<'hir> {
fn sess(&self) -> &Session { fn sess(&self) -> &Session {
self.sess self.sess
} }
fn pp_ann(&self) -> &dyn pprust_ast::PpAnn {
self
}
} }
impl<'hir> HirPrinterSupport<'hir> for NoAnn<'hir> { impl<'tcx> AstPrinterSupport for NoAnn<'tcx> {}
fn sess(&self) -> &Session { impl<'hir> HirPrinterSupport for NoAnn<'hir> {}
self.sess
}
fn pp_ann(&self) -> &dyn pprust_hir::PpAnn {
self
}
}
impl<'hir> pprust_ast::PpAnn for NoAnn<'hir> {} impl<'hir> pprust_ast::PpAnn for NoAnn<'hir> {}
impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> { impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
@ -140,22 +115,21 @@ struct IdentifiedAnnotation<'hir> {
tcx: Option<TyCtxt<'hir>>, tcx: Option<TyCtxt<'hir>>,
} }
impl<'hir> AstPrinterSupport for IdentifiedAnnotation<'hir> { impl<'hir> Sess for IdentifiedAnnotation<'hir> {
fn sess(&self) -> &Session { fn sess(&self) -> &Session {
self.sess 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> { impl<'hir> pprust_ast::PpAnn for IdentifiedAnnotation<'hir> {
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();
} }
} }
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::Crate(_) pprust_ast::AnnNode::Crate(_)
@ -187,15 +161,7 @@ impl<'hir> pprust_ast::PpAnn for IdentifiedAnnotation<'hir> {
} }
} }
impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> { impl<'hir> HirPrinterSupport for IdentifiedAnnotation<'hir> {}
fn sess(&self) -> &Session {
self.sess
}
fn pp_ann(&self) -> &dyn pprust_hir::PpAnn {
self
}
}
impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> { impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) { 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) 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<'_>) { fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
if let pprust_hir::AnnNode::Expr(_) = node { if let pprust_hir::AnnNode::Expr(_) = node {
s.popen(); s.popen();
} }
} }
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) { fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
match node { match node {
pprust_hir::AnnNode::Name(_) => {} pprust_hir::AnnNode::Name(_) => {}
@ -244,16 +212,14 @@ struct HygieneAnnotation<'a> {
sess: &'a Session, sess: &'a Session,
} }
impl<'a> AstPrinterSupport for HygieneAnnotation<'a> { impl<'a> Sess for HygieneAnnotation<'a> {
fn sess(&self) -> &Session { fn sess(&self) -> &Session {
self.sess 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> { impl<'a> pprust_ast::PpAnn for HygieneAnnotation<'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 {
@ -281,16 +247,14 @@ struct TypedAnnotation<'tcx> {
maybe_typeck_results: Cell<Option<&'tcx ty::TypeckResults<'tcx>>>, maybe_typeck_results: Cell<Option<&'tcx ty::TypeckResults<'tcx>>>,
} }
impl<'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'tcx> { impl<'tcx> Sess for TypedAnnotation<'tcx> {
fn sess(&self) -> &Session { fn sess(&self) -> &Session {
self.tcx.sess 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> { impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'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();
@ -301,11 +265,13 @@ impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
pprust_hir::PpAnn::nested(pp_ann, state, nested); pprust_hir::PpAnn::nested(pp_ann, state, nested);
self.maybe_typeck_results.set(old_maybe_typeck_results); self.maybe_typeck_results.set(old_maybe_typeck_results);
} }
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) { fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
if let pprust_hir::AnnNode::Expr(_) = node { if let pprust_hir::AnnNode::Expr(_) = node {
s.popen(); s.popen();
} }
} }
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) { fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
if let pprust_hir::AnnNode::Expr(expr) = node { if let pprust_hir::AnnNode::Expr(expr) = node {
let typeck_results = self.maybe_typeck_results.get().or_else(|| { 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, krate,
src_name, src_name,
src, src,
annotation.pp_ann(), annotation,
false, false,
parse.edition, parse.edition,
&sess.parse_sess.attr_id_generator, &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, &tcx.resolver_for_lowering(()).borrow().1,
src_name, src_name,
src, src,
annotation.pp_ann(), annotation,
true, true,
parse.edition, parse.edition,
&sess.parse_sess.attr_id_generator, &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 sess = annotation.sess();
let sm = sess.source_map(); let sm = sess.source_map();
let attrs = |id| hir_map.attrs(id); let attrs = |id| hir_map.attrs(id);
pprust_hir::print_crate( pprust_hir::print_crate(sm, hir_map.root_module(), src_name, src, &attrs, annotation)
sm,
hir_map.root_module(),
src_name,
src,
&attrs,
annotation.pp_ann(),
)
}), }),
HirTree => { HirTree => {