Skip to content

Commit 87c7c03

Browse files
committed
syntax: Don't parameterize the the pretty printer
The pretty printer constitues an enormous amount of code, there's no reason for it to be generic. This just least to a huge amount of metadata which isn't necessary. Instead, this change migrates the pretty printer to using a trait object instead. Closes #12985
1 parent 92f0bc2 commit 87c7c03

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

Diff for: src/librustc/driver/driver.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -596,15 +596,15 @@ struct IdentifiedAnnotation;
596596

597597
impl pprust::PpAnn for IdentifiedAnnotation {
598598
fn pre(&self,
599-
s: &mut pprust::State<IdentifiedAnnotation>,
599+
s: &mut pprust::State,
600600
node: pprust::AnnNode) -> io::IoResult<()> {
601601
match node {
602602
pprust::NodeExpr(_) => s.popen(),
603603
_ => Ok(())
604604
}
605605
}
606606
fn post(&self,
607-
s: &mut pprust::State<IdentifiedAnnotation>,
607+
s: &mut pprust::State,
608608
node: pprust::AnnNode) -> io::IoResult<()> {
609609
match node {
610610
pprust::NodeItem(item) => {
@@ -634,15 +634,15 @@ struct TypedAnnotation {
634634
635635
impl pprust::PpAnn for TypedAnnotation {
636636
fn pre(&self,
637-
s: &mut pprust::State<TypedAnnotation>,
637+
s: &mut pprust::State,
638638
node: pprust::AnnNode) -> io::IoResult<()> {
639639
match node {
640640
pprust::NodeExpr(_) => s.popen(),
641641
_ => Ok(())
642642
}
643643
}
644644
fn post(&self,
645-
s: &mut pprust::State<TypedAnnotation>,
645+
s: &mut pprust::State,
646646
node: pprust::AnnNode) -> io::IoResult<()> {
647647
let tcx = &self.analysis.ty_cx;
648648
match node {

Diff for: src/librustc/middle/dataflow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ struct LoopScope<'a> {
8585

8686
impl<'a, O:DataFlowOperator> pprust::PpAnn for DataFlowContext<'a, O> {
8787
fn pre(&self,
88-
ps: &mut pprust::State<DataFlowContext<'a, O>>,
88+
ps: &mut pprust::State,
8989
node: pprust::AnnNode) -> io::IoResult<()> {
9090
let id = match node {
9191
pprust::NodeExpr(expr) => expr.id,

Diff for: src/libsyntax/fold.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,8 @@ mod test {
880880
use super::*;
881881

882882
// this version doesn't care about getting comments or docstrings in.
883-
fn fake_print_crate<A: pprust::PpAnn>(s: &mut pprust::State<A>,
884-
krate: &ast::Crate) -> io::IoResult<()> {
883+
fn fake_print_crate(s: &mut pprust::State,
884+
krate: &ast::Crate) -> io::IoResult<()> {
885885
s.print_mod(&krate.module, krate.attrs.as_slice())
886886
}
887887

Diff for: src/libsyntax/print/pprust.rs

+19-18
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ pub enum AnnNode<'a> {
4343
}
4444

4545
pub trait PpAnn {
46-
fn pre(&self, _state: &mut State<Self>, _node: AnnNode) -> IoResult<()> { Ok(()) }
47-
fn post(&self, _state: &mut State<Self>, _node: AnnNode) -> IoResult<()> { Ok(()) }
46+
fn pre(&self, _state: &mut State, _node: AnnNode) -> IoResult<()> { Ok(()) }
47+
fn post(&self, _state: &mut State, _node: AnnNode) -> IoResult<()> { Ok(()) }
4848
}
4949

5050
pub struct NoAnn;
@@ -56,23 +56,24 @@ pub struct CurrentCommentAndLiteral {
5656
cur_lit: uint,
5757
}
5858

59-
pub struct State<'a, A> {
59+
pub struct State<'a> {
6060
s: pp::Printer,
6161
cm: Option<&'a CodeMap>,
6262
intr: @token::IdentInterner,
6363
comments: Option<Vec<comments::Comment> >,
6464
literals: Option<Vec<comments::Literal> >,
6565
cur_cmnt_and_lit: CurrentCommentAndLiteral,
6666
boxes: RefCell<Vec<pp::Breaks> >,
67-
ann: &'a A
67+
ann: &'a PpAnn
6868
}
6969

70-
pub fn rust_printer(writer: ~io::Writer) -> State<'static, NoAnn> {
70+
pub fn rust_printer(writer: ~io::Writer) -> State<'static> {
7171
static NO_ANN: NoAnn = NoAnn;
7272
rust_printer_annotated(writer, &NO_ANN)
7373
}
7474

75-
pub fn rust_printer_annotated<'a, A: PpAnn>(writer: ~io::Writer, ann: &'a A) -> State<'a, A> {
75+
pub fn rust_printer_annotated<'a>(writer: ~io::Writer,
76+
ann: &'a PpAnn) -> State<'a> {
7677
State {
7778
s: pp::mk_printer(writer, default_columns),
7879
cm: None,
@@ -95,14 +96,14 @@ pub static default_columns: uint = 78u;
9596
// Requires you to pass an input filename and reader so that
9697
// it can scan the input text for comments and literals to
9798
// copy forward.
98-
pub fn print_crate<'a, A: PpAnn>(cm: &'a CodeMap,
99-
span_diagnostic: &diagnostic::SpanHandler,
100-
krate: &ast::Crate,
101-
filename: ~str,
102-
input: &mut io::Reader,
103-
out: ~io::Writer,
104-
ann: &'a A,
105-
is_expanded: bool) -> IoResult<()> {
99+
pub fn print_crate<'a>(cm: &'a CodeMap,
100+
span_diagnostic: &diagnostic::SpanHandler,
101+
krate: &ast::Crate,
102+
filename: ~str,
103+
input: &mut io::Reader,
104+
out: ~io::Writer,
105+
ann: &'a PpAnn,
106+
is_expanded: bool) -> IoResult<()> {
106107
let (cmnts, lits) = comments::gather_comments_and_literals(
107108
span_diagnostic,
108109
filename,
@@ -133,7 +134,7 @@ pub fn print_crate<'a, A: PpAnn>(cm: &'a CodeMap,
133134
eof(&mut s.s)
134135
}
135136

136-
pub fn to_str(f: |&mut State<NoAnn>| -> IoResult<()>) -> ~str {
137+
pub fn to_str(f: |&mut State| -> IoResult<()>) -> ~str {
137138
let mut s = rust_printer(~MemWriter::new());
138139
f(&mut s).unwrap();
139140
eof(&mut s.s).unwrap();
@@ -237,7 +238,7 @@ pub fn visibility_qualified(vis: ast::Visibility, s: &str) -> ~str {
237238
}
238239
}
239240

240-
impl<'a, A: PpAnn> State<'a, A> {
241+
impl<'a> State<'a> {
241242
pub fn ibox(&mut self, u: uint) -> IoResult<()> {
242243
self.boxes.borrow_mut().get().push(pp::Inconsistent);
243244
pp::ibox(&mut self.s, u)
@@ -365,7 +366,7 @@ impl<'a, A: PpAnn> State<'a, A> {
365366
}
366367

367368
pub fn commasep<T>(&mut self, b: Breaks, elts: &[T],
368-
op: |&mut State<A>, &T| -> IoResult<()>)
369+
op: |&mut State, &T| -> IoResult<()>)
369370
-> IoResult<()> {
370371
try!(self.rbox(0u, b));
371372
let mut first = true;
@@ -381,7 +382,7 @@ impl<'a, A: PpAnn> State<'a, A> {
381382
&mut self,
382383
b: Breaks,
383384
elts: &[T],
384-
op: |&mut State<A>, &T| -> IoResult<()>,
385+
op: |&mut State, &T| -> IoResult<()>,
385386
get_span: |&T| -> codemap::Span) -> IoResult<()> {
386387
try!(self.rbox(0u, b));
387388
let len = elts.len();

0 commit comments

Comments
 (0)