Skip to content

Remove io::io_error #11946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Feb 3, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
syntax: Remove io_error usage
  • Loading branch information
alexcrichton committed Feb 3, 2014
commit b211b00d21fc7c03c3c378ad5eab60666a00fc08
112 changes: 64 additions & 48 deletions src/libsyntax/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,58 +189,61 @@ impl Level {
}
}

fn print_maybe_styled(msg: &str, color: term::attr::Attr) {
local_data_key!(tls_terminal: ~Option<term::Terminal<StdWriter>>)
fn print_maybe_styled(msg: &str, color: term::attr::Attr) -> io::IoResult<()> {
local_data_key!(tls_terminal: Option<term::Terminal<StdWriter>>)


fn is_stderr_screen() -> bool {
use std::libc;
unsafe { libc::isatty(libc::STDERR_FILENO) != 0 }
}
fn write_pretty<T: Writer>(term: &mut term::Terminal<T>, s: &str, c: term::attr::Attr) {
term.attr(c);
term.write(s.as_bytes());
term.reset();
fn write_pretty<T: Writer>(term: &mut term::Terminal<T>, s: &str,
c: term::attr::Attr) -> io::IoResult<()> {
if_ok!(term.attr(c));
if_ok!(term.write(s.as_bytes()));
if_ok!(term.reset());
Ok(())
}

if is_stderr_screen() {
local_data::get_mut(tls_terminal, |term| {
match term {
Some(term) => {
match **term {
match *term {
Some(ref mut term) => write_pretty(term, msg, color),
None => io::stderr().write(msg.as_bytes())
}
}
None => {
let t = ~match term::Terminal::new(io::stderr()) {
let (t, ret) = match term::Terminal::new(io::stderr()) {
Ok(mut term) => {
write_pretty(&mut term, msg, color);
Some(term)
let r = write_pretty(&mut term, msg, color);
(Some(term), r)
}
Err(_) => {
io::stderr().write(msg.as_bytes());
None
(None, io::stderr().write(msg.as_bytes()))
}
};
local_data::set(tls_terminal, t);
ret
}
}
});
})
} else {
io::stderr().write(msg.as_bytes());
io::stderr().write(msg.as_bytes())
}
}

fn print_diagnostic(topic: &str, lvl: Level, msg: &str) {
let mut stderr = io::stderr();

fn print_diagnostic(topic: &str, lvl: Level, msg: &str) -> io::IoResult<()> {
if !topic.is_empty() {
write!(&mut stderr as &mut io::Writer, "{} ", topic);
let mut stderr = io::stderr();
if_ok!(write!(&mut stderr as &mut io::Writer, "{} ", topic));
}

print_maybe_styled(format!("{}: ", lvl.to_str()),
term::attr::ForegroundColor(lvl.color()));
print_maybe_styled(format!("{}\n", msg), term::attr::Bold);
if_ok!(print_maybe_styled(format!("{}: ", lvl.to_str()),
term::attr::ForegroundColor(lvl.color())));
if_ok!(print_maybe_styled(format!("{}\n", msg), term::attr::Bold));
Ok(())
}

pub struct DefaultEmitter;
Expand All @@ -250,20 +253,28 @@ impl Emitter for DefaultEmitter {
cmsp: Option<(&codemap::CodeMap, Span)>,
msg: &str,
lvl: Level) {
match cmsp {
let error = match cmsp {
Some((cm, sp)) => emit(cm, sp, msg, lvl, false),
None => print_diagnostic("", lvl, msg),
};

match error {
Ok(()) => {}
Err(e) => fail!("failed to print diagnostics: {}", e),
}
}

fn custom_emit(&self, cm: &codemap::CodeMap,
sp: Span, msg: &str, lvl: Level) {
emit(cm, sp, msg, lvl, true);
match emit(cm, sp, msg, lvl, true) {
Ok(()) => {}
Err(e) => fail!("failed to print diagnostics: {}", e),
}
}
}

fn emit(cm: &codemap::CodeMap, sp: Span,
msg: &str, lvl: Level, custom: bool) {
msg: &str, lvl: Level, custom: bool) -> io::IoResult<()> {
let ss = cm.span_to_str(sp);
let lines = cm.span_to_lines(sp);
if custom {
Expand All @@ -272,19 +283,19 @@ fn emit(cm: &codemap::CodeMap, sp: Span,
// the span)
let span_end = Span { lo: sp.hi, hi: sp.hi, expn_info: sp.expn_info};
let ses = cm.span_to_str(span_end);
print_diagnostic(ses, lvl, msg);
custom_highlight_lines(cm, sp, lvl, lines);
if_ok!(print_diagnostic(ses, lvl, msg));
if_ok!(custom_highlight_lines(cm, sp, lvl, lines));
} else {
print_diagnostic(ss, lvl, msg);
highlight_lines(cm, sp, lvl, lines);
if_ok!(print_diagnostic(ss, lvl, msg));
if_ok!(highlight_lines(cm, sp, lvl, lines));
}
print_macro_backtrace(cm, sp);
print_macro_backtrace(cm, sp)
}

fn highlight_lines(cm: &codemap::CodeMap,
sp: Span,
lvl: Level,
lines: &codemap::FileLines) {
lines: &codemap::FileLines) -> io::IoResult<()> {
let fm = lines.file;
let mut err = io::stderr();
let err = &mut err as &mut io::Writer;
Expand All @@ -297,12 +308,13 @@ fn highlight_lines(cm: &codemap::CodeMap,
}
// Print the offending lines
for line in display_lines.iter() {
write!(err, "{}:{} {}\n", fm.name, *line + 1, fm.get_line(*line as int));
if_ok!(write!(err, "{}:{} {}\n", fm.name, *line + 1,
fm.get_line(*line as int)));
}
if elided {
let last_line = display_lines[display_lines.len() - 1u];
let s = format!("{}:{} ", fm.name, last_line + 1u);
write!(err, "{0:1$}...\n", "", s.len());
if_ok!(write!(err, "{0:1$}...\n", "", s.len()));
}

// FIXME (#3260)
Expand Down Expand Up @@ -334,16 +346,18 @@ fn highlight_lines(cm: &codemap::CodeMap,
_ => s.push_char(' '),
};
}
write!(err, "{}", s);
if_ok!(write!(err, "{}", s));
let mut s = ~"^";
let hi = cm.lookup_char_pos(sp.hi);
if hi.col != lo.col {
// the ^ already takes up one space
let num_squigglies = hi.col.to_uint()-lo.col.to_uint()-1u;
for _ in range(0, num_squigglies) { s.push_char('~'); }
}
print_maybe_styled(s + "\n", term::attr::ForegroundColor(lvl.color()));
if_ok!(print_maybe_styled(s + "\n",
term::attr::ForegroundColor(lvl.color())));
}
Ok(())
}

// Here are the differences between this and the normal `highlight_lines`:
Expand All @@ -355,23 +369,23 @@ fn highlight_lines(cm: &codemap::CodeMap,
fn custom_highlight_lines(cm: &codemap::CodeMap,
sp: Span,
lvl: Level,
lines: &codemap::FileLines) {
lines: &codemap::FileLines) -> io::IoResult<()> {
let fm = lines.file;
let mut err = io::stderr();
let err = &mut err as &mut io::Writer;

let lines = lines.lines.as_slice();
if lines.len() > MAX_LINES {
write!(err, "{}:{} {}\n", fm.name,
lines[0] + 1, fm.get_line(lines[0] as int));
write!(err, "...\n");
if_ok!(write!(err, "{}:{} {}\n", fm.name,
lines[0] + 1, fm.get_line(lines[0] as int)));
if_ok!(write!(err, "...\n"));
let last_line = lines[lines.len()-1];
write!(err, "{}:{} {}\n", fm.name,
last_line + 1, fm.get_line(last_line as int));
if_ok!(write!(err, "{}:{} {}\n", fm.name,
last_line + 1, fm.get_line(last_line as int)));
} else {
for line in lines.iter() {
write!(err, "{}:{} {}\n", fm.name,
*line + 1, fm.get_line(*line as int));
if_ok!(write!(err, "{}:{} {}\n", fm.name,
*line + 1, fm.get_line(*line as int)));
}
}
let last_line_start = format!("{}:{} ", fm.name, lines[lines.len()-1]+1);
Expand All @@ -381,22 +395,24 @@ fn custom_highlight_lines(cm: &codemap::CodeMap,
let mut s = ~"";
for _ in range(0, skip) { s.push_char(' '); }
s.push_char('^');
print_maybe_styled(s + "\n", term::attr::ForegroundColor(lvl.color()));
print_maybe_styled(s + "\n", term::attr::ForegroundColor(lvl.color()))
}

fn print_macro_backtrace(cm: &codemap::CodeMap, sp: Span) {
fn print_macro_backtrace(cm: &codemap::CodeMap, sp: Span) -> io::IoResult<()> {
for ei in sp.expn_info.iter() {
let ss = ei.callee.span.as_ref().map_or(~"", |span| cm.span_to_str(*span));
let (pre, post) = match ei.callee.format {
codemap::MacroAttribute => ("#[", "]"),
codemap::MacroBang => ("", "!")
};
print_diagnostic(ss, Note,
format!("in expansion of {}{}{}", pre, ei.callee.name, post));
if_ok!(print_diagnostic(ss, Note,
format!("in expansion of {}{}{}", pre,
ei.callee.name, post)));
let ss = cm.span_to_str(ei.call_site);
print_diagnostic(ss, Note, "expansion site");
print_macro_backtrace(cm, ei.call_site);
if_ok!(print_diagnostic(ss, Note, "expansion site"));
if_ok!(print_macro_backtrace(cm, ei.call_site));
}
Ok(())
}

pub fn expect<T:Clone>(diag: @SpanHandler, opt: Option<T>, msg: || -> ~str)
Expand Down
9 changes: 4 additions & 5 deletions src/libsyntax/ext/source_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use parse::token::get_ident_interner;
use parse::token;
use print::pprust;

use std::io;
use std::io::File;
use std::rc::Rc;
use std::str;
Expand Down Expand Up @@ -109,9 +108,9 @@ pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
None => return MacResult::dummy_expr()
};
let file = res_rel_file(cx, sp, &Path::new(file));
let bytes = match io::result(|| File::open(&file).read_to_end()) {
let bytes = match File::open(&file).read_to_end() {
Err(e) => {
cx.span_err(sp, format!("couldn't read {}: {}", file.display(), e.desc));
cx.span_err(sp, format!("couldn't read {}: {}", file.display(), e));
return MacResult::dummy_expr();
}
Ok(bytes) => bytes,
Expand Down Expand Up @@ -141,9 +140,9 @@ pub fn expand_include_bin(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
None => return MacResult::dummy_expr()
};
let file = res_rel_file(cx, sp, &Path::new(file));
match io::result(|| File::open(&file).read_to_end()) {
match File::open(&file).read_to_end() {
Err(e) => {
cx.span_err(sp, format!("couldn't read {}: {}", file.display(), e.desc));
cx.span_err(sp, format!("couldn't read {}: {}", file.display(), e));
return MacResult::dummy_expr();
}
Ok(bytes) => {
Expand Down
5 changes: 5 additions & 0 deletions src/libsyntax/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ This API is completely unstable and subject to change.
extern mod extra;
extern mod term;

#[cfg(stage0)]
macro_rules! if_ok (
($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
)

pub mod util {
pub mod interner;
#[cfg(test)]
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/parse/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,8 @@ pub fn gather_comments_and_literals(span_diagnostic:
path: ~str,
srdr: &mut io::Reader)
-> (~[Comment], ~[Literal]) {
let src = str::from_utf8_owned(srdr.read_to_end()).unwrap();
let src = srdr.read_to_end().unwrap();
let src = str::from_utf8_owned(src).unwrap();
let cm = CodeMap::new();
let filemap = cm.new_filemap(path, src);
let rdr = lexer::new_low_level_string_reader(span_diagnostic, filemap);
Expand Down
5 changes: 2 additions & 3 deletions src/libsyntax/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use parse::attr::ParserAttr;
use parse::parser::Parser;

use std::cell::RefCell;
use std::io;
use std::io::File;
use std::str;

Expand Down Expand Up @@ -232,10 +231,10 @@ pub fn file_to_filemap(sess: @ParseSess, path: &Path, spanopt: Option<Span>)
None => sess.span_diagnostic.handler().fatal(msg),
}
};
let bytes = match io::result(|| File::open(path).read_to_end()) {
let bytes = match File::open(path).read_to_end() {
Ok(bytes) => bytes,
Err(e) => {
err(format!("couldn't read {}: {}", path.display(), e.desc));
err(format!("couldn't read {}: {}", path.display(), e));
unreachable!()
}
};
Expand Down
Loading