Skip to content

Commit 8d432fb

Browse files
committed
Auto merge of #26846 - P1start:print-maybe-styled-macro, r=pnkfelix
`EmitterWriter::print_maybe_styled` was basically always used with `format!`, so this macro makes some code cleaner. It should also remove some unnecessary allocations (most `print_maybe_styled` invocations allocated a `String` previously, whereas the new macro uses `write_fmt` to write the formatted string directly to the terminal). This probably could have been part of #26838, but it’s too late now. It’s also rebased on #26838’s branch because otherwise pretty much all of the changes in this PR would conflict with the other PR’s changes.
2 parents ba32469 + 3ebf706 commit 8d432fb

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

src/libsyntax/diagnostic.rs

+38-20
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,20 @@ enum Destination {
322322
Raw(Box<Write + Send>),
323323
}
324324

325+
/// Do not use this for messages that end in `\n` – use `println_maybe_styled` instead. See
326+
/// `EmitterWriter::print_maybe_styled` for details.
327+
macro_rules! print_maybe_styled {
328+
($writer: expr, $style: expr, $($arg: tt)*) => {
329+
$writer.print_maybe_styled(format_args!($($arg)*), $style, false)
330+
}
331+
}
332+
333+
macro_rules! println_maybe_styled {
334+
($writer: expr, $style: expr, $($arg: tt)*) => {
335+
$writer.print_maybe_styled(format_args!($($arg)*), $style, true)
336+
}
337+
}
338+
325339
impl EmitterWriter {
326340
pub fn stderr(color_config: ColorConfig,
327341
registry: Option<diagnostics::registry::Registry>) -> EmitterWriter {
@@ -350,8 +364,9 @@ impl EmitterWriter {
350364
}
351365

352366
fn print_maybe_styled(&mut self,
353-
msg: &str,
354-
color: term::attr::Attr) -> io::Result<()> {
367+
args: fmt::Arguments,
368+
color: term::attr::Attr,
369+
print_newline_at_end: bool) -> io::Result<()> {
355370
match self.dst {
356371
Terminal(ref mut t) => {
357372
try!(t.attr(color));
@@ -368,17 +383,22 @@ impl EmitterWriter {
368383
// once, which still leaves the opportunity for interleaved output
369384
// to be miscolored. We assume this is rare enough that we don't
370385
// have to worry about it.
371-
if msg.ends_with("\n") {
372-
try!(t.write_all(msg[..msg.len()-1].as_bytes()));
373-
try!(t.reset());
374-
try!(t.write_all(b"\n"));
386+
try!(t.write_fmt(args));
387+
try!(t.reset());
388+
if print_newline_at_end {
389+
t.write_all(b"\n")
390+
} else {
391+
Ok(())
392+
}
393+
}
394+
Raw(ref mut w) => {
395+
try!(w.write_fmt(args));
396+
if print_newline_at_end {
397+
w.write_all(b"\n")
375398
} else {
376-
try!(t.write_all(msg.as_bytes()));
377-
try!(t.reset());
399+
Ok(())
378400
}
379-
Ok(())
380401
}
381-
Raw(ref mut w) => w.write_all(msg.as_bytes()),
382402
}
383403
}
384404

@@ -388,15 +408,14 @@ impl EmitterWriter {
388408
try!(write!(&mut self.dst, "{} ", topic));
389409
}
390410

391-
try!(self.print_maybe_styled(&format!("{}: ", lvl.to_string()),
392-
term::attr::ForegroundColor(lvl.color())));
393-
try!(self.print_maybe_styled(&format!("{}", msg),
394-
term::attr::Bold));
411+
try!(print_maybe_styled!(self, term::attr::ForegroundColor(lvl.color()),
412+
"{}: ", lvl.to_string()));
413+
try!(print_maybe_styled!(self, term::attr::Bold, "{}", msg));
395414

396415
match code {
397416
Some(code) => {
398417
let style = term::attr::ForegroundColor(term::color::BRIGHT_MAGENTA);
399-
try!(self.print_maybe_styled(&format!(" [{}]", code.clone()), style));
418+
try!(print_maybe_styled!(self, style, " [{}]", code.clone()));
400419
}
401420
None => ()
402421
}
@@ -627,8 +646,8 @@ impl EmitterWriter {
627646
s.pop();
628647
}
629648

630-
try!(self.print_maybe_styled(&format!("{}\n", s),
631-
term::attr::ForegroundColor(lvl.color())));
649+
try!(println_maybe_styled!(self, term::attr::ForegroundColor(lvl.color()),
650+
"{}", s));
632651
}
633652
}
634653
Ok(())
@@ -700,9 +719,8 @@ impl EmitterWriter {
700719
}
701720
}
702721
s.push('^');
703-
s.push('\n');
704-
self.print_maybe_styled(&s[..],
705-
term::attr::ForegroundColor(lvl.color()))
722+
println_maybe_styled!(self, term::attr::ForegroundColor(lvl.color()),
723+
"{}", s)
706724
}
707725

708726
fn print_macro_backtrace(&mut self,

0 commit comments

Comments
 (0)