Skip to content

Commit 7bcb9af

Browse files
committed
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.)
1 parent 1733e8d commit 7bcb9af

File tree

2 files changed

+30
-70
lines changed

2 files changed

+30
-70
lines changed

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
//! This API is completely unstable and subject to change.
66
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
8-
#![feature(lazy_cell)]
98
#![feature(decl_macro)]
10-
#![feature(panic_update_hook)]
9+
#![feature(lazy_cell)]
1110
#![feature(let_chains)]
11+
#![feature(panic_update_hook)]
12+
#![feature(trait_upcasting)]
1213
#![recursion_limit = "256"]
1314
#![allow(rustc::potential_query_instability)]
1415
#![deny(rustc::untranslatable_diagnostic)]

compiler/rustc_driver_impl/src/pretty.rs

Lines changed: 27 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ where
5555
}
5656
}
5757
}
58+
5859
fn call_with_pp_support_hir<A, F>(ppmode: &PpHirMode, tcx: TyCtxt<'_>, f: F) -> A
5960
where
60-
F: FnOnce(&dyn HirPrinterSupport<'_>, hir_map::Map<'_>) -> A,
61+
F: FnOnce(&dyn HirPrinterSupport, hir_map::Map<'_>) -> A,
6162
{
6263
match *ppmode {
6364
PpHirMode::Normal => {
@@ -77,54 +78,28 @@ where
7778
}
7879
}
7980

80-
trait AstPrinterSupport: pprust_ast::PpAnn {
81+
trait Sess {
8182
/// Provides a uniform interface for re-extracting a reference to a
82-
/// `Session` from a value that now owns it.
83+
/// `Session`.
8384
fn sess(&self) -> &Session;
84-
85-
/// Produces the pretty-print annotation object.
86-
///
87-
/// (Rust does not yet support upcasting from a trait object to
88-
/// an object for one of its supertraits.)
89-
fn pp_ann(&self) -> &dyn pprust_ast::PpAnn;
9085
}
9186

92-
trait HirPrinterSupport<'hir>: pprust_hir::PpAnn {
93-
/// Provides a uniform interface for re-extracting a reference to a
94-
/// `Session` from a value that now owns it.
95-
fn sess(&self) -> &Session;
96-
97-
/// Produces the pretty-print annotation object.
98-
///
99-
/// (Rust does not yet support upcasting from a trait object to
100-
/// an object for one of its supertraits.)
101-
fn pp_ann(&self) -> &dyn pprust_hir::PpAnn;
102-
}
87+
trait AstPrinterSupport: pprust_ast::PpAnn + Sess {}
88+
trait HirPrinterSupport: pprust_hir::PpAnn + Sess {}
10389

10490
struct NoAnn<'hir> {
10591
sess: &'hir Session,
10692
tcx: Option<TyCtxt<'hir>>,
10793
}
10894

109-
impl<'hir> AstPrinterSupport for NoAnn<'hir> {
95+
impl<'hir> Sess for NoAnn<'hir> {
11096
fn sess(&self) -> &Session {
11197
self.sess
11298
}
113-
114-
fn pp_ann(&self) -> &dyn pprust_ast::PpAnn {
115-
self
116-
}
11799
}
118100

119-
impl<'hir> HirPrinterSupport<'hir> for NoAnn<'hir> {
120-
fn sess(&self) -> &Session {
121-
self.sess
122-
}
123-
124-
fn pp_ann(&self) -> &dyn pprust_hir::PpAnn {
125-
self
126-
}
127-
}
101+
impl<'tcx> AstPrinterSupport for NoAnn<'tcx> {}
102+
impl<'hir> HirPrinterSupport for NoAnn<'hir> {}
128103

129104
impl<'hir> pprust_ast::PpAnn for NoAnn<'hir> {}
130105
impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
@@ -140,22 +115,21 @@ struct IdentifiedAnnotation<'hir> {
140115
tcx: Option<TyCtxt<'hir>>,
141116
}
142117

143-
impl<'hir> AstPrinterSupport for IdentifiedAnnotation<'hir> {
118+
impl<'hir> Sess for IdentifiedAnnotation<'hir> {
144119
fn sess(&self) -> &Session {
145120
self.sess
146121
}
147-
148-
fn pp_ann(&self) -> &dyn pprust_ast::PpAnn {
149-
self
150-
}
151122
}
152123

124+
impl<'hir> AstPrinterSupport for IdentifiedAnnotation<'hir> {}
125+
153126
impl<'hir> pprust_ast::PpAnn for IdentifiedAnnotation<'hir> {
154127
fn pre(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
155128
if let pprust_ast::AnnNode::Expr(_) = node {
156129
s.popen();
157130
}
158131
}
132+
159133
fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
160134
match node {
161135
pprust_ast::AnnNode::Crate(_)
@@ -187,27 +161,21 @@ impl<'hir> pprust_ast::PpAnn for IdentifiedAnnotation<'hir> {
187161
}
188162
}
189163

190-
impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> {
191-
fn sess(&self) -> &Session {
192-
self.sess
193-
}
194-
195-
fn pp_ann(&self) -> &dyn pprust_hir::PpAnn {
196-
self
197-
}
198-
}
164+
impl<'hir> HirPrinterSupport for IdentifiedAnnotation<'hir> {}
199165

200166
impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
201167
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
202168
if let Some(ref tcx) = self.tcx {
203169
pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
204170
}
205171
}
172+
206173
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
207174
if let pprust_hir::AnnNode::Expr(_) = node {
208175
s.popen();
209176
}
210177
}
178+
211179
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
212180
match node {
213181
pprust_hir::AnnNode::Name(_) => {}
@@ -244,16 +212,14 @@ struct HygieneAnnotation<'a> {
244212
sess: &'a Session,
245213
}
246214

247-
impl<'a> AstPrinterSupport for HygieneAnnotation<'a> {
215+
impl<'a> Sess for HygieneAnnotation<'a> {
248216
fn sess(&self) -> &Session {
249217
self.sess
250218
}
251-
252-
fn pp_ann(&self) -> &dyn pprust_ast::PpAnn {
253-
self
254-
}
255219
}
256220

221+
impl<'a> AstPrinterSupport for HygieneAnnotation<'a> {}
222+
257223
impl<'a> pprust_ast::PpAnn for HygieneAnnotation<'a> {
258224
fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
259225
match node {
@@ -281,16 +247,14 @@ struct TypedAnnotation<'tcx> {
281247
maybe_typeck_results: Cell<Option<&'tcx ty::TypeckResults<'tcx>>>,
282248
}
283249

284-
impl<'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'tcx> {
250+
impl<'tcx> Sess for TypedAnnotation<'tcx> {
285251
fn sess(&self) -> &Session {
286252
self.tcx.sess
287253
}
288-
289-
fn pp_ann(&self) -> &dyn pprust_hir::PpAnn {
290-
self
291-
}
292254
}
293255

256+
impl<'tcx> HirPrinterSupport for TypedAnnotation<'tcx> {}
257+
294258
impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
295259
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
296260
let old_maybe_typeck_results = self.maybe_typeck_results.get();
@@ -301,11 +265,13 @@ impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
301265
pprust_hir::PpAnn::nested(pp_ann, state, nested);
302266
self.maybe_typeck_results.set(old_maybe_typeck_results);
303267
}
268+
304269
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
305270
if let pprust_hir::AnnNode::Expr(_) = node {
306271
s.popen();
307272
}
308273
}
274+
309275
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
310276
if let pprust_hir::AnnNode::Expr(expr) = node {
311277
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) {
359325
krate,
360326
src_name,
361327
src,
362-
annotation.pp_ann(),
328+
annotation,
363329
false,
364330
parse.edition,
365331
&sess.parse_sess.attr_id_generator,
@@ -396,7 +362,7 @@ pub fn print_after_hir_lowering<'tcx>(tcx: TyCtxt<'tcx>, ppm: PpMode) {
396362
&tcx.resolver_for_lowering(()).borrow().1,
397363
src_name,
398364
src,
399-
annotation.pp_ann(),
365+
annotation,
400366
true,
401367
parse.edition,
402368
&sess.parse_sess.attr_id_generator,
@@ -414,14 +380,7 @@ pub fn print_after_hir_lowering<'tcx>(tcx: TyCtxt<'tcx>, ppm: PpMode) {
414380
let sess = annotation.sess();
415381
let sm = sess.source_map();
416382
let attrs = |id| hir_map.attrs(id);
417-
pprust_hir::print_crate(
418-
sm,
419-
hir_map.root_module(),
420-
src_name,
421-
src,
422-
&attrs,
423-
annotation.pp_ann(),
424-
)
383+
pprust_hir::print_crate(sm, hir_map.root_module(), src_name, src, &attrs, annotation)
425384
}),
426385

427386
HirTree => {

0 commit comments

Comments
 (0)