Skip to content

Commit

Permalink
auto merge of #11946 : alexcrichton/rust/no-io-error, r=brson
Browse files Browse the repository at this point in the history
Turns out this was a little more far-reaching than I thought it was.

The first commit is the crux of this stack of commits. The `io::io_error` condition is completely removed and the `read` and `write` methods are altered to return `IoResult<T>`. This turned out to be an incredibly far-reaching change!

Overall, I'm very happy with how this turned out (in addition with the `unused_must_use` lint). I had to almost rewrite the pretty printer in `libsyntax` as well as the the formatting in `librustdoc` (as one would expect). These two modules do *tons* of I/O, and I believe that it's definitely improved.

This pull request also introduces the `if_ok!()` macro for returning-early from something that returns a result. I made quite liberal use of this in mostly the pretty printer and html renderer, and I found its usage generally quite pleasant and convenient to have. I didn't really feel like adding any other macro while I was using it, and I figured that pretty printing could be nicer, but it's nowhere near horrid today.

This may be a controversial issue closing, but I'm going to say it.

Closes #6163
  • Loading branch information
bors committed Feb 3, 2014
2 parents be4fc63 + c765a8e commit cb40eba
Show file tree
Hide file tree
Showing 122 changed files with 4,359 additions and 4,246 deletions.
10 changes: 8 additions & 2 deletions src/compiletest/compiletest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,13 @@ pub fn run_tests(config: &config) {
// For context, see #8904
io::test::raise_fd_limit();
let res = test::run_tests_console(&opts, tests);
if !res { fail!("Some tests failed"); }
match res {
Ok(true) => {}
Ok(false) => fail!("Some tests failed"),
Err(e) => {
println!("I/O failure during tests: {}", e);
}
}
}

pub fn test_opts(config: &config) -> test::TestOpts {
Expand All @@ -255,7 +261,7 @@ pub fn make_tests(config: &config) -> ~[test::TestDescAndFn] {
debug!("making tests from {}",
config.src_base.display());
let mut tests = ~[];
let dirs = fs::readdir(&config.src_base);
let dirs = fs::readdir(&config.src_base).unwrap();
for file in dirs.iter() {
let file = file.clone();
debug!("inspecting file {}", file.display());
Expand Down
12 changes: 6 additions & 6 deletions src/compiletest/procsrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ pub fn run(lib_path: &str,
});

match opt_process {
Some(ref mut process) => {
Ok(ref mut process) => {
for input in input.iter() {
process.input().write(input.as_bytes());
process.input().write(input.as_bytes()).unwrap();
}
let run::ProcessOutput { status, output, error } = process.finish_with_output();

Expand All @@ -70,7 +70,7 @@ pub fn run(lib_path: &str,
err: str::from_utf8_owned(error).unwrap()
})
},
None => None
Err(..) => None
}
}

Expand All @@ -90,13 +90,13 @@ pub fn run_background(lib_path: &str,
});

match opt_process {
Some(mut process) => {
Ok(mut process) => {
for input in input.iter() {
process.input().write(input.as_bytes());
process.input().write(input.as_bytes()).unwrap();
}

Some(process)
},
None => None
Err(..) => None
}
}
20 changes: 11 additions & 9 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fn run_pretty_test(config: &config, props: &TestProps, testfile: &Path) {
let rounds =
match props.pp_exact { Some(_) => 1, None => 2 };

let src = File::open(testfile).read_to_end();
let src = File::open(testfile).read_to_end().unwrap();
let src = str::from_utf8_owned(src).unwrap();
let mut srcs = ~[src];

Expand All @@ -175,7 +175,7 @@ fn run_pretty_test(config: &config, props: &TestProps, testfile: &Path) {
let mut expected = match props.pp_exact {
Some(ref file) => {
let filepath = testfile.dir_path().join(file);
let s = File::open(&filepath).read_to_end();
let s = File::open(&filepath).read_to_end().unwrap();
str::from_utf8_owned(s).unwrap()
}
None => { srcs[srcs.len() - 2u].clone() }
Expand Down Expand Up @@ -318,8 +318,10 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
//waiting 1 second for gdbserver start
timer::sleep(1000);
let result = task::try(proc() {
tcp::TcpStream::connect(
SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 5039 });
tcp::TcpStream::connect(SocketAddr {
ip: Ipv4Addr(127, 0, 0, 1),
port: 5039,
}).unwrap();
});
if result.is_err() {
continue;
Expand Down Expand Up @@ -361,7 +363,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
stdout: out,
stderr: err,
cmdline: cmdline};
process.force_destroy();
process.force_destroy().unwrap();
}

_=> {
Expand Down Expand Up @@ -727,7 +729,7 @@ fn compose_and_run_compiler(

fn ensure_dir(path: &Path) {
if path.is_dir() { return; }
fs::mkdir(path, io::UserRWX);
fs::mkdir(path, io::UserRWX).unwrap();
}

fn compose_and_run(config: &config, testfile: &Path,
Expand Down Expand Up @@ -852,7 +854,7 @@ fn dump_output(config: &config, testfile: &Path, out: &str, err: &str) {
fn dump_output_file(config: &config, testfile: &Path,
out: &str, extension: &str) {
let outfile = make_out_name(config, testfile, extension);
File::create(&outfile).write(out.as_bytes());
File::create(&outfile).write(out.as_bytes()).unwrap();
}

fn make_out_name(config: &config, testfile: &Path, extension: &str) -> Path {
Expand Down Expand Up @@ -1003,7 +1005,7 @@ fn _arm_exec_compiled_test(config: &config, props: &TestProps,
fn _arm_push_aux_shared_library(config: &config, testfile: &Path) {
let tdir = aux_output_dir_name(config, testfile);

let dirs = fs::readdir(&tdir);
let dirs = fs::readdir(&tdir).unwrap();
for file in dirs.iter() {
if file.extension_str() == Some("so") {
// FIXME (#9639): This needs to handle non-utf8 paths
Expand Down Expand Up @@ -1099,7 +1101,7 @@ fn disassemble_extract(config: &config, _props: &TestProps,


fn count_extracted_lines(p: &Path) -> uint {
let x = File::open(&p.with_extension("ll")).read_to_end();
let x = File::open(&p.with_extension("ll")).read_to_end().unwrap();
let x = str::from_utf8_owned(x).unwrap();
x.lines().len()
}
Expand Down
35 changes: 14 additions & 21 deletions src/doc/guide-conditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ An example program that does this task reads like this:
# #[allow(unused_imports)];
use std::io::{BufferedReader, File};
# mod BufferedReader {
# use std::io::File;
# use std::io::{File, IoResult};
# use std::io::MemReader;
# use std::io::BufferedReader;
# static s : &'static [u8] = bytes!("1 2\n\
# 34 56\n\
# 789 123\n\
# 45 67\n\
# ");
# pub fn new(_inner: Option<File>) -> BufferedReader<MemReader> {
# pub fn new(_inner: IoResult<File>) -> BufferedReader<MemReader> {
# BufferedReader::new(MemReader::new(s.to_owned()))
# }
# }
Expand All @@ -71,7 +71,6 @@ fn read_int_pairs() -> ~[(int,int)] {
let mut pairs = ~[];
// Path takes a generic by-value, rather than by reference
# let _g = std::io::ignore_io_error();
let path = Path::new(&"foo.txt");
let mut reader = BufferedReader::new(File::open(&path));
Expand Down Expand Up @@ -245,15 +244,15 @@ and trapping its exit status using `task::try`:
use std::io::{BufferedReader, File};
use std::task;
# mod BufferedReader {
# use std::io::File;
# use std::io::{File, IoResult};
# use std::io::MemReader;
# use std::io::BufferedReader;
# static s : &'static [u8] = bytes!("1 2\n\
# 34 56\n\
# 789 123\n\
# 45 67\n\
# ");
# pub fn new(_inner: Option<File>) -> BufferedReader<MemReader> {
# pub fn new(_inner: IoResult<File>) -> BufferedReader<MemReader> {
# BufferedReader::new(MemReader::new(s.to_owned()))
# }
# }
Expand All @@ -277,7 +276,6 @@ fn main() {
fn read_int_pairs() -> ~[(int,int)] {
let mut pairs = ~[];
# let _g = std::io::ignore_io_error();
let path = Path::new(&"foo.txt");
let mut reader = BufferedReader::new(File::open(&path));
Expand Down Expand Up @@ -347,15 +345,15 @@ but similarly clear as the version that used `fail!` in the logic where the erro
# #[allow(unused_imports)];
use std::io::{BufferedReader, File};
# mod BufferedReader {
# use std::io::File;
# use std::io::{File, IoResult};
# use std::io::MemReader;
# use std::io::BufferedReader;
# static s : &'static [u8] = bytes!("1 2\n\
# 34 56\n\
# 789 123\n\
# 45 67\n\
# ");
# pub fn new(_inner: Option<File>) -> BufferedReader<MemReader> {
# pub fn new(_inner: IoResult<File>) -> BufferedReader<MemReader> {
# BufferedReader::new(MemReader::new(s.to_owned()))
# }
# }
Expand All @@ -374,7 +372,6 @@ fn main() {
fn read_int_pairs() -> ~[(int,int)] {
let mut pairs = ~[];
# let _g = std::io::ignore_io_error();
let path = Path::new(&"foo.txt");
let mut reader = BufferedReader::new(File::open(&path));
Expand Down Expand Up @@ -415,15 +412,15 @@ and replaces bad input lines with the pair `(-1,-1)`:
# #[allow(unused_imports)];
use std::io::{BufferedReader, File};
# mod BufferedReader {
# use std::io::File;
# use std::io::{File, IoResult};
# use std::io::MemReader;
# use std::io::BufferedReader;
# static s : &'static [u8] = bytes!("1 2\n\
# 34 56\n\
# 789 123\n\
# 45 67\n\
# ");
# pub fn new(_inner: Option<File>) -> BufferedReader<MemReader> {
# pub fn new(_inner: IoResult<File>) -> BufferedReader<MemReader> {
# BufferedReader::new(MemReader::new(s.to_owned()))
# }
# }
Expand All @@ -447,7 +444,6 @@ fn main() {
fn read_int_pairs() -> ~[(int,int)] {
let mut pairs = ~[];
# let _g = std::io::ignore_io_error();
let path = Path::new(&"foo.txt");
let mut reader = BufferedReader::new(File::open(&path));
Expand Down Expand Up @@ -489,15 +485,15 @@ Changing the condition's return type from `(int,int)` to `Option<(int,int)>` wil
# #[allow(unused_imports)];
use std::io::{BufferedReader, File};
# mod BufferedReader {
# use std::io::File;
# use std::io::{IoResult, File};
# use std::io::MemReader;
# use std::io::BufferedReader;
# static s : &'static [u8] = bytes!("1 2\n\
# 34 56\n\
# 789 123\n\
# 45 67\n\
# ");
# pub fn new(_inner: Option<File>) -> BufferedReader<MemReader> {
# pub fn new(_inner: IoResult<File>) -> BufferedReader<MemReader> {
# BufferedReader::new(MemReader::new(s.to_owned()))
# }
# }
Expand All @@ -522,7 +518,6 @@ fn main() {
fn read_int_pairs() -> ~[(int,int)] {
let mut pairs = ~[];
# let _g = std::io::ignore_io_error();
let path = Path::new(&"foo.txt");
let mut reader = BufferedReader::new(File::open(&path));
Expand Down Expand Up @@ -573,15 +568,15 @@ This can be encoded in the handler API by introducing a helper type: `enum Malfo
# #[allow(unused_imports)];
use std::io::{BufferedReader, File};
# mod BufferedReader {
# use std::io::File;
# use std::io::{File, IoResult};
# use std::io::MemReader;
# use std::io::BufferedReader;
# static s : &'static [u8] = bytes!("1 2\n\
# 34 56\n\
# 789 123\n\
# 45 67\n\
# ");
# pub fn new(_inner: Option<File>) -> BufferedReader<MemReader> {
# pub fn new(_inner: IoResult<File>) -> BufferedReader<MemReader> {
# BufferedReader::new(MemReader::new(s.to_owned()))
# }
# }
Expand Down Expand Up @@ -615,7 +610,6 @@ fn main() {
fn read_int_pairs() -> ~[(int,int)] {
let mut pairs = ~[];
# let _g = std::io::ignore_io_error();
let path = Path::new(&"foo.txt");
let mut reader = BufferedReader::new(File::open(&path));
Expand Down Expand Up @@ -696,15 +690,15 @@ a second condition and a helper function will suffice:
# #[allow(unused_imports)];
use std::io::{BufferedReader, File};
# mod BufferedReader {
# use std::io::File;
# use std::io::{File, IoResult};
# use std::io::MemReader;
# use std::io::BufferedReader;
# static s : &'static [u8] = bytes!("1 2\n\
# 34 56\n\
# 789 123\n\
# 45 67\n\
# ");
# pub fn new(_inner: Option<File>) -> BufferedReader<MemReader> {
# pub fn new(_inner: IoResult<File>) -> BufferedReader<MemReader> {
# BufferedReader::new(MemReader::new(s.to_owned()))
# }
# }
Expand Down Expand Up @@ -752,7 +746,6 @@ fn parse_int(x: &str) -> int {
fn read_int_pairs() -> ~[(int,int)] {
let mut pairs = ~[];
# let _g = std::io::ignore_io_error();
let path = Path::new(&"foo.txt");
let mut reader = BufferedReader::new(File::open(&path));
Expand Down
1 change: 1 addition & 0 deletions src/etc/combine-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def scrub(b):
use run_pass_stage2::*;
use std::io;
use std::io::Writer;
#[allow(warnings)]
fn main() {
let mut out = io::stdout();
"""
Expand Down
Loading

0 comments on commit cb40eba

Please sign in to comment.