Skip to content

Commit cb40eba

Browse files
committed
auto merge of #11946 : alexcrichton/rust/no-io-error, r=brson
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
2 parents be4fc63 + c765a8e commit cb40eba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+4359
-4246
lines changed

src/compiletest/compiletest.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,13 @@ pub fn run_tests(config: &config) {
234234
// For context, see #8904
235235
io::test::raise_fd_limit();
236236
let res = test::run_tests_console(&opts, tests);
237-
if !res { fail!("Some tests failed"); }
237+
match res {
238+
Ok(true) => {}
239+
Ok(false) => fail!("Some tests failed"),
240+
Err(e) => {
241+
println!("I/O failure during tests: {}", e);
242+
}
243+
}
238244
}
239245

240246
pub fn test_opts(config: &config) -> test::TestOpts {
@@ -255,7 +261,7 @@ pub fn make_tests(config: &config) -> ~[test::TestDescAndFn] {
255261
debug!("making tests from {}",
256262
config.src_base.display());
257263
let mut tests = ~[];
258-
let dirs = fs::readdir(&config.src_base);
264+
let dirs = fs::readdir(&config.src_base).unwrap();
259265
for file in dirs.iter() {
260266
let file = file.clone();
261267
debug!("inspecting file {}", file.display());

src/compiletest/procsrv.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ pub fn run(lib_path: &str,
5858
});
5959

6060
match opt_process {
61-
Some(ref mut process) => {
61+
Ok(ref mut process) => {
6262
for input in input.iter() {
63-
process.input().write(input.as_bytes());
63+
process.input().write(input.as_bytes()).unwrap();
6464
}
6565
let run::ProcessOutput { status, output, error } = process.finish_with_output();
6666

@@ -70,7 +70,7 @@ pub fn run(lib_path: &str,
7070
err: str::from_utf8_owned(error).unwrap()
7171
})
7272
},
73-
None => None
73+
Err(..) => None
7474
}
7575
}
7676

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

9292
match opt_process {
93-
Some(mut process) => {
93+
Ok(mut process) => {
9494
for input in input.iter() {
95-
process.input().write(input.as_bytes());
95+
process.input().write(input.as_bytes()).unwrap();
9696
}
9797

9898
Some(process)
9999
},
100-
None => None
100+
Err(..) => None
101101
}
102102
}

src/compiletest/runtest.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ fn run_pretty_test(config: &config, props: &TestProps, testfile: &Path) {
153153
let rounds =
154154
match props.pp_exact { Some(_) => 1, None => 2 };
155155

156-
let src = File::open(testfile).read_to_end();
156+
let src = File::open(testfile).read_to_end().unwrap();
157157
let src = str::from_utf8_owned(src).unwrap();
158158
let mut srcs = ~[src];
159159

@@ -175,7 +175,7 @@ fn run_pretty_test(config: &config, props: &TestProps, testfile: &Path) {
175175
let mut expected = match props.pp_exact {
176176
Some(ref file) => {
177177
let filepath = testfile.dir_path().join(file);
178-
let s = File::open(&filepath).read_to_end();
178+
let s = File::open(&filepath).read_to_end().unwrap();
179179
str::from_utf8_owned(s).unwrap()
180180
}
181181
None => { srcs[srcs.len() - 2u].clone() }
@@ -318,8 +318,10 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
318318
//waiting 1 second for gdbserver start
319319
timer::sleep(1000);
320320
let result = task::try(proc() {
321-
tcp::TcpStream::connect(
322-
SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 5039 });
321+
tcp::TcpStream::connect(SocketAddr {
322+
ip: Ipv4Addr(127, 0, 0, 1),
323+
port: 5039,
324+
}).unwrap();
323325
});
324326
if result.is_err() {
325327
continue;
@@ -361,7 +363,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
361363
stdout: out,
362364
stderr: err,
363365
cmdline: cmdline};
364-
process.force_destroy();
366+
process.force_destroy().unwrap();
365367
}
366368

367369
_=> {
@@ -727,7 +729,7 @@ fn compose_and_run_compiler(
727729

728730
fn ensure_dir(path: &Path) {
729731
if path.is_dir() { return; }
730-
fs::mkdir(path, io::UserRWX);
732+
fs::mkdir(path, io::UserRWX).unwrap();
731733
}
732734

733735
fn compose_and_run(config: &config, testfile: &Path,
@@ -852,7 +854,7 @@ fn dump_output(config: &config, testfile: &Path, out: &str, err: &str) {
852854
fn dump_output_file(config: &config, testfile: &Path,
853855
out: &str, extension: &str) {
854856
let outfile = make_out_name(config, testfile, extension);
855-
File::create(&outfile).write(out.as_bytes());
857+
File::create(&outfile).write(out.as_bytes()).unwrap();
856858
}
857859

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

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

11001102

11011103
fn count_extracted_lines(p: &Path) -> uint {
1102-
let x = File::open(&p.with_extension("ll")).read_to_end();
1104+
let x = File::open(&p.with_extension("ll")).read_to_end().unwrap();
11031105
let x = str::from_utf8_owned(x).unwrap();
11041106
x.lines().len()
11051107
}

src/doc/guide-conditions.md

+14-21
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ An example program that does this task reads like this:
4747
# #[allow(unused_imports)];
4848
use std::io::{BufferedReader, File};
4949
# mod BufferedReader {
50-
# use std::io::File;
50+
# use std::io::{File, IoResult};
5151
# use std::io::MemReader;
5252
# use std::io::BufferedReader;
5353
# static s : &'static [u8] = bytes!("1 2\n\
5454
# 34 56\n\
5555
# 789 123\n\
5656
# 45 67\n\
5757
# ");
58-
# pub fn new(_inner: Option<File>) -> BufferedReader<MemReader> {
58+
# pub fn new(_inner: IoResult<File>) -> BufferedReader<MemReader> {
5959
# BufferedReader::new(MemReader::new(s.to_owned()))
6060
# }
6161
# }
@@ -71,7 +71,6 @@ fn read_int_pairs() -> ~[(int,int)] {
7171
let mut pairs = ~[];
7272
7373
// Path takes a generic by-value, rather than by reference
74-
# let _g = std::io::ignore_io_error();
7574
let path = Path::new(&"foo.txt");
7675
let mut reader = BufferedReader::new(File::open(&path));
7776
@@ -245,15 +244,15 @@ and trapping its exit status using `task::try`:
245244
use std::io::{BufferedReader, File};
246245
use std::task;
247246
# mod BufferedReader {
248-
# use std::io::File;
247+
# use std::io::{File, IoResult};
249248
# use std::io::MemReader;
250249
# use std::io::BufferedReader;
251250
# static s : &'static [u8] = bytes!("1 2\n\
252251
# 34 56\n\
253252
# 789 123\n\
254253
# 45 67\n\
255254
# ");
256-
# pub fn new(_inner: Option<File>) -> BufferedReader<MemReader> {
255+
# pub fn new(_inner: IoResult<File>) -> BufferedReader<MemReader> {
257256
# BufferedReader::new(MemReader::new(s.to_owned()))
258257
# }
259258
# }
@@ -277,7 +276,6 @@ fn main() {
277276
278277
fn read_int_pairs() -> ~[(int,int)] {
279278
let mut pairs = ~[];
280-
# let _g = std::io::ignore_io_error();
281279
let path = Path::new(&"foo.txt");
282280
283281
let mut reader = BufferedReader::new(File::open(&path));
@@ -347,15 +345,15 @@ but similarly clear as the version that used `fail!` in the logic where the erro
347345
# #[allow(unused_imports)];
348346
use std::io::{BufferedReader, File};
349347
# mod BufferedReader {
350-
# use std::io::File;
348+
# use std::io::{File, IoResult};
351349
# use std::io::MemReader;
352350
# use std::io::BufferedReader;
353351
# static s : &'static [u8] = bytes!("1 2\n\
354352
# 34 56\n\
355353
# 789 123\n\
356354
# 45 67\n\
357355
# ");
358-
# pub fn new(_inner: Option<File>) -> BufferedReader<MemReader> {
356+
# pub fn new(_inner: IoResult<File>) -> BufferedReader<MemReader> {
359357
# BufferedReader::new(MemReader::new(s.to_owned()))
360358
# }
361359
# }
@@ -374,7 +372,6 @@ fn main() {
374372
375373
fn read_int_pairs() -> ~[(int,int)] {
376374
let mut pairs = ~[];
377-
# let _g = std::io::ignore_io_error();
378375
let path = Path::new(&"foo.txt");
379376
380377
let mut reader = BufferedReader::new(File::open(&path));
@@ -415,15 +412,15 @@ and replaces bad input lines with the pair `(-1,-1)`:
415412
# #[allow(unused_imports)];
416413
use std::io::{BufferedReader, File};
417414
# mod BufferedReader {
418-
# use std::io::File;
415+
# use std::io::{File, IoResult};
419416
# use std::io::MemReader;
420417
# use std::io::BufferedReader;
421418
# static s : &'static [u8] = bytes!("1 2\n\
422419
# 34 56\n\
423420
# 789 123\n\
424421
# 45 67\n\
425422
# ");
426-
# pub fn new(_inner: Option<File>) -> BufferedReader<MemReader> {
423+
# pub fn new(_inner: IoResult<File>) -> BufferedReader<MemReader> {
427424
# BufferedReader::new(MemReader::new(s.to_owned()))
428425
# }
429426
# }
@@ -447,7 +444,6 @@ fn main() {
447444
448445
fn read_int_pairs() -> ~[(int,int)] {
449446
let mut pairs = ~[];
450-
# let _g = std::io::ignore_io_error();
451447
let path = Path::new(&"foo.txt");
452448
453449
let mut reader = BufferedReader::new(File::open(&path));
@@ -489,15 +485,15 @@ Changing the condition's return type from `(int,int)` to `Option<(int,int)>` wil
489485
# #[allow(unused_imports)];
490486
use std::io::{BufferedReader, File};
491487
# mod BufferedReader {
492-
# use std::io::File;
488+
# use std::io::{IoResult, File};
493489
# use std::io::MemReader;
494490
# use std::io::BufferedReader;
495491
# static s : &'static [u8] = bytes!("1 2\n\
496492
# 34 56\n\
497493
# 789 123\n\
498494
# 45 67\n\
499495
# ");
500-
# pub fn new(_inner: Option<File>) -> BufferedReader<MemReader> {
496+
# pub fn new(_inner: IoResult<File>) -> BufferedReader<MemReader> {
501497
# BufferedReader::new(MemReader::new(s.to_owned()))
502498
# }
503499
# }
@@ -522,7 +518,6 @@ fn main() {
522518
523519
fn read_int_pairs() -> ~[(int,int)] {
524520
let mut pairs = ~[];
525-
# let _g = std::io::ignore_io_error();
526521
let path = Path::new(&"foo.txt");
527522
528523
let mut reader = BufferedReader::new(File::open(&path));
@@ -573,15 +568,15 @@ This can be encoded in the handler API by introducing a helper type: `enum Malfo
573568
# #[allow(unused_imports)];
574569
use std::io::{BufferedReader, File};
575570
# mod BufferedReader {
576-
# use std::io::File;
571+
# use std::io::{File, IoResult};
577572
# use std::io::MemReader;
578573
# use std::io::BufferedReader;
579574
# static s : &'static [u8] = bytes!("1 2\n\
580575
# 34 56\n\
581576
# 789 123\n\
582577
# 45 67\n\
583578
# ");
584-
# pub fn new(_inner: Option<File>) -> BufferedReader<MemReader> {
579+
# pub fn new(_inner: IoResult<File>) -> BufferedReader<MemReader> {
585580
# BufferedReader::new(MemReader::new(s.to_owned()))
586581
# }
587582
# }
@@ -615,7 +610,6 @@ fn main() {
615610
616611
fn read_int_pairs() -> ~[(int,int)] {
617612
let mut pairs = ~[];
618-
# let _g = std::io::ignore_io_error();
619613
let path = Path::new(&"foo.txt");
620614
621615
let mut reader = BufferedReader::new(File::open(&path));
@@ -696,15 +690,15 @@ a second condition and a helper function will suffice:
696690
# #[allow(unused_imports)];
697691
use std::io::{BufferedReader, File};
698692
# mod BufferedReader {
699-
# use std::io::File;
693+
# use std::io::{File, IoResult};
700694
# use std::io::MemReader;
701695
# use std::io::BufferedReader;
702696
# static s : &'static [u8] = bytes!("1 2\n\
703697
# 34 56\n\
704698
# 789 123\n\
705699
# 45 67\n\
706700
# ");
707-
# pub fn new(_inner: Option<File>) -> BufferedReader<MemReader> {
701+
# pub fn new(_inner: IoResult<File>) -> BufferedReader<MemReader> {
708702
# BufferedReader::new(MemReader::new(s.to_owned()))
709703
# }
710704
# }
@@ -752,7 +746,6 @@ fn parse_int(x: &str) -> int {
752746
753747
fn read_int_pairs() -> ~[(int,int)] {
754748
let mut pairs = ~[];
755-
# let _g = std::io::ignore_io_error();
756749
let path = Path::new(&"foo.txt");
757750
758751
let mut reader = BufferedReader::new(File::open(&path));

src/etc/combine-tests.py

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def scrub(b):
6969
use run_pass_stage2::*;
7070
use std::io;
7171
use std::io::Writer;
72+
#[allow(warnings)]
7273
fn main() {
7374
let mut out = io::stdout();
7475
"""

0 commit comments

Comments
 (0)