Skip to content

Commit ada9150

Browse files
committed
libsyntax: Correctly de-@mut the pretty printer writer
1 parent 39f0270 commit ada9150

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

src/libsyntax/ext/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ mod test {
13191319
}
13201320

13211321
fn fake_print_crate(crate: &ast::Crate) {
1322-
let out = @mut std::io::stderr() as @mut std::io::Writer;
1322+
let mut out = ~std::io::stderr() as ~std::io::Writer;
13231323
let s = pprust::rust_printer(out, get_ident_interner());
13241324
pprust::print_crate_(s, crate);
13251325
}

src/libsyntax/print/pprust.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub fn print_crate(cm: @CodeMap,
122122
span_diagnostic: @diagnostic::SpanHandler,
123123
crate: &ast::Crate,
124124
filename: @str,
125-
input: @mut io::Reader,
125+
input: &mut io::Reader,
126126
out: ~io::Writer,
127127
ann: @pp_ann,
128128
is_expanded: bool) {
@@ -211,13 +211,8 @@ pub fn fun_to_str(decl: &ast::fn_decl, purity: ast::purity, name: ast::Ident,
211211
end(&mut s); // Close the head box
212212
end(&mut s); // Close the outer box
213213
eof(&mut s.s);
214-
215-
// XXX(pcwalton): Need checked downcasts.
216214
unsafe {
217-
let (_, wr): (uint, ~MemWriter) = cast::transmute(s.s.out);
218-
let result = str::from_utf8_owned(wr.inner_ref().to_owned());
219-
cast::forget(wr);
220-
result
215+
get_mem_writer(&mut s.s.out)
221216
}
222217
}
223218

@@ -230,13 +225,8 @@ pub fn block_to_str(blk: &ast::Block, intr: @ident_interner) -> ~str {
230225
ibox(&mut s, 0u);
231226
print_block(&mut s, blk);
232227
eof(&mut s.s);
233-
234-
// XXX(pcwalton): Need checked downcasts.
235228
unsafe {
236-
let (_, wr): (uint, ~MemWriter) = cast::transmute(s.s.out);
237-
let result = str::from_utf8_owned(wr.inner_ref().to_owned());
238-
cast::forget(wr);
239-
result
229+
get_mem_writer(&mut s.s.out)
240230
}
241231
}
242232

@@ -2318,17 +2308,23 @@ pub fn print_string(s: &mut ps, st: &str, style: ast::StrStyle) {
23182308
word(&mut s.s, st);
23192309
}
23202310

2311+
// XXX(pcwalton): A nasty function to extract the string from an `io::Writer`
2312+
// that we "know" to be a `MemWriter` that works around the lack of checked
2313+
// downcasts.
2314+
unsafe fn get_mem_writer(writer: &mut ~io::Writer) -> ~str {
2315+
let (_, wr): (uint, ~MemWriter) = cast::transmute_copy(writer);
2316+
let result = str::from_utf8_owned(wr.inner_ref().to_owned());
2317+
cast::forget(wr);
2318+
result
2319+
}
2320+
23212321
pub fn to_str<T>(t: &T, f: |&mut ps, &T|, intr: @ident_interner) -> ~str {
23222322
let wr = ~MemWriter::new();
23232323
let mut s = rust_printer(wr as ~io::Writer, intr);
23242324
f(&mut s, t);
23252325
eof(&mut s.s);
2326-
// XXX(pcwalton): Need checked downcasts.
23272326
unsafe {
2328-
let (_, wr): (uint, ~MemWriter) = cast::transmute(s.s.out);
2329-
let result = str::from_utf8_owned(wr.inner_ref().to_owned());
2330-
cast::forget(wr);
2331-
result
2327+
get_mem_writer(&mut s.s.out)
23322328
}
23332329
}
23342330

0 commit comments

Comments
 (0)