Skip to content

Commit 90566d6

Browse files
committed
syntax: Remove use of TraitObject in pretty printer
1 parent 1ad2128 commit 90566d6

File tree

5 files changed

+109
-84
lines changed

5 files changed

+109
-84
lines changed

src/librustc/middle/dataflow.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,12 @@ impl<'a, 'tcx, O:DataFlowOperator> DataFlowContext<'a, 'tcx, O> {
106106
}
107107
}
108108

109-
impl<'a, 'tcx, O:DataFlowOperator> pprust::PpAnn for DataFlowContext<'a, 'tcx, O> {
109+
impl<'a, 'tcx, O, W> pprust::PpAnn<W> for DataFlowContext<'a, 'tcx, O> where
110+
O: DataFlowOperator,
111+
W: io::Write,
112+
{
110113
fn pre(&self,
111-
ps: &mut pprust::State,
114+
ps: &mut pprust::State<W>,
112115
node: pprust::AnnNode) -> io::Result<()> {
113116
let id = match node {
114117
pprust::NodeIdent(_) | pprust::NodeName(_) => 0,

src/librustc_driver/pretty.rs

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,15 @@ pub fn parse_pretty(sess: &Session,
122122

123123
impl PpSourceMode {
124124
/// Constructs a `PrinterSupport` object and passes it to `f`.
125-
fn call_with_pp_support<'tcx, A, B, F>(&self,
125+
fn call_with_pp_support<'tcx, A, B, F, W>(&self,
126126
sess: Session,
127127
ast_map: Option<ast_map::Map<'tcx>>,
128128
arenas: &'tcx ty::CtxtArenas<'tcx>,
129129
id: String,
130130
payload: B,
131131
f: F) -> A where
132-
F: FnOnce(&PrinterSupport, B) -> A,
132+
F: FnOnce(&PrinterSupport<W>, B) -> A,
133+
W: io::Write,
133134
{
134135
match *self {
135136
PpmNormal | PpmEveryBodyLoops | PpmExpanded => {
@@ -159,7 +160,7 @@ impl PpSourceMode {
159160
}
160161
}
161162

162-
trait PrinterSupport<'ast>: pprust::PpAnn {
163+
trait PrinterSupport<'ast, W>: pprust::PpAnn<W> where W: io::Write {
163164
/// Provides a uniform interface for re-extracting a reference to a
164165
/// `Session` from a value that now owns it.
165166
fn sess<'a>(&'a self) -> &'a Session;
@@ -172,52 +173,58 @@ trait PrinterSupport<'ast>: pprust::PpAnn {
172173
///
173174
/// (Rust does not yet support upcasting from a trait object to
174175
/// an object for one of its super-traits.)
175-
fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn;
176+
fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn<W>;
176177
}
177178

178179
struct NoAnn<'ast> {
179180
sess: Session,
180181
ast_map: Option<ast_map::Map<'ast>>
181182
}
182183

183-
impl<'ast> PrinterSupport<'ast> for NoAnn<'ast> {
184+
impl<'ast, W> PrinterSupport<'ast, W> for NoAnn<'ast> where
185+
W: io::Write,
186+
{
184187
fn sess<'a>(&'a self) -> &'a Session { &self.sess }
185188

186189
fn ast_map<'a>(&'a self) -> Option<&'a ast_map::Map<'ast>> {
187190
self.ast_map.as_ref()
188191
}
189192

190-
fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn { self }
193+
fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn<W> { self }
191194
}
192195

193-
impl<'ast> pprust::PpAnn for NoAnn<'ast> {}
196+
impl<'ast, W: io::Write> pprust::PpAnn<W> for NoAnn<'ast> {}
194197

195198
struct IdentifiedAnnotation<'ast> {
196199
sess: Session,
197200
ast_map: Option<ast_map::Map<'ast>>,
198201
}
199202

200-
impl<'ast> PrinterSupport<'ast> for IdentifiedAnnotation<'ast> {
203+
impl<'ast, W> PrinterSupport<'ast, W> for IdentifiedAnnotation<'ast> where
204+
W: io::Write,
205+
{
201206
fn sess<'a>(&'a self) -> &'a Session { &self.sess }
202207

203208
fn ast_map<'a>(&'a self) -> Option<&'a ast_map::Map<'ast>> {
204209
self.ast_map.as_ref()
205210
}
206211

207-
fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn { self }
212+
fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn<W> { self }
208213
}
209214

210-
impl<'ast> pprust::PpAnn for IdentifiedAnnotation<'ast> {
215+
impl<'ast, W> pprust::PpAnn<W> for IdentifiedAnnotation<'ast> where
216+
W: io::Write,
217+
{
211218
fn pre(&self,
212-
s: &mut pprust::State,
219+
s: &mut pprust::State<W>,
213220
node: pprust::AnnNode) -> io::Result<()> {
214221
match node {
215222
pprust::NodeExpr(_) => s.popen(),
216223
_ => Ok(())
217224
}
218225
}
219226
fn post(&self,
220-
s: &mut pprust::State,
227+
s: &mut pprust::State<W>,
221228
node: pprust::AnnNode) -> io::Result<()> {
222229
match node {
223230
pprust::NodeIdent(_) | pprust::NodeName(_) => Ok(()),
@@ -252,19 +259,23 @@ struct HygieneAnnotation<'ast> {
252259
ast_map: Option<ast_map::Map<'ast>>,
253260
}
254261

255-
impl<'ast> PrinterSupport<'ast> for HygieneAnnotation<'ast> {
262+
impl<'ast, W> PrinterSupport<'ast, W> for HygieneAnnotation<'ast> where
263+
W: io::Write,
264+
{
256265
fn sess<'a>(&'a self) -> &'a Session { &self.sess }
257266

258267
fn ast_map<'a>(&'a self) -> Option<&'a ast_map::Map<'ast>> {
259268
self.ast_map.as_ref()
260269
}
261270

262-
fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn { self }
271+
fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn<W> { self }
263272
}
264273

265-
impl<'ast> pprust::PpAnn for HygieneAnnotation<'ast> {
274+
impl<'ast, W> pprust::PpAnn<W> for HygieneAnnotation<'ast> where
275+
W: io::Write,
276+
{
266277
fn post(&self,
267-
s: &mut pprust::State,
278+
s: &mut pprust::State<W>,
268279
node: pprust::AnnNode) -> io::Result<()> {
269280
match node {
270281
pprust::NodeIdent(&ast::Ident { name: ast::Name(nm), ctxt }) => {
@@ -287,27 +298,31 @@ struct TypedAnnotation<'tcx> {
287298
analysis: ty::CrateAnalysis<'tcx>,
288299
}
289300

290-
impl<'tcx> PrinterSupport<'tcx> for TypedAnnotation<'tcx> {
301+
impl<'tcx, W> PrinterSupport<'tcx, W> for TypedAnnotation<'tcx> where
302+
W: io::Write,
303+
{
291304
fn sess<'a>(&'a self) -> &'a Session { &self.analysis.ty_cx.sess }
292305

293306
fn ast_map<'a>(&'a self) -> Option<&'a ast_map::Map<'tcx>> {
294307
Some(&self.analysis.ty_cx.map)
295308
}
296309

297-
fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn { self }
310+
fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn<W> { self }
298311
}
299312

300-
impl<'tcx> pprust::PpAnn for TypedAnnotation<'tcx> {
313+
impl<'tcx, W> pprust::PpAnn<W> for TypedAnnotation<'tcx> where
314+
W: io::Write,
315+
{
301316
fn pre(&self,
302-
s: &mut pprust::State,
317+
s: &mut pprust::State<W>,
303318
node: pprust::AnnNode) -> io::Result<()> {
304319
match node {
305320
pprust::NodeExpr(_) => s.popen(),
306321
_ => Ok(())
307322
}
308323
}
309324
fn post(&self,
310-
s: &mut pprust::State,
325+
s: &mut pprust::State<W>,
311326
node: pprust::AnnNode) -> io::Result<()> {
312327
let tcx = &self.analysis.ty_cx;
313328
match node {

src/libsyntax/ast_map/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,9 @@ pub trait NodePrinter {
885885
fn print_node(&mut self, node: &Node) -> io::Result<()>;
886886
}
887887

888-
impl<'a> NodePrinter for pprust::State<'a> {
888+
impl<'a, W> NodePrinter for pprust::State<'a, W> where
889+
W: io::Write,
890+
{
889891
fn print_node(&mut self, node: &Node) -> io::Result<()> {
890892
match *node {
891893
NodeItem(a) => self.print_item(&*a),

src/libsyntax/print/pp.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub struct PrintStackElem {
161161

162162
const SIZE_INFINITY: isize = 0xffff;
163163

164-
pub fn mk_printer<'a>(out: Box<io::Write+'a>, linewidth: usize) -> Printer<'a> {
164+
pub fn mk_printer<W: io::Write>(out: W, linewidth: usize) -> Printer<W> {
165165
// Yes 3, it makes the ring buffers big enough to never
166166
// fall behind.
167167
let n: usize = 3 * linewidth;
@@ -265,8 +265,8 @@ pub fn mk_printer<'a>(out: Box<io::Write+'a>, linewidth: usize) -> Printer<'a> {
265265
/// In this implementation (following the paper, again) the SCAN process is
266266
/// the method called 'pretty_print', and the 'PRINT' process is the method
267267
/// called 'print'.
268-
pub struct Printer<'a> {
269-
pub out: Box<io::Write+'a>,
268+
pub struct Printer<W> {
269+
pub out: W,
270270
buf_len: usize,
271271
/// Width of lines we're constrained to
272272
margin: isize,
@@ -303,7 +303,7 @@ pub struct Printer<'a> {
303303
pending_indentation: isize,
304304
}
305305

306-
impl<'a> Printer<'a> {
306+
impl<W: io::Write> Printer<W> {
307307
pub fn last_token(&mut self) -> Token {
308308
self.token[self.right].clone()
309309
}
@@ -615,66 +615,70 @@ impl<'a> Printer<'a> {
615615
}
616616
}
617617
}
618+
619+
pub fn into_inner(self) -> W {
620+
self.out
621+
}
618622
}
619623

620624
// Convenience functions to talk to the printer.
621625
//
622626
// "raw box"
623-
pub fn rbox(p: &mut Printer, indent: usize, b: Breaks) -> io::Result<()> {
627+
pub fn rbox<W: io::Write>(p: &mut Printer<W>, indent: usize, b: Breaks) -> io::Result<()> {
624628
p.pretty_print(Token::Begin(BeginToken {
625629
offset: indent as isize,
626630
breaks: b
627631
}))
628632
}
629633

630-
pub fn ibox(p: &mut Printer, indent: usize) -> io::Result<()> {
634+
pub fn ibox<W: io::Write>(p: &mut Printer<W>, indent: usize) -> io::Result<()> {
631635
rbox(p, indent, Breaks::Inconsistent)
632636
}
633637

634-
pub fn cbox(p: &mut Printer, indent: usize) -> io::Result<()> {
638+
pub fn cbox<W: io::Write>(p: &mut Printer<W>, indent: usize) -> io::Result<()> {
635639
rbox(p, indent, Breaks::Consistent)
636640
}
637641

638-
pub fn break_offset(p: &mut Printer, n: usize, off: isize) -> io::Result<()> {
642+
pub fn break_offset<W: io::Write>(p: &mut Printer<W>, n: usize, off: isize) -> io::Result<()> {
639643
p.pretty_print(Token::Break(BreakToken {
640644
offset: off,
641645
blank_space: n as isize
642646
}))
643647
}
644648

645-
pub fn end(p: &mut Printer) -> io::Result<()> {
649+
pub fn end<W: io::Write>(p: &mut Printer<W>) -> io::Result<()> {
646650
p.pretty_print(Token::End)
647651
}
648652

649-
pub fn eof(p: &mut Printer) -> io::Result<()> {
653+
pub fn eof<W: io::Write>(p: &mut Printer<W>) -> io::Result<()> {
650654
p.pretty_print(Token::Eof)
651655
}
652656

653-
pub fn word(p: &mut Printer, wrd: &str) -> io::Result<()> {
657+
pub fn word<W: io::Write>(p: &mut Printer<W>, wrd: &str) -> io::Result<()> {
654658
p.pretty_print(Token::String(/* bad */ wrd.to_string(), wrd.len() as isize))
655659
}
656660

657-
pub fn huge_word(p: &mut Printer, wrd: &str) -> io::Result<()> {
661+
pub fn huge_word<W: io::Write>(p: &mut Printer<W>, wrd: &str) -> io::Result<()> {
658662
p.pretty_print(Token::String(/* bad */ wrd.to_string(), SIZE_INFINITY))
659663
}
660664

661-
pub fn zero_word(p: &mut Printer, wrd: &str) -> io::Result<()> {
665+
pub fn zero_word<W: io::Write>(p: &mut Printer<W>, wrd: &str) -> io::Result<()> {
662666
p.pretty_print(Token::String(/* bad */ wrd.to_string(), 0))
663667
}
664668

665-
pub fn spaces(p: &mut Printer, n: usize) -> io::Result<()> {
669+
pub fn spaces<W: io::Write>(p: &mut Printer<W>, n: usize) -> io::Result<()> {
666670
break_offset(p, n, 0)
667671
}
668672

669-
pub fn zerobreak(p: &mut Printer) -> io::Result<()> {
673+
pub fn zerobreak<W: io::Write>(p: &mut Printer<W>) -> io::Result<()> {
670674
spaces(p, 0)
671675
}
672676

673-
pub fn space(p: &mut Printer) -> io::Result<()> {
677+
pub fn space<W: io::Write>(p: &mut Printer<W>) -> io::Result<()> {
674678
spaces(p, 1)
675679
}
676680

677-
pub fn hardbreak(p: &mut Printer) -> io::Result<()> {
681+
pub fn hardbreak<W: io::Write>(p: &mut Printer<W>) -> io::Result<()> {
678682
spaces(p, SIZE_INFINITY as usize)
679683
}
680684

0 commit comments

Comments
 (0)